Skip to content

Commit

Permalink
Merge pull request #908 from woowacourse-teams/BE/hotfix-ret
Browse files Browse the repository at this point in the history
[BE] 회고를 두번 이상 작성할 시 예외 던지도록 변경
  • Loading branch information
reddevilmidzy authored Oct 24, 2024
2 parents 511d646 + 97cf459 commit 426cfa4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum RetrospectApiError {
INVALID_RETROSPECT_INPUT_ERROR(HttpStatus.BAD_REQUEST, "잘못된 회고 입력 값입니다."),
NOT_RETROSPECT_OWNER_ACCESS_ERROR(HttpStatus.FORBIDDEN, "회고 소유자 외 접근할 수 없는 작업입니다."),
RETROSPECT_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "해당 요청의 회고가 존재하지 않습니다."),
MAX_RETROSPECT_LIMIT_REACHED_ERROR(HttpStatus.BAD_REQUEST, "회고는 최대 6개까지 추가할 수 있습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.coduo.retrospect.exception;

public class MaxRetrospectLimitException extends RetrospectException {

public MaxRetrospectLimitException(final String message) {
super(message);
}

public MaxRetrospectLimitException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.data.jpa.repository.JpaRepository;

import site.coduo.pairroom.repository.PairRoomEntity;
import site.coduo.pairroom.repository.PairRoomMemberEntity;

public interface RetrospectRepository extends JpaRepository<RetrospectEntity, Long> {
Expand All @@ -12,4 +13,5 @@ public interface RetrospectRepository extends JpaRepository<RetrospectEntity, Lo

void deleteAllByPairRoomMember(PairRoomMemberEntity pairRoomMember);

boolean existsRetrospectEntityByPairRoomMember(PairRoomMemberEntity pairRoomMember);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import site.coduo.retrospect.domain.Retrospect;
import site.coduo.retrospect.domain.RetrospectContent;
import site.coduo.retrospect.domain.RetrospectContents;
import site.coduo.retrospect.exception.MaxRetrospectLimitException;
import site.coduo.retrospect.exception.NotRetrospectOwnerAccessException;
import site.coduo.retrospect.repository.RetrospectEntity;
import site.coduo.retrospect.repository.RetrospectRepository;
Expand All @@ -42,6 +43,9 @@ public void createRetrospect(
final PairRoomEntity pairRoom = pairRoomRepository.fetchByAccessCode(pairRoomAccessCode);
final Member member = memberService.findMemberByCredential(credentialToken);
final PairRoomMemberEntity pairRoomMember = pairRoomMemberRepository.fetchByPairRoomAndMember(pairRoom, member);
if (retrospectRepository.existsRetrospectEntityByPairRoomMember(pairRoomMember)) {
throw new MaxRetrospectLimitException("회고가 이미 존재합니다.");
}
final RetrospectContents retrospectContents = RetrospectContents.from(answers);
final Retrospect retrospect = new Retrospect(retrospectContents);
final List<RetrospectEntity> retrospectContentEntities = retrospect.getContents().getValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import site.coduo.pairroom.repository.PairRoomRepository;
import site.coduo.referencelink.repository.CategoryRepository;
import site.coduo.retrospect.controller.response.FindRetrospectsResponse;
import site.coduo.retrospect.exception.MaxRetrospectLimitException;
import site.coduo.retrospect.repository.RetrospectEntity;
import site.coduo.retrospect.repository.RetrospectRepository;
import site.coduo.timer.repository.TimerRepository;
Expand Down Expand Up @@ -104,6 +105,40 @@ void createRetrospect() {
assertThat(allByPairRoomMember).isNotEmpty();
}

@DisplayName("회고를 두 번 이상 저장하면 예외가 발생한다.")
@Test
void createTwiceRetrospect() {
// Given
final Member savedMember = memberRepository.save(
Member.builder()
.userId("userid")
.accessToken("access")
.loginId("login")
.username("username")
.profileImage("some image")
.build()
);
final PairRoomEntity savedPairRoom = pairRoomRepository.save(PairRoomEntity.from(
new PairRoom(PairRoomStatus.IN_PROGRESS,
new Pair(new PairName("레디"), new PairName("파슬리")),
new MissionUrl("https://missionUrl.xxx"),
new AccessCode("123456"),
EASY_ACCESS_CODE_INK_REDDY)
));
pairRoomMemberRepository.save(new PairRoomMemberEntity(savedPairRoom, savedMember));

final String credentialToken = jwtProvider.sign(savedMember.getUserId());
final List<String> answers = List.of("답변1", "답변2", "답변3", "답변4", "답변5", "답변6");
retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(), answers);

// When && Then
assertThatThrownBy(
() -> retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(), answers))
.isInstanceOf(MaxRetrospectLimitException.class);

}


@DisplayName("입력된 페어룸 접근 코드에 대응되는 페어룸 정보가 존재하지 않는다면 예외를 발생시킨다.")
@Test
void notExistPairRoomByAccessCode() {
Expand Down Expand Up @@ -300,9 +335,11 @@ void notExistRetrospectWithPairRoom() {
final String credentialToken = jwtProvider.sign(savedMember.getUserId());

// When
retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(), List.of("", "", "", "", "", ""));
retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(),
List.of("", "", "", "", "", ""));

final boolean isExist = retrospectService.existRetrospectWithPairRoom(credentialToken, savedPairRoom.getAccessCode());
final boolean isExist = retrospectService.existRetrospectWithPairRoom(credentialToken,
savedPairRoom.getAccessCode());

// Then
assertThat(isExist).isFalse();
Expand Down Expand Up @@ -330,10 +367,12 @@ void oneExistRetrospectWithPairRoom() {
pairRoomMemberRepository.save(new PairRoomMemberEntity(savedPairRoom, savedMember));
final String credentialToken = jwtProvider.sign(savedMember.getUserId());

retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(), List.of("답변!", "", "", "", "", ""));
retrospectService.createRetrospect(credentialToken, savedPairRoom.getAccessCode(),
List.of("답변!", "", "", "", "", ""));

// When
final boolean isExist = retrospectService.existRetrospectWithPairRoom(credentialToken, savedPairRoom.getAccessCode());
final boolean isExist = retrospectService.existRetrospectWithPairRoom(credentialToken,
savedPairRoom.getAccessCode());

// Then
assertThat(isExist).isTrue();
Expand Down

0 comments on commit 426cfa4

Please sign in to comment.