feat: Registration 강의별 수강신청 현황 조회 추가

This commit is contained in:
kgc9007 2024-07-31 11:17:46 +09:00
parent 7cd5f493cf
commit 34050e912d
8 changed files with 99 additions and 30 deletions

View File

@ -28,4 +28,6 @@ public interface LectureService {
Lecture findLectureByTitle(String title);
void changeState(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);
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> lecture = lectureRepository.findById(lectureId);
return lecture.isPresent() && lecture.get().getUser().getId() == userId;
}
}

View File

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

View File

@ -26,5 +26,5 @@ public class Registration {
private Lecture lecture;
@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 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<Registration, Long> {
List<Registration> findAllByUserId(@Param("userId") Long userId);
List<Registration> findAllNotAcceptedByLectureId(@Param("lectureId") Long lectureId);
Registration findByUserIdAndLectureId(Long userId, Long lectureId);
}

View File

@ -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<RegistrationSearchResponse> searchRegistrations(Long LectureId);
RegistrationStatus isOnline(Long userId, Long lectureId);
}

View File

@ -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> 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> 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> 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<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
public RegistrationStatus isOnline(Long userId, Long lectureId) {
Registration registration = registrationRepository.findByUserIdAndLectureId(userId,lectureId);
Registration registration = registrationRepository.findByUserIdAndLectureId(userId, lectureId);
return registration.getStatus();
}