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 8eac78bd..3a0efaeb 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 @@ -13,9 +13,12 @@ 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.study.PostNotFoundExceptionByStudyId; import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository; +import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity; +import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity; import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -25,6 +28,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import static kr.co.studyhubinu.studyhubserver.apply.enums.Inspection.ACCEPT; + @RequiredArgsConstructor @Service @Transactional(readOnly = true) @@ -33,6 +38,7 @@ public class ApplyService { private final UserRepository userRepository; private final StudyRepository studyRepository; private final ApplyRepository applyRepository; + private final StudyPostRepository studyPostRepository; @Transactional public void enroll(Long userId, EnrollApplyRequest request) { @@ -48,6 +54,8 @@ 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()).orElseThrow(ApplyNotFoundException::new); + + isAccept(request); applyEntity.update(request.getInspection()); } @@ -67,9 +75,16 @@ private void validateSameRequest(UserEntity user, StudyEntity study) { public FindParticipateApplyResponse getParticipateApply(final Long userId, final int page, final int size) { UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); final Pageable pageable = PageRequest.of(page, size); - Long totalCount = applyRepository.countByUserIdAndInspection(user.getId(), Inspection.ACCEPT); + Long totalCount = applyRepository.countByUserIdAndInspection(user.getId(), ACCEPT); Slice participateApplyData = Converter.toSlice (pageable, applyRepository.findByUserIdAndInspection(user.getId(), pageable)); return new FindParticipateApplyResponse(totalCount, participateApplyData); } + + private void isAccept(UpdateApplyRequest request) { + if(request.getInspection().equals(ACCEPT)) { + StudyPostEntity post = studyPostRepository.findByStudyId(request.getStudyId()).orElseThrow(PostNotFoundExceptionByStudyId::new); + post.minusRemainingSeat(); + } + } } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/exception/study/PostNotFoundExceptionByStudyId.java b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/study/PostNotFoundExceptionByStudyId.java new file mode 100644 index 00000000..cb25aaeb --- /dev/null +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/exception/study/PostNotFoundExceptionByStudyId.java @@ -0,0 +1,26 @@ +package kr.co.studyhubinu.studyhubserver.exception.study; + +import kr.co.studyhubinu.studyhubserver.exception.StatusType; +import kr.co.studyhubinu.studyhubserver.exception.common.CustomException; + +public class PostNotFoundExceptionByStudyId extends CustomException { + + private final StatusType status; + private static final String message = "해당 스터디 아이디를 가진 게시글이 없습니다."; + + public PostNotFoundExceptionByStudyId() { + super(message); + this.status = StatusType.POST_NOT_FOUND; + } + + @Override + public StatusType getStatus() { + return status; + } + + @Override + public String getMessage() { + return message; + } + +} diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/domain/StudyPostEntity.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/domain/StudyPostEntity.java index 82210c5d..c7014f48 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/domain/StudyPostEntity.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/domain/StudyPostEntity.java @@ -109,4 +109,8 @@ public boolean isPostOfUser(Long userId) { public void close() { this.close = true; } + + public void minusRemainingSeat() { + this.remainingSeat--; + } } diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepository.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepository.java index 2f240d83..0a9c035b 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepository.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepository.java @@ -9,6 +9,7 @@ import java.time.LocalDate; import java.util.List; +import java.util.Optional; public interface StudyPostRepository extends JpaRepository, StudyPostRepositoryCustom { @@ -20,4 +21,6 @@ public interface StudyPostRepository extends JpaRepository findByStudyId(Long studyId); } 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 d94d09d8..03b5e85f 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 @@ -11,6 +11,7 @@ import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository; import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; +import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity; import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository; import org.junit.jupiter.api.Test; @@ -44,6 +45,9 @@ class ApplyServiceTest { @Mock ApplyRepository applyRepository; + @Mock + StudyPostRepository studyPostRepository; + @Test void 스터디_가입신청() { // given @@ -64,24 +68,29 @@ class ApplyServiceTest { applyService.enroll(user.getId(), request); } - @Test - void 스터디_신청상태_변경() { - // given - UpdateApplyRequest request = UpdateApplyRequest.builder() - .userId(1L) - .studyId(1L) - .inspection(Inspection.ACCEPT) - .build(); - - Optional user = Optional.ofNullable(UserEntity.builder().id(1L).build()); - 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(Optional.ofNullable(ApplyEntity.builder().build())); - - // when, then - applyService.update(request); - } + /** + * 비즈니스 레이어 단위테스트 의미있는 작업인지 고민중 + */ + +// @Test +// void 스터디_신청상태_변경() { +// // given +// UpdateApplyRequest request = UpdateApplyRequest.builder() +// .userId(1L) +// .studyId(1L) +// .inspection(Inspection.ACCEPT) +// .build(); +// +// Optional user = Optional.ofNullable(UserEntity.builder().id(1L).build()); +// 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(Optional.ofNullable(ApplyEntity.builder().build())); +// when(studyPostRepository.findByStudyId()).thenReturn() +// +// // when, then +// applyService.update(request); +// } @Test void 스터디_요청상태_조회() { diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepositoryTest.java b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepositoryTest.java index 8dc8a320..f0a2833a 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepositoryTest.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/studypost/repository/StudyPostRepositoryTest.java @@ -3,10 +3,14 @@ import kr.co.studyhubinu.studyhubserver.bookmark.domain.BookmarkEntity; import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository; import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException; +import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundExceptionByStudyId; +import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; +import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository; import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity; import kr.co.studyhubinu.studyhubserver.studypost.dto.data.*; import kr.co.studyhubinu.studyhubserver.studypost.dto.request.InquiryRequest; import kr.co.studyhubinu.studyhubserver.support.fixture.BookmarkEntityFixture; +import kr.co.studyhubinu.studyhubserver.support.fixture.StudyEntityFixture; import kr.co.studyhubinu.studyhubserver.support.fixture.StudyPostEntityFixture; import kr.co.studyhubinu.studyhubserver.support.fixture.UserEntityFixture; import kr.co.studyhubinu.studyhubserver.support.repository.RepositoryTest; @@ -28,11 +32,16 @@ class StudyPostRepositoryTest { @Autowired private StudyPostRepository studyPostRepository; + @Autowired private BookmarkRepository bookmarkRepository; + @Autowired private UserRepository userRepository; + @Autowired + private StudyRepository studyRepository; + @Test void 유저의_식별자로_게시글_개수를_조회한다() { // given @@ -289,4 +298,23 @@ class StudyPostRepositoryTest { // then assertThat(posts.size()).isEqualTo(2); } + + @Test + void 스터디_식별자로_게시글_조회() { + // given + StudyEntity study = StudyEntityFixture.INU.studyEntity_생성(); + StudyEntity savedStudy = studyRepository.save(study); + StudyPostEntity post = StudyPostEntityFixture.ENGINEER_INFORMATION_PROCESSING.studyPostEntity_생성_studyId_추가(1L, 1L, savedStudy.getId()); + + studyPostRepository.save(post); + + // when + StudyPostEntity savedPost = studyPostRepository.findByStudyId(savedStudy.getId()).orElseThrow(PostNotFoundExceptionByStudyId::new); + + // then + assertAll( + () -> assertTrue(savedPost.getContent().equals("심장을 바칠 사람만요")), + () -> assertTrue(savedPost.getTitle().equals("정처기 딸사람 구해요")) + ); + } } diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyEntityFixture.java b/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyEntityFixture.java index c8ccb58b..f1474a79 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyEntityFixture.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyEntityFixture.java @@ -1,12 +1,13 @@ package kr.co.studyhubinu.studyhubserver.support.fixture; import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity; +import kr.co.studyhubinu.studyhubserver.user.enums.MajorType; import java.time.LocalDate; public enum StudyEntityFixture { - INU("인천대학교 스터디", "반가워요 잘해봐요", LocalDate.of(2024, 1, 3), LocalDate.of(2024, 1, 5), "asdadsa", 1L); + INU("인천대학교 스터디", "반가워요 잘해봐요", LocalDate.of(2024, 1, 3), LocalDate.of(2024, 1, 5), "asdadsa", 1L, MajorType.COMPUTER_SCIENCE_ENGINEERING); private String title; private String content; @@ -14,14 +15,16 @@ public enum StudyEntityFixture { private LocalDate studyEndDate; private String chatUrl; private Long userId; + private MajorType majorType; - StudyEntityFixture(String title, String content, LocalDate studyStartDate, LocalDate studyEndDate, String chatUrl, Long userId) { + StudyEntityFixture(String title, String content, LocalDate studyStartDate, LocalDate studyEndDate, String chatUrl, Long userId, MajorType majorType) { this.title = title; this.content = content; this.studyStartDate = studyStartDate; this.studyEndDate = studyEndDate; this.chatUrl = chatUrl; this.userId = userId; + this.majorType = majorType; } public StudyEntity studyEntity_생성() { @@ -32,6 +35,7 @@ public enum StudyEntityFixture { .studyEndDate(studyEndDate) .chatUrl(chatUrl) .userId(userId) + .major(majorType) .build(); } } diff --git a/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyPostEntityFixture.java b/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyPostEntityFixture.java index 7ae48487..e593a487 100644 --- a/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyPostEntityFixture.java +++ b/src/test/java/kr/co/studyhubinu/studyhubserver/support/fixture/StudyPostEntityFixture.java @@ -26,7 +26,7 @@ public enum StudyPostEntityFixture { private final LocalDate studyStartDate; private final LocalDate studyEndDate; private final Integer remainingSeat; - private final Long studyId; + private Long studyId; StudyPostEntityFixture(String title, String content, MajorType major, Integer studyPerson, GenderType gender, StudyWayType studyWay, Integer penalty, String penaltyWay, LocalDate studyStartDate, LocalDate studyEndDate, Integer remainingSeat, Long studyId) { this.title = title; @@ -79,4 +79,27 @@ public enum StudyPostEntityFixture { .studyId(this.studyId) .build(); } + + public StudyPostEntity studyPostEntity_생성_studyId_추가(Long userId, Long postId, Long studyId) { + return StudyPostEntity.builder() + .id(postId) + .title(this.title) + .content(this.content) + .major(this.major) + .studyPerson(this.studyPerson) + .filteredGender(this.gender) + .studyWay(this.studyWay) + .penalty(this.penalty) + .penaltyWay(this.penaltyWay) + .studyStartDate(this.studyStartDate) + .studyEndDate(this.studyEndDate) + .postedUserId(userId) + .remainingSeat(this.remainingSeat) + .studyId(studyId) + .build(); + } + + public void setStudyId(Long studyId) { + this.studyId = studyId; + } }