From 3968a2f8dacf957a26a6bb1eacf75f7ac4fc9586 Mon Sep 17 00:00:00 2001 From: JunH Date: Sat, 12 Aug 2023 01:41:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20(#321)=20=EC=88=98=EC=A0=95=ED=95=A0=20?= =?UTF-8?q?=EB=A7=88=EA=B0=90=EA=B8=B0=ED=95=9C=EC=9D=B4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EA=B8=B0=ED=95=9C=EB=B3=B4=EB=8B=A4=203=EC=9D=BC=20?= =?UTF-8?q?=EC=B4=88=EA=B3=BC=ED=95=A0=20=EA=B2=BD=EC=9A=B0=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../votogether/domain/post/entity/Post.java | 6 ++ .../post/exception/PostExceptionType.java | 2 +- .../domain/post/service/PostService.java | 1 + .../domain/post/service/PostServiceTest.java | 93 +++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/votogether/domain/post/entity/Post.java b/backend/src/main/java/com/votogether/domain/post/entity/Post.java index 874e4f678..1e504f9eb 100644 --- a/backend/src/main/java/com/votogether/domain/post/entity/Post.java +++ b/backend/src/main/java/com/votogether/domain/post/entity/Post.java @@ -283,4 +283,10 @@ private String getPostOptionImageUrl( return postOptionImageUrls.get(postOptionIndex); } + public void validateDeadLineToModify(final LocalDateTime deadlineToModify) { + if (getCreatedAt().plusDays(3).isBefore(deadlineToModify)) { + throw new BadRequestException(PostExceptionType.DEADLINE_EXCEED_THREE_DAYS); + } + } + } diff --git a/backend/src/main/java/com/votogether/domain/post/exception/PostExceptionType.java b/backend/src/main/java/com/votogether/domain/post/exception/PostExceptionType.java index 14895fac4..10fc47b77 100644 --- a/backend/src/main/java/com/votogether/domain/post/exception/PostExceptionType.java +++ b/backend/src/main/java/com/votogether/domain/post/exception/PostExceptionType.java @@ -13,7 +13,7 @@ public enum PostExceptionType implements ExceptionType { POST_CLOSED(1004, "게시글이 이미 마감되었습니다."), POST_NOT_HALF_DEADLINE(1005, "게시글이 마감 시간까지 절반의 시간 이상이 지나지 않으면 조기마감을 할 수 없습니다."), NOT_VOTER(1004, "해당 게시글 작성자는 투표할 수 없습니다."), - DEADLINE_EXCEED_THREE_DAYS(1005, "마감 기한은 현재 시간으로부터 3일을 초과할 수 없습니다."), + DEADLINE_EXCEED_THREE_DAYS(1005, "마감 기한은 생성 시간으로부터 3일을 초과할 수 없습니다."), WRONG_IMAGE(1006, "이미지 저장에 실패했습니다. 다시 시도해주세요."), ; diff --git a/backend/src/main/java/com/votogether/domain/post/service/PostService.java b/backend/src/main/java/com/votogether/domain/post/service/PostService.java index 45d232457..9c5d64426 100644 --- a/backend/src/main/java/com/votogether/domain/post/service/PostService.java +++ b/backend/src/main/java/com/votogether/domain/post/service/PostService.java @@ -290,6 +290,7 @@ public void update( .orElseThrow(() -> new BadRequestException(PostExceptionType.POST_NOT_FOUND)); post.validateWriter(member); post.validateDeadLine(); + post.validateDeadLineToModify(request.deadline()); post.update( diff --git a/backend/src/test/java/com/votogether/domain/post/service/PostServiceTest.java b/backend/src/test/java/com/votogether/domain/post/service/PostServiceTest.java index 86d790a0d..5d8afc2f5 100644 --- a/backend/src/test/java/com/votogether/domain/post/service/PostServiceTest.java +++ b/backend/src/test/java/com/votogether/domain/post/service/PostServiceTest.java @@ -1023,4 +1023,97 @@ void throwExceptionUpdateClosedPost() throws IOException { .hasMessage(PostExceptionType.POST_CLOSED.getMessage()); } + @Test + @DisplayName("게시글 수정 시, 수정할 마감 기한이 생성 날짜보다 3일 초과면 예외를 던진다.") + void throwExceptionUpdateDeadlineOver() throws IOException { + // given + Category category1 = categoryRepository.save(CategoryFixtures.DEVELOP.get()); + Member writer = memberRepository.save(MemberFixtures.MALE_20.get()); + + MockMultipartFile file1 = new MockMultipartFile( + "image1", + "test1.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage1.PNG") + ); + MockMultipartFile file2 = new MockMultipartFile( + "image2", + "test2.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage2.PNG") + ); + + MockMultipartFile file3 = new MockMultipartFile( + "image3", + "test3.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage3.PNG") + ); + + PostCreateRequest postCreateRequest = PostCreateRequest.builder() + .categoryIds(List.of(category1.getId())) + .title("title") + .content("content") + .postOptions(List.of( + PostOptionCreateRequest.builder() + .content("option1") + .build(), + PostOptionCreateRequest.builder() + .content("option2") + .build() + )) + .deadline(LocalDateTime.now().plusDays(3)) + .build(); + + Long savedPostId = postService.save(postCreateRequest, writer, List.of(file3), List.of(file1, file2)); + + + Category category2 = categoryRepository.save(CategoryFixtures.FOOD.get()); + MockMultipartFile file4 = new MockMultipartFile( + "image4", + "test4.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage1.PNG") + ); + MockMultipartFile file5 = new MockMultipartFile( + "image5", + "test5.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage2.PNG") + ); + + MockMultipartFile file6 = new MockMultipartFile( + "image6", + "test6.png", + "image/png", + new FileInputStream("src/test/resources/images/testImage3.PNG") + ); + + PostUpdateRequest postUpdateRequest = PostUpdateRequest.builder() + .categoryIds(List.of(category2.getId())) + .title("title2") + .content("content2") + .postOptions(List.of( + PostOptionUpdateRequest.builder() + .content("option3") + .build(), + PostOptionUpdateRequest.builder() + .content("option4") + .build() + )) + .deadline(LocalDateTime.now().plusDays(4)) + .build(); + + // when, then + assertThatThrownBy(() -> postService.update( + savedPostId, + postUpdateRequest, + writer, + List.of(file4), + List.of(file5, file6) + )) + .isInstanceOf(BadRequestException.class) + .hasMessage(PostExceptionType.DEADLINE_EXCEED_THREE_DAYS.getMessage()); + } + }