Skip to content

Commit

Permalink
[Feat] : 신청 수락 시 게시글 내 잔여석 -1 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
elyudwo committed Jan 26, 2024
1 parent a2fc4c3 commit 6c04640
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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());
}

Expand All @@ -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> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ public boolean isPostOfUser(Long userId) {
public void close() {
this.close = true;
}

public void minusRemainingSeat() {
this.remainingSeat--;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,9 @@ class ApplyServiceTest {
@Mock
ApplyRepository applyRepository;

@Mock
StudyPostRepository studyPostRepository;

@Test
void 스터디_가입신청() {
// given
Expand All @@ -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<UserEntity> user = Optional.ofNullable(UserEntity.builder().id(1L).build());
Optional<StudyEntity> 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<UserEntity> user = Optional.ofNullable(UserEntity.builder().id(1L).build());
// Optional<StudyEntity> 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 스터디_요청상태_조회() {
Expand Down

0 comments on commit 6c04640

Please sign in to comment.