From 1fce51a272097a208ba3014f32a05e7e1d7d0926 Mon Sep 17 00:00:00 2001 From: ReO Date: Thu, 21 Sep 2023 04:43:09 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EA=B0=90=EC=83=81=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=EC=8B=9C=20=EB=8B=A4=EB=A5=B8=20=EC=9C=A0=EC=A0=80?= =?UTF-8?q?=EB=8F=84=20=EA=B0=90=EC=83=81=EC=9D=84=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20(#388)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/application/PostService.java | 18 ++--- .../post/presentation/PostController.java | 4 +- .../post/application/PostServiceTest.java | 66 +++---------------- 3 files changed, 22 insertions(+), 66 deletions(-) diff --git a/backend/src/main/java/dev/tripdraw/post/application/PostService.java b/backend/src/main/java/dev/tripdraw/post/application/PostService.java index ed9bf309..08557ab5 100644 --- a/backend/src/main/java/dev/tripdraw/post/application/PostService.java +++ b/backend/src/main/java/dev/tripdraw/post/application/PostService.java @@ -1,5 +1,6 @@ package dev.tripdraw.post.application; +import static dev.tripdraw.trip.exception.TripExceptionType.TRIP_NOT_FOUND; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; @@ -25,13 +26,15 @@ import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.Trip; import dev.tripdraw.trip.domain.TripRepository; -import java.util.List; +import dev.tripdraw.trip.exception.TripException; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.util.List; + @RequiredArgsConstructor @Transactional @Service @@ -91,18 +94,16 @@ public PostCreateResponse addAtExistingLocation( } @Transactional(readOnly = true) - public PostResponse read(LoginUser loginUser, Long postId) { + public PostResponse read(Long postId) { Post post = postRepository.getByPostId(postId); - Member member = memberRepository.getById(loginUser.memberId()); - post.validateAuthorization(member); return PostResponse.from(post); } @Transactional(readOnly = true) - public PostsResponse readAllByTripId(LoginUser loginUser, Long tripId) { - Member member = memberRepository.getById(loginUser.memberId()); - Trip trip = tripRepository.getById(tripId); - trip.validateAuthorization(member); + public PostsResponse readAllByTripId(Long tripId) { + if (!tripRepository.existsById(tripId)) { + throw new TripException(TRIP_NOT_FOUND); + } return postRepository.findAllByTripId(tripId).stream() .sorted(comparing(Post::pointRecordedAt).reversed()) @@ -128,6 +129,7 @@ public void delete(LoginUser loginUser, Long postId) { postRepository.deleteById(postId); } + @Transactional(readOnly = true) public PostsSearchResponse readAll(PostSearchRequest postSearchRequest) { PostSearchPaging postSearchPaging = postSearchRequest.toPostSearchPaging(); diff --git a/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java b/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java index a8417359..263ae2c3 100644 --- a/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java +++ b/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java @@ -105,7 +105,7 @@ public ResponseEntity read( @Auth LoginUser loginUser, @PathVariable Long postId ) { - PostResponse response = postService.read(loginUser, postId); + PostResponse response = postService.read(postId); return ResponseEntity.ok(response); } @@ -119,7 +119,7 @@ public ResponseEntity readAllPostsOfTrip( @Auth LoginUser loginUser, @PathVariable Long tripId ) { - PostsResponse response = postService.readAllByTripId(loginUser, tripId); + PostsResponse response = postService.readAllByTripId(tripId); return ResponseEntity.ok(response); } diff --git a/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java b/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java index 2ff00c5b..99064cd4 100644 --- a/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java +++ b/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java @@ -4,7 +4,6 @@ import static dev.tripdraw.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; import static dev.tripdraw.post.exception.PostExceptionType.NOT_AUTHORIZED_TO_POST; import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND; -import static dev.tripdraw.trip.exception.TripExceptionType.NOT_AUTHORIZED_TO_TRIP; import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND; import static dev.tripdraw.trip.exception.TripExceptionType.TRIP_NOT_FOUND; import static org.assertj.core.api.Assertions.assertThat; @@ -37,9 +36,6 @@ import dev.tripdraw.trip.domain.Trip; import dev.tripdraw.trip.domain.TripRepository; import dev.tripdraw.trip.exception.TripException; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -48,6 +44,10 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.web.multipart.MultipartFile; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Set; + @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @ServiceTest @@ -224,7 +224,7 @@ void setUp() { PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now()); // when - PostResponse postResponse = postService.read(loginUser, postCreateResponse.postId()); + PostResponse postResponse = postService.read(postCreateResponse.postId()); // then assertSoftly(softly -> { @@ -237,36 +237,11 @@ void setUp() { @Test void 특정_감상을_조회할_때_존재하지_않는_감상_ID이면_예외를_발생시킨다() { // expect - assertThatThrownBy(() -> postService.read(loginUser, Long.MIN_VALUE)) + assertThatThrownBy(() -> postService.read(Long.MIN_VALUE)) .isInstanceOf(PostException.class) .hasMessage(POST_NOT_FOUND.message()); } - @Test - void 특정_감상을_조회할_때_존재하지_않는_사용자_닉네임이면_예외를_발생시킨다() { - // given - PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now()); - LoginUser wrongUser = new LoginUser(Long.MIN_VALUE); - - // expect - Long postId = postCreateResponse.postId(); - assertThatThrownBy(() -> postService.read(wrongUser, postId)) - .isInstanceOf(MemberException.class) - .hasMessage(MEMBER_NOT_FOUND.message()); - } - - @Test - void 특정_감상을_조회할_때_로그인_한_사용자가_감상의_작성자가_아니면_예외가_발생한다() { - // given - PostCreateResponse postCreateResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now()); - - // expect - Long postId = postCreateResponse.postId(); - assertThatThrownBy(() -> postService.read(otherUser, postId)) - .isInstanceOf(PostException.class) - .hasMessage(NOT_AUTHORIZED_TO_POST.message()); - } - @Test void 특정_여행의_모든_감상을_조회한다() { // given @@ -274,7 +249,7 @@ void setUp() { createPost("제주특별자치도 제주시 애월읍", LocalDateTime.now()); // when - PostsResponse postsResponse = postService.readAllByTripId(loginUser, trip.id()); + PostsResponse postsResponse = postService.readAllByTripId(trip.id()); // then List posts = postsResponse.posts(); @@ -286,35 +261,14 @@ void setUp() { }); } - @Test - void 특정_여행의_모든_감상을_조회할_때_존재하지_않는_사용자_닉네임이면_예외를_발생시킨다() { - // given - LoginUser wrongUser = new LoginUser(Long.MIN_VALUE); - - // expect - Long tripId = trip.id(); - assertThatThrownBy(() -> postService.readAllByTripId(wrongUser, tripId)) - .isInstanceOf(MemberException.class) - .hasMessage(MEMBER_NOT_FOUND.message()); - } - @Test void 특정_여행의_모든_감상을_조회할_때_존재하지_않는_여행_ID이면_예외가_발생한다() { // expect - assertThatThrownBy(() -> postService.readAllByTripId(loginUser, Long.MIN_VALUE)) + assertThatThrownBy(() -> postService.readAllByTripId(Long.MIN_VALUE)) .isInstanceOf(TripException.class) .hasMessage(TRIP_NOT_FOUND.message()); } - @Test - void 특정_여행의_모든_감상을_조회할_때_로그인_한_사용자가_여행의_주인이_아니면_예외가_발생한다() { - // expect - Long tripId = trip.id(); - assertThatThrownBy(() -> postService.readAllByTripId(otherUser, tripId)) - .isInstanceOf(TripException.class) - .hasMessage(NOT_AUTHORIZED_TO_TRIP.message()); - } - @Test void 조건에_해당하는_모든_여행을_조회한다() { // given @@ -357,7 +311,7 @@ void setUp() { postService.update(loginUser, postCreateResponse.postId(), postUpdateRequest, null); // then - PostResponse postResponseBeforeUpdate = postService.read(loginUser, postCreateResponse.postId()); + PostResponse postResponseBeforeUpdate = postService.read(postCreateResponse.postId()); assertSoftly(softly -> { softly.assertThat(postResponseBeforeUpdate.postId()).isEqualTo(postCreateResponse.postId()); @@ -422,7 +376,7 @@ void setUp() { Long postId = postCreateResponse.postId(); assertDoesNotThrow(() -> postService.delete(loginUser, postId)); - assertThatThrownBy(() -> postService.read(loginUser, postId)) + assertThatThrownBy(() -> postService.read(postId)) .isInstanceOf(PostException.class) .hasMessage(POST_NOT_FOUND.message()); }