diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java index 822c7879..623366b9 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/domain/ApplyEntity.java @@ -42,6 +42,15 @@ public ApplyEntity(Inspection inspection, String introduce, Long study, Long use this.userId = userId; } + public static ApplyEntity of(Long userId, Long studyId, String introduce) { + return ApplyEntity.builder() + .userId(userId) + .study(studyId) + .introduce(introduce) + .inspection(Inspection.STANDBY) + .build(); + } + public void update(Inspection inspection) { this.inspection = inspection; } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepository.java b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepository.java index 6f2a53af..93fd817b 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepository.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepository.java @@ -6,9 +6,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; public interface ApplyRepository extends JpaRepository, ApplyRepositoryCustom { - ApplyEntity findByUserIdAndStudyId(Long userId, Long studyId); + Optional findByUserIdAndStudyId(Long userId, Long studyId); List findByUserId(Long userId); } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyService.java b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyService.java index ca1e6809..154f3636 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyService.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyService.java @@ -1,57 +1,46 @@ package kr.co.studyhubinu.studyhubserver.apply.service; -import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity; import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData; import kr.co.studyhubinu.studyhubserver.apply.dto.request.EnrollApplyRequest; import kr.co.studyhubinu.studyhubserver.apply.dto.request.FindApplyRequest; import kr.co.studyhubinu.studyhubserver.apply.dto.request.UpdateApplyRequest; import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse; -import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection; import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository; import kr.co.studyhubinu.studyhubserver.common.dto.Converter; +import kr.co.studyhubinu.studyhubserver.exception.apply.ApplyNotFoundException; +import kr.co.studyhubinu.studyhubserver.exception.apply.SameUserRequestException; import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException; import kr.co.studyhubinu.studyhubserver.study.StudyRepository; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity; import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository; +import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -@Service @RequiredArgsConstructor -@Transactional(readOnly = true) +@Service public class ApplyService { private final UserRepository userRepository; private final StudyRepository studyRepository; private final ApplyRepository applyRepository; - @Transactional public void enroll(Long userId, EnrollApplyRequest request) { UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); StudyEntity study = studyRepository.findById(request.getStudyId()).orElseThrow(); - ApplyEntity apply = toApplyEntity(user, study); + validateSameRequest(user, study); - applyRepository.save(apply); + applyRepository.save(ApplyEntity.of(user.getId(), study.getId(), request.getIntroduce())); } - private ApplyEntity toApplyEntity(UserEntity user, StudyEntity study) { - return ApplyEntity.builder() - .userId(user.getId()) - .study(study.getId()) - .inspection(Inspection.STANDBY) - .build(); - } - - @Transactional public void update(UpdateApplyRequest request) { UserEntity user = userRepository.findById(request.getUserId()).orElseThrow(UserNotFoundException::new); StudyEntity study = studyRepository.findById(request.getStudyId()).orElseThrow(); - ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); + ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).orElseThrow(ApplyNotFoundException::new); applyEntity.update(request.getInspection()); } @@ -61,4 +50,10 @@ public FindApplyResponse findApply(FindApplyRequest request, final int page, fin return new FindApplyResponse((long) size, userData); } + + private void validateSameRequest(UserEntity user, StudyEntity study) { + if(applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).isPresent()) { + throw new SameUserRequestException(); + } + } } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/ApplyNotFoundException.java b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/ApplyNotFoundException.java new file mode 100644 index 00000000..27547246 --- /dev/null +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/ApplyNotFoundException.java @@ -0,0 +1,25 @@ +package kr.co.studyhubinu.studyhubserver.exception.apply; + +import kr.co.studyhubinu.studyhubserver.exception.StatusType; +import kr.co.studyhubinu.studyhubserver.exception.common.CustomException; + +public class ApplyNotFoundException extends CustomException { + + private final StatusType status; + private static final String message = "조회되는 요청이 없습니다."; + + public ApplyNotFoundException() { + super(message); + this.status = StatusType.BAD_REQUEST; + } + + @Override + public StatusType getStatus() { + return status; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/SameUserRequestException.java b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/SameUserRequestException.java new file mode 100644 index 00000000..dda2ed83 --- /dev/null +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/apply/SameUserRequestException.java @@ -0,0 +1,25 @@ +package kr.co.studyhubinu.studyhubserver.exception.apply; + +import kr.co.studyhubinu.studyhubserver.exception.StatusType; +import kr.co.studyhubinu.studyhubserver.exception.common.CustomException; + +public class SameUserRequestException extends CustomException { + + private final StatusType status; + private static final String message = "동일한 유저가 요청한 기록이 있습니다."; + + public SameUserRequestException() { + super(message); + this.status = StatusType.BAD_REQUEST; + } + + @Override + public StatusType getStatus() { + return status; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java index 6fc4541d..b0b38a9c 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/repository/ApplyRepositoryTest.java @@ -4,6 +4,7 @@ import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData; import kr.co.studyhubinu.studyhubserver.apply.dto.request.UpdateApplyRequest; import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection; +import kr.co.studyhubinu.studyhubserver.exception.apply.ApplyNotFoundException; import kr.co.studyhubinu.studyhubserver.study.StudyRepository; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; import kr.co.studyhubinu.studyhubserver.support.fixture.StudyEntityFixture; @@ -21,6 +22,7 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; @@ -83,13 +85,13 @@ class ApplyRepositoryTest { applyRepository.save(applyEntity); applyRepository.flush(); - ApplyEntity apply = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); - apply.update(updateApplyRequest.getInspection()); + Optional apply = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); + apply.get().update(updateApplyRequest.getInspection()); applyRepository.flush(); - ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); + Optional result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); // then - assertThat(result.getInspection()).isEqualTo(Inspection.STANDBY); + assertThat(result.get().getInspection()).isEqualTo(Inspection.STANDBY); } @Test @@ -109,7 +111,7 @@ class ApplyRepositoryTest { userRepository.flush(); // when - ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()); + ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).orElseThrow(ApplyNotFoundException::new); // then assertAll( diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java index 525738bf..a608cf38 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyServiceTest.java @@ -9,6 +9,8 @@ import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse; import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection; import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository; +import kr.co.studyhubinu.studyhubserver.exception.apply.SameUserRequestException; +import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException; import kr.co.studyhubinu.studyhubserver.study.StudyRepository; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity; @@ -26,6 +28,7 @@ import java.util.List; import java.util.Optional; +import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -53,18 +56,18 @@ class ApplyServiceTest { EnrollApplyRequest request = EnrollApplyRequest.builder() .studyId(1L) .build(); - Optional user = Optional.ofNullable(UserEntity.builder().id(1L).build()); - Optional study = Optional.ofNullable(StudyEntity.builder().id(1L).build()); + UserEntity user = UserEntity.builder().id(1L).build(); + StudyEntity study = StudyEntity.builder().id(1L).build(); - when(userRepository.findById(anyLong())).thenReturn(user); - when(studyRepository.findById(anyLong())).thenReturn(study); + when(userRepository.findById(anyLong())).thenReturn(Optional.ofNullable(user)); + when(studyRepository.findById(anyLong())).thenReturn(Optional.ofNullable(study)); when(applyRepository.save(any())).thenReturn(ApplyEntity.builder() - .userId(user.get().getId()) - .study(study.get().getId()) + .userId(user.getId()) + .study(study.getId()) .build()); // when, then - applyService.enroll(user.get().getId(), request); + applyService.enroll(user.getId(), request); } @Test @@ -80,7 +83,7 @@ class ApplyServiceTest { Optional study = Optional.ofNullable(StudyEntity.builder().id(1L).build()); when(userRepository.findById(anyLong())).thenReturn(user); when(studyRepository.findById(anyLong())).thenReturn(study); - when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(ApplyEntity.builder().build()); + when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(Optional.ofNullable(ApplyEntity.builder().build())); // when, then applyService.update(request);