Skip to content

Commit

Permalink
[refactor] Post 전체 조회 api QueryString 방식으로 수정 (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Sep 20, 2023
1 parent 7006c6d commit bde72e5
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -129,15 +129,20 @@ public void delete(LoginUser loginUser, Long postId) {
}

public PostsSearchResponse readAll(PostSearchRequest postSearchRequest) {
List<Post> posts = postQueryService.findAllByConditions(postSearchRequest.conditions(), postSearchRequest.paging());
PostSearchPaging postSearchPaging = postSearchRequest.toPostSearchPaging();

List<Post> posts = postQueryService.findAllByConditions(
postSearchRequest.toPostSearchConditions(),
postSearchPaging
);

List<PostSearchResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> years,
Set<Integer> months,
Set<Integer> daysOfWeek,
Set<Integer> hours,
Set<Integer> ageRanges,
Set<Integer> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,7 +131,7 @@ public ResponseEntity<PostsResponse> readAllPostsOfTrip(
@GetMapping("/posts")
public ResponseEntity<PostsSearchResponse> readAllPosts(
@Auth LoginUser loginUser,
@RequestBody PostSearchRequest postSearchRequest
PostSearchRequest postSearchRequest
) {
PostsSearchResponse response = postService.readAll(postSearchRequest);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,16 +33,16 @@
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;
import org.junit.jupiter.api.Test;
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 {
Expand Down Expand Up @@ -594,76 +591,74 @@ 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<String, Object> jejuParams = Map.of(
"address", "제주특별자치도 제주시 애월읍",
"limit", 10
);

PostSearchRequest jeju17hourRequest = new PostSearchRequest(
PostSearchConditions.builder()
.hours(Set.of(17))
.address("제주특별자치도 제주시 애월읍")
.build(),
new PostSearchPaging(null, 10)
Map<String, Object> 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<String, Object> hour17Params = Map.of(
"hours", Set.of(17),
"limit", 10
);

// when
ExtractableResponse<Response> 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<Response> jeju17hourResponse = RestAssured.given().log().all()
.contentType(APPLICATION_JSON_VALUE)
ExtractableResponse<Response> jejuhour17Response = RestAssured.given().log().all()
.auth().preemptive().oauth2(huchuToken)
.body(jeju17hourRequest)
.params(jejuHour17Params)
.when().get("/posts")
.then().log().all()
.statusCode(OK.value())
.extract();

ExtractableResponse<Response> 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())
.extract();

// 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());
}

Expand Down

0 comments on commit bde72e5

Please sign in to comment.