Merge pull request #149 from TeamBNBN/be/Registration

feat: Registration 강의별 수강신청 현황 조회 추가
This commit is contained in:
Jungmin 2024-07-31 12:43:02 +09:00 committed by GitHub
commit 80fc29a195
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 100 additions and 30 deletions

View File

@ -28,5 +28,8 @@ public interface LectureService {
Lecture findLectureByTitle(String title); Lecture findLectureByTitle(String title);
void changeState(Long lectureId); void changeState(Long lectureId);
boolean getState(Long lectureId); boolean getState(Long lectureId);
boolean checkTeacher(Long userId, Long lectureId);
} }

View File

@ -223,8 +223,7 @@ public class LectureServiceImpl implements LectureService {
Optional<Lecture> lecture = lectureRepository.findById(id); Optional<Lecture> lecture = lectureRepository.findById(id);
Lecture l; Lecture l;
if(lecture.isPresent()) if (lecture.isPresent()) {
{
l = lecture.get(); l = lecture.get();
System.out.println(l.isOnline()); System.out.println(l.isOnline());
@ -232,7 +231,7 @@ public class LectureServiceImpl implements LectureService {
System.out.println(l.isOnline()); System.out.println(l.isOnline());
}else { } else {
throw new RuntimeException("Lecture not found with id: " + id); throw new RuntimeException("Lecture not found with id: " + id);
} }
@ -247,5 +246,11 @@ public class LectureServiceImpl implements LectureService {
} }
@Override
public boolean checkTeacher(Long userId, Long lectureId) {
Optional<Lecture> lecture = lectureRepository.findById(lectureId);
return lecture.isPresent() && lecture.get().getUser().getId() == userId;
}
} }

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.registration.controller; package com.edufocus.edufocus.registration.controller;
import com.edufocus.edufocus.lecture.service.LectureService;
import com.edufocus.edufocus.registration.service.RegistrationService; import com.edufocus.edufocus.registration.service.RegistrationService;
import com.edufocus.edufocus.user.util.JWTUtil; import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -16,7 +17,9 @@ import java.util.Map;
@RequiredArgsConstructor @RequiredArgsConstructor
public class RegistrationController { public class RegistrationController {
private final RegistrationService registrationServiceImpl; private final RegistrationService registrationService;
private final LectureService lectureService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
@ -25,7 +28,7 @@ public class RegistrationController {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
Long lectureId = map.get("lectureId"); Long lectureId = map.get("lectureId");
if (!registrationServiceImpl.createRegistration(userId, lectureId)) { if (!registrationService.createRegistration(userId, lectureId)) {
return new ResponseEntity<>(HttpStatus.CONFLICT); return new ResponseEntity<>(HttpStatus.CONFLICT);
} }
@ -36,12 +39,10 @@ public class RegistrationController {
public ResponseEntity<?> acceptRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) { public ResponseEntity<?> acceptRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!registrationServiceImpl.acceptRegistration(userId, registrationId)) { if (!registrationService.acceptRegistration(userId, registrationId)) {
String msg = new String("Not Acceptable"); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} }
String msg = new String("registration accepted");
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@ -49,12 +50,22 @@ public class RegistrationController {
public ResponseEntity<?> deleteRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) { public ResponseEntity<?> deleteRigistration(@RequestHeader("Authorization") String accessToken, @PathVariable long registrationId) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!registrationServiceImpl.deleteRegistration(userId, registrationId)) { if (!registrationService.deleteRegistration(userId, registrationId)) {
String msg = new String("Not Acceptable"); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} }
return new ResponseEntity<>(HttpStatus.NO_CONTENT); 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);
}
} }

View File

@ -26,5 +26,5 @@ public class Registration {
private Lecture lecture; private Lecture lecture;
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private RegistrationStatus status ; private RegistrationStatus status;
} }

View File

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

View File

@ -2,6 +2,7 @@ package com.edufocus.edufocus.registration.repository;
import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.entity.Registration;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -11,5 +12,7 @@ import java.util.List;
public interface RegistrationRepository extends JpaRepository<Registration, Long> { public interface RegistrationRepository extends JpaRepository<Registration, Long> {
List<Registration> findAllByUserId(@Param("userId") Long userId); List<Registration> findAllByUserId(@Param("userId") Long userId);
List<Registration> findAllNotAcceptedByLectureId(@Param("lectureId") Long lectureId);
Registration findByUserIdAndLectureId(Long userId, Long lectureId); Registration findByUserIdAndLectureId(Long userId, Long lectureId);
} }

View File

@ -1,17 +1,22 @@
package com.edufocus.edufocus.registration.service; package com.edufocus.edufocus.registration.service;
import com.edufocus.edufocus.registration.entity.Registration; 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.entity.RegistrationStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public interface RegistrationService { 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<RegistrationSearchResponse> searchRegistrations(Long LectureId);
RegistrationStatus isOnline(Long userId, Long lectureId);
} }

View File

@ -1,14 +1,20 @@
package com.edufocus.edufocus.registration.service; package com.edufocus.edufocus.registration.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.repository.LectureRepository; 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.RegistrationSearchResponse;
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.entity.User;
import com.edufocus.edufocus.user.model.entity.UserRole;
import com.edufocus.edufocus.user.model.repository.UserRepository; 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;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service @Service
@ -21,14 +27,16 @@ public class RegistrationServiceImpl implements RegistrationService {
private final LectureRepository lectureRepository; private final LectureRepository lectureRepository;
@Override @Override
public boolean createRegistration(long userId, long lectureId) { public boolean createRegistration(Long userId, Long lectureId) {
if (registrationRepository.findByUserIdAndLectureId(userId, lectureId) != null) { Optional<User> user = userRepository.findById(userId);
if (user.isEmpty() || user.get().getRole().equals(UserRole.ADMIN)) {
return false; return false;
} }
Registration registration = new Registration().builder() Registration registration = new Registration().builder()
.user(userRepository.getReferenceById(userId)) .user(userRepository.findById(userId).get())
.lecture(lectureRepository.getReferenceById(lectureId)) .lecture(lectureRepository.findById(lectureId).get())
.status(RegistrationStatus.WAITING) .status(RegistrationStatus.WAITING)
.build(); .build();
@ -37,24 +45,24 @@ public class RegistrationServiceImpl implements RegistrationService {
} }
@Override @Override
public boolean acceptRegistration(long userId, long registrationId) { public boolean acceptRegistration(Long userId, Long registrationId) {
Registration registration = registrationRepository.findById(registrationId).get(); Optional<Registration> registration = registrationRepository.findById(registrationId);
if (registration.getLecture().getUser().getId() != userId) { if (registration.isEmpty() || registration.get().getLecture().getUser().getId() != userId) {
return false; return false;
} }
registration.setStatus(RegistrationStatus.valueOf("ACCEPTED")); registration.get().setStatus(RegistrationStatus.valueOf("ACCEPTED"));
registrationRepository.save(registration); registrationRepository.save(registration.get());
return true; return true;
} }
@Override @Override
public boolean deleteRegistration(long userId, long registrationId) { public boolean deleteRegistration(Long userId, Long registrationId) {
Registration registration = registrationRepository.findById(registrationId).get(); Optional<Registration> registration = registrationRepository.findById(registrationId);
if (registration.getLecture().getUser().getId() != userId) { if (registration.isEmpty() || registration.get().getLecture().getUser().getId() != userId) {
return false; return false;
} }
@ -62,10 +70,27 @@ public class RegistrationServiceImpl implements RegistrationService {
return true; return true;
} }
@Override
public List<RegistrationSearchResponse> searchRegistrations(Long lectureId) {
List<Registration> registrations = registrationRepository.findAllNotAcceptedByLectureId(lectureId);
List<RegistrationSearchResponse> 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 @Override
public RegistrationStatus isOnline(Long userId, Long lectureId) { public RegistrationStatus isOnline(Long userId, Long lectureId) {
Registration registration = registrationRepository.findByUserIdAndLectureId(userId,lectureId); Registration registration = registrationRepository.findByUserIdAndLectureId(userId, lectureId);
return registration.getStatus(); return registration.getStatus();
} }