From bde72e595378bfdd84b20c0a628f430efc5c4771 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Wed, 20 Sep 2023 12:30:25 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20Post=20=EC=A0=84=EC=B2=B4=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20api=20QueryString=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95=20(#379)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/application/PostService.java | 15 ++-- .../tripdraw/post/dto/PostSearchRequest.java | 32 +++++++- .../post/presentation/PostController.java | 3 +- .../post/application/PostServiceTest.java | 35 ++++----- .../post/presentation/PostControllerTest.java | 75 +++++++++---------- 5 files changed, 92 insertions(+), 68 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 0d004002f..ed9bf309f 100644 --- a/backend/src/main/java/dev/tripdraw/post/application/PostService.java +++ b/backend/src/main/java/dev/tripdraw/post/application/PostService.java @@ -15,6 +15,7 @@ import dev.tripdraw.post.dto.PostCreateResponse; import dev.tripdraw.post.dto.PostRequest; import dev.tripdraw.post.dto.PostResponse; +import dev.tripdraw.post.dto.PostSearchPaging; import dev.tripdraw.post.dto.PostSearchRequest; import dev.tripdraw.post.dto.PostSearchResponse; import dev.tripdraw.post.dto.PostUpdateRequest; @@ -24,14 +25,13 @@ import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.Trip; import dev.tripdraw.trip.domain.TripRepository; +import java.util.List; 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 @@ -129,15 +129,20 @@ public void delete(LoginUser loginUser, Long postId) { } public PostsSearchResponse readAll(PostSearchRequest postSearchRequest) { - List posts = postQueryService.findAllByConditions(postSearchRequest.conditions(), postSearchRequest.paging()); + PostSearchPaging postSearchPaging = postSearchRequest.toPostSearchPaging(); + + List posts = postQueryService.findAllByConditions( + postSearchRequest.toPostSearchConditions(), + postSearchPaging + ); List postSearchResponses = posts.stream() .map(PostSearchResponse::from) .toList(); - boolean hasNextPage = (posts.size() == postSearchRequest.paging().limit() + 1); + boolean hasNextPage = (posts.size() == postSearchPaging.limit() + 1); if (hasNextPage) { - postSearchResponses = postSearchResponses.subList(0, postSearchRequest.paging().limit()); + postSearchResponses = postSearchResponses.subList(0, postSearchPaging.limit()); } return PostsSearchResponse.of(postSearchResponses, hasNextPage); diff --git a/backend/src/main/java/dev/tripdraw/post/dto/PostSearchRequest.java b/backend/src/main/java/dev/tripdraw/post/dto/PostSearchRequest.java index 5e8edab4c..1426fba3d 100644 --- a/backend/src/main/java/dev/tripdraw/post/dto/PostSearchRequest.java +++ b/backend/src/main/java/dev/tripdraw/post/dto/PostSearchRequest.java @@ -1,4 +1,34 @@ package dev.tripdraw.post.dto; -public record PostSearchRequest(PostSearchConditions conditions, PostSearchPaging paging) { +import java.util.Set; +import lombok.Builder; + +@Builder +public record PostSearchRequest( + Set years, + Set months, + Set daysOfWeek, + Set hours, + Set ageRanges, + Set genders, + String address, + Long lastViewedId, + Integer limit +) { + + public PostSearchConditions toPostSearchConditions() { + return PostSearchConditions.builder() + .years(years) + .months(months) + .daysOfWeek(daysOfWeek) + .hours(hours) + .ageRanges(ageRanges) + .genders(genders) + .address(address) + .build(); + } + + public PostSearchPaging toPostSearchPaging() { + return new PostSearchPaging(lastViewedId, limit); + } } 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 c563d3394..a84173594 100644 --- a/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java +++ b/backend/src/main/java/dev/tripdraw/post/presentation/PostController.java @@ -29,7 +29,6 @@ import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; @@ -132,7 +131,7 @@ public ResponseEntity readAllPostsOfTrip( @GetMapping("/posts") public ResponseEntity readAllPosts( @Auth LoginUser loginUser, - @RequestBody PostSearchRequest postSearchRequest + PostSearchRequest postSearchRequest ) { PostsSearchResponse response = postService.readAll(postSearchRequest); 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 740e7e5e8..2ff00c5b8 100644 --- a/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java +++ b/backend/src/test/java/dev/tripdraw/post/application/PostServiceTest.java @@ -25,8 +25,6 @@ import dev.tripdraw.post.dto.PostCreateResponse; import dev.tripdraw.post.dto.PostRequest; import dev.tripdraw.post.dto.PostResponse; -import dev.tripdraw.post.dto.PostSearchConditions; -import dev.tripdraw.post.dto.PostSearchPaging; import dev.tripdraw.post.dto.PostSearchRequest; import dev.tripdraw.post.dto.PostSearchResponse; import dev.tripdraw.post.dto.PostUpdateRequest; @@ -39,6 +37,9 @@ 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; @@ -47,10 +48,6 @@ 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 @@ -325,27 +322,25 @@ void setUp() { PostCreateResponse jejuJuly = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.of(2023, 7, 12, 15, 30)); PostCreateResponse seoulJuly = createPost("서울특별시 송파구 문정동", LocalDateTime.of(2023, 7, 12, 15, 30)); - PostSearchRequest postSearchRequestJeju = new PostSearchRequest( - PostSearchConditions.builder() - .address("제주특별자치도 제주시 애월읍") - .build(), - new PostSearchPaging(null, 10) - ); + PostSearchRequest postSearchRequestJeju = PostSearchRequest.builder() + .address("제주특별자치도 제주시 애월읍") + .limit(10) + .build(); - PostSearchRequest postSearchRequestJuly = new PostSearchRequest( - PostSearchConditions.builder() - .months(Set.of(7)) - .build(), - new PostSearchPaging(null, 10) - ); + PostSearchRequest postSearchRequestJuly = PostSearchRequest.builder() + .months(Set.of(7)) + .limit(10) + .build(); // when PostsSearchResponse postsSearchJejuResponse = postService.readAll(postSearchRequestJeju); PostsSearchResponse postsSearchJulyResponse = postService.readAll(postSearchRequestJuly); // then - assertThat(postsSearchJejuResponse.posts().stream().map(PostSearchResponse::postId).toList()).containsExactly(jejuJuly.postId(), jejuMay.postId()); - assertThat(postsSearchJulyResponse.posts().stream().map(PostSearchResponse::postId).toList()).containsExactly(seoulJuly.postId(), jejuJuly.postId()); + assertThat(postsSearchJejuResponse.posts().stream().map(PostSearchResponse::postId).toList()).containsExactly( + jejuJuly.postId(), jejuMay.postId()); + assertThat(postsSearchJulyResponse.posts().stream().map(PostSearchResponse::postId).toList()).containsExactly( + seoulJuly.postId(), jejuJuly.postId()); } diff --git a/backend/src/test/java/dev/tripdraw/post/presentation/PostControllerTest.java b/backend/src/test/java/dev/tripdraw/post/presentation/PostControllerTest.java index c1f2f03f9..f28b976d3 100644 --- a/backend/src/test/java/dev/tripdraw/post/presentation/PostControllerTest.java +++ b/backend/src/test/java/dev/tripdraw/post/presentation/PostControllerTest.java @@ -20,9 +20,6 @@ import dev.tripdraw.post.dto.PostCreateResponse; import dev.tripdraw.post.dto.PostRequest; import dev.tripdraw.post.dto.PostResponse; -import dev.tripdraw.post.dto.PostSearchConditions; -import dev.tripdraw.post.dto.PostSearchPaging; -import dev.tripdraw.post.dto.PostSearchRequest; import dev.tripdraw.post.dto.PostUpdateRequest; import dev.tripdraw.post.dto.PostsResponse; import dev.tripdraw.post.dto.PostsSearchResponse; @@ -36,6 +33,9 @@ import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; import io.restassured.specification.MultiPartSpecification; +import java.time.LocalDateTime; +import java.util.Map; +import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -43,9 +43,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import java.time.LocalDateTime; -import java.util.Set; - @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class PostControllerTest extends ControllerTest { @@ -594,56 +591,51 @@ public void setUp() { @Test void 다른_사용자들의_감상을_조회한다() { // given - PostCreateResponse jejuJuly20hourPostResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.of(2023, 7, 18, 20, 24)); - PostCreateResponse jejuAugust17hourPostResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.of(2023, 8, 18, 17, 24)); - PostCreateResponse jejuSeptember17hourPostResponse = createPost("제주특별자치도 제주시 애월읍", LocalDateTime.of(2023, 9, 18, 17, 24)); - PostCreateResponse seoulSeptember17hourPostResponse = createPost("서울특별시 송파구 잠실동", LocalDateTime.of(2023, 9, 18, 17, 24)); - - PostSearchRequest jejuRequest = new PostSearchRequest( - PostSearchConditions.builder() - .address("제주특별자치도 제주시 애월읍") - .build(), - new PostSearchPaging(null, 10) + PostCreateResponse jejuJuly20hourPostResponse = createPost("제주특별자치도 제주시 애월읍", + LocalDateTime.of(2023, 7, 18, 20, 24)); + PostCreateResponse jejuAugust17hourPostResponse = createPost("제주특별자치도 제주시 애월읍", + LocalDateTime.of(2023, 8, 18, 17, 24)); + PostCreateResponse jejuSeptember17hourPostResponse = createPost("제주특별자치도 제주시 애월읍", + LocalDateTime.of(2023, 9, 18, 17, 24)); + PostCreateResponse seoulSeptember17hourPostResponse = createPost("서울특별시 송파구 잠실동", + LocalDateTime.of(2023, 9, 18, 17, 24)); + + Map jejuParams = Map.of( + "address", "제주특별자치도 제주시 애월읍", + "limit", 10 ); - PostSearchRequest jeju17hourRequest = new PostSearchRequest( - PostSearchConditions.builder() - .hours(Set.of(17)) - .address("제주특별자치도 제주시 애월읍") - .build(), - new PostSearchPaging(null, 10) + Map jejuHour17Params = Map.of( + "hours", Set.of(17), + "address", "제주특별자치도 제주시 애월읍", + "limit", 10 ); - PostSearchRequest hour17Request = new PostSearchRequest( - PostSearchConditions.builder() - .hours(Set.of(17)) - .build(), - new PostSearchPaging(null, 10) + Map hour17Params = Map.of( + "hours", Set.of(17), + "limit", 10 ); // when ExtractableResponse jejuResponse = RestAssured.given().log().all() - .contentType(APPLICATION_JSON_VALUE) .auth().preemptive().oauth2(huchuToken) - .body(jejuRequest) + .params(jejuParams) .when().get("/posts") .then().log().all() .statusCode(OK.value()) .extract(); - ExtractableResponse jeju17hourResponse = RestAssured.given().log().all() - .contentType(APPLICATION_JSON_VALUE) + ExtractableResponse jejuhour17Response = RestAssured.given().log().all() .auth().preemptive().oauth2(huchuToken) - .body(jeju17hourRequest) + .params(jejuHour17Params) .when().get("/posts") .then().log().all() .statusCode(OK.value()) .extract(); ExtractableResponse hour17Response = RestAssured.given().log().all() - .contentType(APPLICATION_JSON_VALUE) .auth().preemptive().oauth2(huchuToken) - .body(hour17Request) + .params(hour17Params) .when().get("/posts") .then().log().all() .statusCode(OK.value()) @@ -651,19 +643,22 @@ public void setUp() { // then PostsSearchResponse jejuPostsSearchResponse = jejuResponse.as(PostsSearchResponse.class); - PostsSearchResponse jeju17hourPostsSearchResponse = jeju17hourResponse.as(PostsSearchResponse.class); + PostsSearchResponse jeju17hourPostsSearchResponse = jejuhour17Response.as(PostsSearchResponse.class); PostsSearchResponse hour17PostsSearchResponse = hour17Response.as(PostsSearchResponse.class); assertThat(jejuPostsSearchResponse.posts().get(0).postId()).isEqualTo(jejuSeptember17hourPostResponse.postId()); assertThat(jejuPostsSearchResponse.posts().get(1).postId()).isEqualTo(jejuAugust17hourPostResponse.postId()); assertThat(jejuPostsSearchResponse.posts().get(2).postId()).isEqualTo(jejuJuly20hourPostResponse.postId()); + assertThat(jeju17hourPostsSearchResponse.posts().get(0).postId()).isEqualTo( + jejuSeptember17hourPostResponse.postId()); + assertThat(jeju17hourPostsSearchResponse.posts().get(1).postId()).isEqualTo( + jejuAugust17hourPostResponse.postId()); - assertThat(jeju17hourPostsSearchResponse.posts().get(0).postId()).isEqualTo(jejuSeptember17hourPostResponse.postId()); - assertThat(jeju17hourPostsSearchResponse.posts().get(1).postId()).isEqualTo(jejuAugust17hourPostResponse.postId()); - - assertThat(hour17PostsSearchResponse.posts().get(0).postId()).isEqualTo(seoulSeptember17hourPostResponse.postId()); - assertThat(hour17PostsSearchResponse.posts().get(1).postId()).isEqualTo(jejuSeptember17hourPostResponse.postId()); + assertThat(hour17PostsSearchResponse.posts().get(0).postId()).isEqualTo( + seoulSeptember17hourPostResponse.postId()); + assertThat(hour17PostsSearchResponse.posts().get(1).postId()).isEqualTo( + jejuSeptember17hourPostResponse.postId()); assertThat(hour17PostsSearchResponse.posts().get(2).postId()).isEqualTo(jejuAugust17hourPostResponse.postId()); }