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.entity.Registration;
import com.edufocus.edufocus.registration.service.RegistrationService; import com.edufocus.edufocus.registration.service.RegistrationService;
import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -10,29 +12,30 @@ import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping("/registration") @RequestMapping("/registration")
@Slf4j @Slf4j
@RequiredArgsConstructor
public class RegistrationController { public class RegistrationController {
private final RegistrationService registrationServiceImpl; private final RegistrationService registrationServiceImpl;
public RegistrationController(RegistrationService registrationServiceImpl) { private final JWTUtil jwtUtil;
this.registrationServiceImpl = registrationServiceImpl;
}
@PostMapping @PostMapping
public ResponseEntity<?> register(@RequestBody Registration registration) { public ResponseEntity<?> register(@RequestHeader("Authorization") String accessToken, @RequestBody long lectureId) {
registrationServiceImpl.createRegistration(registration); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
registrationServiceImpl.createRegistration(userId, lectureId);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@PatchMapping("/registrationId/{registrationId}") @PutMapping("/{registrationId}")
public ResponseEntity<?> acceptRigistration(@PathVariable long registrationId) { public ResponseEntity<?> acceptRigistration(@PathVariable long registrationId) {
registrationServiceImpl.acceptRegistration(registrationId); registrationServiceImpl.acceptRegistration(registrationId);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@DeleteMapping("/registrationId/{registrationId}") @DeleteMapping("/{registrationId}")
public ResponseEntity<?> deleteRigistration(@PathVariable long registrationId) { public ResponseEntity<?> deleteRigistration(@PathVariable long registrationId) {
registrationServiceImpl.deleteRegistration(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.lecture.entity.Lecture;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.*;
import lombok.Setter;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Registration { public class Registration {
@Id @Id

View File

@ -6,7 +6,7 @@ import org.springframework.stereotype.Service;
@Service @Service
public interface RegistrationService { public interface RegistrationService {
void createRegistration(Registration registration); void createRegistration(long userId, long registrationId);
void acceptRegistration(long RegistrationId); void acceptRegistration(long RegistrationId);

View File

@ -1,8 +1,10 @@
package com.edufocus.edufocus.registration.service; 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.Registration;
import com.edufocus.edufocus.registration.entity.RegistrationStatus; import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.repository.RegistrationRepository; import com.edufocus.edufocus.registration.repository.RegistrationRepository;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -15,21 +17,26 @@ import java.util.Optional;
public class RegistrationServiceImpl implements RegistrationService { public class RegistrationServiceImpl implements RegistrationService {
private final RegistrationRepository registrationRepository; private final RegistrationRepository registrationRepository;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
@Override @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); registrationRepository.save(registration);
} }
@Override @Override
public void acceptRegistration(long registrationId) { public void acceptRegistration(long registrationId) {
Optional<Registration> registration = registrationRepository.findById(registrationId); Registration registration = registrationRepository.findById(registrationId).get();
if (registration.isPresent()) { registration.setStatus(RegistrationStatus.valueOf("ACCEPTED"));
Registration reg = registration.get(); registrationRepository.save(registration);
reg.setStatus(RegistrationStatus.valueOf("ACCEPTED"));
registrationRepository.save(reg);
}
} }
@Override @Override
@ -39,8 +46,8 @@ public class RegistrationServiceImpl implements RegistrationService {
@Override @Override
public boolean isAcceptedRegistration(long registrationId) { 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");
} }
} }