From 34050e912d986a5a8059b2bb61fad9b7da40684c Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Wed, 31 Jul 2024 11:17:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Registration=20=EA=B0=95=EC=9D=98?= =?UTF-8?q?=EB=B3=84=20=EC=88=98=EA=B0=95=EC=8B=A0=EC=B2=AD=20=ED=98=84?= =?UTF-8?q?=ED=99=A9=20=EC=A1=B0=ED=9A=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/service/LectureService.java | 2 + .../lecture/service/LectureServiceImpl.java | 11 ++-- .../controller/RegistrationController.java | 29 +++++++---- .../registration/entity/Registration.java | 2 +- .../entity/RegistrationSearchResponse.java | 18 +++++++ .../repository/RegistrationRepository.java | 3 ++ .../service/RegistrationService.java | 13 +++-- .../service/RegistrationServiceImpl.java | 51 ++++++++++++++----- 8 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/registration/entity/RegistrationSearchResponse.java diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java index 65153a0..50e3b3c 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java @@ -28,4 +28,6 @@ public interface LectureService { Lecture findLectureByTitle(String title); void changeState(Long lectureId); + + boolean checkTeacher(Long userId, Long lectureId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index 90da51c..2ba7032 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -223,8 +223,7 @@ public class LectureServiceImpl implements LectureService { Optional lecture = lectureRepository.findById(id); Lecture l; - if(lecture.isPresent()) - { + if (lecture.isPresent()) { l = lecture.get(); System.out.println(l.isOnline()); @@ -232,12 +231,18 @@ public class LectureServiceImpl implements LectureService { System.out.println(l.isOnline()); - }else { + } else { throw new RuntimeException("Lecture not found with id: " + id); } lectureRepository.save(l); } + @Override + public boolean checkTeacher(Long userId, Long lectureId) { + Optional lecture = lectureRepository.findById(lectureId); + + return lecture.isPresent() && lecture.get().getUser().getId() == userId; + } } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java b/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java index ee36efb..6e008fe 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/controller/RegistrationController.java @@ -1,5 +1,6 @@ package com.edufocus.edufocus.registration.controller; +import com.edufocus.edufocus.lecture.service.LectureService; import com.edufocus.edufocus.registration.service.RegistrationService; import com.edufocus.edufocus.user.util.JWTUtil; import lombok.RequiredArgsConstructor; @@ -16,7 +17,9 @@ import java.util.Map; @RequiredArgsConstructor public class RegistrationController { - private final RegistrationService registrationServiceImpl; + private final RegistrationService registrationService; + + private final LectureService lectureService; private final JWTUtil jwtUtil; @@ -25,7 +28,7 @@ public class RegistrationController { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long lectureId = map.get("lectureId"); - if (!registrationServiceImpl.createRegistration(userId, lectureId)) { + if (!registrationService.createRegistration(userId, lectureId)) { return new ResponseEntity<>(HttpStatus.CONFLICT); } @@ -36,12 +39,10 @@ public class RegistrationController { public ResponseEntity acceptRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); - if (!registrationServiceImpl.acceptRegistration(userId, registrationId)) { - String msg = new String("Not Acceptable"); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + if (!registrationService.acceptRegistration(userId, registrationId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - String msg = new String("registration accepted"); return new ResponseEntity<>(HttpStatus.OK); } @@ -49,12 +50,22 @@ public class RegistrationController { public ResponseEntity deleteRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); - if (!registrationServiceImpl.deleteRegistration(userId, registrationId)) { - String msg = new String("Not Acceptable"); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + if (!registrationService.deleteRegistration(userId, registrationId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + @GetMapping("/{lectureId}") + public ResponseEntity getRegistrations(@RequestHeader("Authorization") String accessToken, @PathVariable long lectureId) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + if (!lectureService.checkTeacher(userId, lectureId)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(registrationService.searchRegistrations(lectureId), HttpStatus.OK); + } + } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/entity/Registration.java b/backend/src/main/java/com/edufocus/edufocus/registration/entity/Registration.java index 4a81aba..38cfe35 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/entity/Registration.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/entity/Registration.java @@ -26,5 +26,5 @@ public class Registration { private Lecture lecture; @Enumerated(EnumType.STRING) - private RegistrationStatus status ; + private RegistrationStatus status; } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/entity/RegistrationSearchResponse.java b/backend/src/main/java/com/edufocus/edufocus/registration/entity/RegistrationSearchResponse.java new file mode 100644 index 0000000..e9c1b46 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/registration/entity/RegistrationSearchResponse.java @@ -0,0 +1,18 @@ +package com.edufocus.edufocus.registration.entity; + +import com.edufocus.edufocus.lecture.entity.Lecture; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class RegistrationSearchResponse { + + private Long id; + + private String userName; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java b/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java index 251acaf..f8c31b7 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/repository/RegistrationRepository.java @@ -2,6 +2,7 @@ package com.edufocus.edufocus.registration.repository; import com.edufocus.edufocus.registration.entity.Registration; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -11,5 +12,7 @@ import java.util.List; public interface RegistrationRepository extends JpaRepository { List findAllByUserId(@Param("userId") Long userId); + List findAllNotAcceptedByLectureId(@Param("lectureId") Long lectureId); + Registration findByUserIdAndLectureId(Long userId, Long lectureId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationService.java b/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationService.java index bc27b37..3c7d477 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationService.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationService.java @@ -1,17 +1,22 @@ package com.edufocus.edufocus.registration.service; import com.edufocus.edufocus.registration.entity.Registration; +import com.edufocus.edufocus.registration.entity.RegistrationSearchResponse; import com.edufocus.edufocus.registration.entity.RegistrationStatus; import org.springframework.stereotype.Service; +import java.util.List; + @Service public interface RegistrationService { - boolean createRegistration(long userId, long registrationId); + boolean createRegistration(Long userId, Long registrationId); - boolean acceptRegistration(long userId, long RegistrationId); + boolean acceptRegistration(Long userId, Long RegistrationId); - boolean deleteRegistration(long userId, long registrationId); + boolean deleteRegistration(Long userId, Long registrationId); - RegistrationStatus isOnline(Long userId , Long lectureId); + List searchRegistrations(Long LectureId); + + RegistrationStatus isOnline(Long userId, Long lectureId); } diff --git a/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationServiceImpl.java index 0eca5b5..e36c9a1 100644 --- a/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/registration/service/RegistrationServiceImpl.java @@ -1,14 +1,20 @@ package com.edufocus.edufocus.registration.service; +import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.lecture.repository.LectureRepository; import com.edufocus.edufocus.registration.entity.Registration; +import com.edufocus.edufocus.registration.entity.RegistrationSearchResponse; import com.edufocus.edufocus.registration.entity.RegistrationStatus; import com.edufocus.edufocus.registration.repository.RegistrationRepository; +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.entity.UserRole; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; @Service @@ -21,14 +27,16 @@ public class RegistrationServiceImpl implements RegistrationService { private final LectureRepository lectureRepository; @Override - public boolean createRegistration(long userId, long lectureId) { - if (registrationRepository.findByUserIdAndLectureId(userId, lectureId) != null) { + public boolean createRegistration(Long userId, Long lectureId) { + Optional user = userRepository.findById(userId); + + if (user.isEmpty() || user.get().getRole().equals(UserRole.ADMIN)) { return false; } Registration registration = new Registration().builder() - .user(userRepository.getReferenceById(userId)) - .lecture(lectureRepository.getReferenceById(lectureId)) + .user(userRepository.findById(userId).get()) + .lecture(lectureRepository.findById(lectureId).get()) .status(RegistrationStatus.WAITING) .build(); @@ -37,24 +45,24 @@ public class RegistrationServiceImpl implements RegistrationService { } @Override - public boolean acceptRegistration(long userId, long registrationId) { - Registration registration = registrationRepository.findById(registrationId).get(); + public boolean acceptRegistration(Long userId, Long registrationId) { + Optional registration = registrationRepository.findById(registrationId); - if (registration.getLecture().getUser().getId() != userId) { + if (registration.isEmpty() || registration.get().getLecture().getUser().getId() != userId) { return false; } - registration.setStatus(RegistrationStatus.valueOf("ACCEPTED")); - registrationRepository.save(registration); + registration.get().setStatus(RegistrationStatus.valueOf("ACCEPTED")); + registrationRepository.save(registration.get()); return true; } @Override - public boolean deleteRegistration(long userId, long registrationId) { - Registration registration = registrationRepository.findById(registrationId).get(); + public boolean deleteRegistration(Long userId, Long registrationId) { + Optional registration = registrationRepository.findById(registrationId); - if (registration.getLecture().getUser().getId() != userId) { + if (registration.isEmpty() || registration.get().getLecture().getUser().getId() != userId) { return false; } @@ -62,10 +70,27 @@ public class RegistrationServiceImpl implements RegistrationService { return true; } + @Override + public List searchRegistrations(Long lectureId) { + List registrations = registrationRepository.findAllNotAcceptedByLectureId(lectureId); + + List responses = new ArrayList<>(); + for (Registration registration : registrations) { + RegistrationSearchResponse response = new RegistrationSearchResponse().builder() + .id(registration.getId()) + .userName(registration.getUser().getName()) + .build(); + + responses.add(response); + } + + return responses; + } + @Override public RegistrationStatus isOnline(Long userId, Long lectureId) { - Registration registration = registrationRepository.findByUserIdAndLectureId(userId,lectureId); + Registration registration = registrationRepository.findByUserIdAndLectureId(userId, lectureId); return registration.getStatus(); }