feat: Registration 수강신청, 승인, 거절 수정

This commit is contained in:
kgc9007 2024-07-22 17:51:57 +09:00
parent bb27d409d6
commit 3eac0e45b2
4 changed files with 31 additions and 19 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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> 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> registration = registrationRepository.findById(registrationId);
Registration registration = registrationRepository.findById(registrationId).get();
return registration.isPresent() && registration.get().getStatus().equals("ACCEPTED");
return registration.getStatus().equals("ACCEPTED");
}
}