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 fc6e67d..2534d69 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 @@ -2,6 +2,8 @@ package com.edufocus.edufocus.registration.controller; import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.service.RegistrationService; +import com.edufocus.edufocus.user.util.JWTUtil; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -10,29 +12,30 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/registration") @Slf4j +@RequiredArgsConstructor public class RegistrationController { private final RegistrationService registrationServiceImpl; - public RegistrationController(RegistrationService registrationServiceImpl) { - this.registrationServiceImpl = registrationServiceImpl; - } + private final JWTUtil jwtUtil; @PostMapping - public ResponseEntity register(@RequestBody Registration registration) { - registrationServiceImpl.createRegistration(registration); + public ResponseEntity register(@RequestHeader("Authorization") String accessToken, @RequestBody long lectureId) { + Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); + + registrationServiceImpl.createRegistration(userId, lectureId); return new ResponseEntity<>(HttpStatus.OK); } - @PatchMapping("/registrationId/{registrationId}") + @PutMapping("/{registrationId}") public ResponseEntity acceptRigistration(@PathVariable long registrationId) { registrationServiceImpl.acceptRegistration(registrationId); return new ResponseEntity<>(HttpStatus.OK); } - @DeleteMapping("/registrationId/{registrationId}") + @DeleteMapping("/{registrationId}") public ResponseEntity deleteRigistration(@PathVariable long registrationId) { registrationServiceImpl.deleteRegistration(registrationId); 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 7e1cce6..4a81aba 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 @@ -3,12 +3,14 @@ package com.edufocus.edufocus.registration.entity; import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.user.model.entity.User; import jakarta.persistence.*; -import lombok.Getter; -import lombok.Setter; +import lombok.*; @Entity @Getter @Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class Registration { @Id 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 fcaac94..4553408 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 @@ -6,7 +6,7 @@ import org.springframework.stereotype.Service; @Service public interface RegistrationService { - void createRegistration(Registration registration); + void createRegistration(long userId, long registrationId); void acceptRegistration(long RegistrationId); 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 6eadce6..507e084 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,8 +1,10 @@ package com.edufocus.edufocus.registration.service; +import com.edufocus.edufocus.lecture.repository.LectureRepository; import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.entity.RegistrationStatus; import com.edufocus.edufocus.registration.repository.RegistrationRepository; +import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,21 +17,26 @@ import java.util.Optional; public class RegistrationServiceImpl implements RegistrationService { private final RegistrationRepository registrationRepository; + private final UserRepository userRepository; + private final LectureRepository lectureRepository; @Override - public void createRegistration(Registration registration) { + public void createRegistration(long userId, long lectureId) { + Registration registration = new Registration().builder() + .user(userRepository.getReferenceById(userId)) + .lecture(lectureRepository.getReferenceById(lectureId)) + .status(RegistrationStatus.WAITING) + .build(); + registrationRepository.save(registration); } @Override public void acceptRegistration(long registrationId) { - Optional registration = registrationRepository.findById(registrationId); + Registration registration = registrationRepository.findById(registrationId).get(); - if (registration.isPresent()) { - Registration reg = registration.get(); - reg.setStatus(RegistrationStatus.valueOf("ACCEPTED")); - registrationRepository.save(reg); - } + registration.setStatus(RegistrationStatus.valueOf("ACCEPTED")); + registrationRepository.save(registration); } @Override @@ -39,8 +46,8 @@ public class RegistrationServiceImpl implements RegistrationService { @Override public boolean isAcceptedRegistration(long registrationId) { - Optional registration = registrationRepository.findById(registrationId); + Registration registration = registrationRepository.findById(registrationId).get(); - return registration.isPresent() && registration.get().getStatus().equals("ACCEPTED"); + return registration.getStatus().equals("ACCEPTED"); } }