From d953d341ed7bbb8d3709298867d4d381d955dd29 Mon Sep 17 00:00:00 2001 From: jainefer Date: Sat, 21 Dec 2024 21:02:16 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EA=B8=B0=20=EA=B1=B0=EB=A6=AC=20=EA=B8=B0?= =?UTF-8?q?=EB=B0=98=EC=9C=BC=EB=A1=9C=20=EA=B0=80=EC=A0=B8=EC=98=A4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PostsController.java | 15 +++++++++------ .../api/borrowPosts/service/PostsService.java | 3 ++- .../borrowPosts/service/PostsServiceImpl.java | 15 ++++++++------- .../borrowPosts/ItemDslRepository.java | 2 +- .../borrowPosts/ItemDslRepositoryImpl.java | 19 +++++++++++++++++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java index d6a33fc..1f003de 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java @@ -48,14 +48,17 @@ public BaseResponse getPostsController( @RequestParam(value = "category", required = false) String category, @Parameter(name = "page", description = "페이지 번호 (1부터 시작)", example = "1", in = ParameterIn.QUERY, required = false) @RequestParam(value = "page", required = false, defaultValue = "1") int page, - @Parameter(name = "order", description = "정렬 방향 (asc: 오름차순, desc: 내림차순)", example = "desc", in = ParameterIn.QUERY, required = false) + @Parameter(name = "order", description = "정렬 방향 (asc: 오름차순, desc: 내림차순)", example = "asc", in = ParameterIn.QUERY, required = false) @RequestParam(value = "order", required = false, defaultValue = "desc") String order, - @Parameter(name = "sortBy", description = "정렬 기준 (예: price, createdAt, likeCount)", example = "createdAt", in = ParameterIn.QUERY, required = true) + @Parameter(name = "sortBy", description = "정렬 기준 (예: price, createdAt, likeCount,distance)", example = "distance", in = ParameterIn.QUERY, required = true) @RequestParam(value = "sortBy", required = true, defaultValue = "accuracy") String sortBy ) { - + String userId = ""; + if (MDC.get(JWTUtil.MDC_USER_ID) != null) { + userId = MDC.get(JWTUtil.MDC_USER_ID); + } Sort.Direction direction = "asc".equalsIgnoreCase(order) ? Sort.Direction.ASC : Sort.Direction.DESC; - return new BaseResponse<>(postsService.ViewAllPostService(category, page, direction, sortBy)); + return new BaseResponse<>(postsService.ViewAllPostService(category, page, direction, sortBy,userId)); } @Operation(summary = "게시물 검색", description = "게시물 검색 API") @@ -65,9 +68,9 @@ public BaseResponse getSearchPostsControlle @RequestParam(value = "category", required = false) String category, @Parameter(name = "page", description = "페이지 번호 (1부터 시작)", example = "1", in = ParameterIn.QUERY, required = false) @RequestParam(value = "page", required = false, defaultValue = "1") int page, - @Parameter(name = "order", description = "정렬 방향 (asc: 오름차순, desc: 내림차순)", example = "desc", in = ParameterIn.QUERY, required = false) + @Parameter(name = "order", description = "정렬 방향 (asc: 오름차순, desc: 내림차순)", example = "asc", in = ParameterIn.QUERY, required = false) @RequestParam(value = "order", required = false, defaultValue = "desc") String order, - @Parameter(name = "sortBy", description = "정렬 기준 (예: price, createdAt, likeCount)", example = "createdAt", in = ParameterIn.QUERY, required = true) + @Parameter(name = "sortBy", description = "정렬 기준 (예: price, createdAt, likeCount,distance)", example = "distance", in = ParameterIn.QUERY, required = true) @RequestParam(value = "sortBy", required = true, defaultValue = "accuracy") String sortBy, @Parameter(name = "keyword", description = "검색 키워드(예: 6인용+텐트)", in = ParameterIn.QUERY, required = true) @RequestParam(value = "keyword", required = true) String keyword) { diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java index 27f52a6..6de1f61 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java @@ -3,13 +3,14 @@ import org.springframework.data.domain.Sort; import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest; import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse; +import site.billbill.apiserver.model.user.UserLocationJpaEntity; import java.util.List; public interface PostsService { PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request, String userId); - PostsResponse.ViewAllResultResponse ViewAllPostService(String category, int page, Sort.Direction direction, String orderType); + PostsResponse.ViewAllResultResponse ViewAllPostService(String category, int page, Sort.Direction direction, String orderType, String userId); PostsResponse.ViewPostResponse ViewPostService(String postId, String userId); diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java index a95834c..b5cbb57 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java @@ -83,11 +83,11 @@ public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest } public PostsResponse.ViewAllResultResponse ViewAllPostService( - String category, int page, Sort.Direction direction, String orderType) { - + String category, int page, Sort.Direction direction, String orderType,String userId) { + UserLocationJpaEntity userLocation = userLocationReposity.findByUserId(userId).orElse(null); Pageable pageable = createPageable(page, direction, orderType); log.info(category); - List items = findAndConvertItems(category, pageable, null); + List items = findAndConvertItems(category, pageable, null,userLocation); return PostsConverter.toViewAllList(items); } @@ -175,10 +175,10 @@ public String UpdatePostService(String postId, String userId, PostsRequest.Uploa @Transactional public PostsResponse.ViewAllResultResponse ViewSearchPostService(String userId, String category, int page, Sort.Direction direction, String orderType, String keyword, boolean state) { - UserJpaEntity user = userRepository.findById(userId).orElse(null); + UserLocationJpaEntity userLocation= userLocationReposity.findByUserId(userId).orElse(null); Pageable pageable = createPageable(page, direction, orderType); - List items = findAndConvertItems(category, pageable, keyword); + List items = findAndConvertItems(category, pageable, keyword,userLocation); //사용자가 검색어 저장을 허용했을 경우 String tempKeyword = keyword.replaceAll("\\+", " "); // if(state){ @@ -330,6 +330,7 @@ private Pageable createPageable(int page, Sort.Direction direction, String order case "price" -> "price"; case "createdAt" -> "createdAt"; case "likeCount" -> "likeCount"; + case "distance"->"distance"; default -> "createdAt"; // 기본 정렬 }; //정렬 순서 @@ -342,9 +343,9 @@ private Pageable createPageable(int page, Sort.Direction direction, String order ); } - private List findAndConvertItems(String category, Pageable pageable, String keyword) { + private List findAndConvertItems(String category, Pageable pageable, String keyword,UserLocationJpaEntity userLocation) { // Repository 호출 - Page itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword); + Page itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword,userLocation.getLatitude(),userLocation.getLongitude()); // 빈 결과 체크 if (itemsPage.isEmpty()) { diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepository.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepository.java index eca7ae7..5172687 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepository.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepository.java @@ -12,7 +12,7 @@ public interface ItemDslRepository { - Page findItemsWithConditions(String category, Pageable pageable, String sortField,String keyword); + Page findItemsWithConditions(String category, Pageable pageable, String sortField,String keyword,Double latitude,Double longitude); List getPostHistory(String userId, Pageable pageable); diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java index 6c61cbd..cad6b16 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java @@ -7,6 +7,7 @@ import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.core.types.dsl.NumberExpression; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; @@ -30,11 +31,11 @@ public class ItemDslRepositoryImpl implements ItemDslRepository { private final JPAQueryFactory queryFactory; @Override - public Page findItemsWithConditions(String category, Pageable pageable, String sortField, String keyword) { + public Page findItemsWithConditions(String category, Pageable pageable, String sortField, String keyword,Double latitude,Double longitude) { QItemsJpaEntity items = QItemsJpaEntity.itemsJpaEntity; QItemsBorrowJpaEntity borrow = QItemsBorrowJpaEntity.itemsBorrowJpaEntity; QItemsCategoryJpaEntity categoryEntity = QItemsCategoryJpaEntity.itemsCategoryJpaEntity; - + QItemsLocationJpaEntity QItemsLocation = QItemsLocationJpaEntity.itemsLocationJpaEntity; JPAQuery query = queryFactory.selectFrom(items) .leftJoin(items.category, categoryEntity).fetchJoin() // 명시적 Fetch Join @@ -70,6 +71,20 @@ public Page findItemsWithConditions(String category, Pageable pa orderSpecifier = order.isAscending() ? items.createdAt.asc() : items.createdAt.desc(); case "likeCount" -> orderSpecifier = order.isAscending() ? items.likeCount.asc() : items.likeCount.desc(); + case "distance" -> { + if (latitude != null && longitude != null) { + // 거리 계산식 + NumberExpression distanceExpression = Expressions.numberTemplate( + Double.class, + "ST_Distance_Sphere(POINT({0}, {1}), POINT({2}, {3}))", + QItemsLocation.latitude, QItemsLocation.longitude, latitude, longitude + ); + orderSpecifier = order.isAscending() ? distanceExpression.asc() : distanceExpression.desc(); + } else { + log.warn("Latitude와 Longitude가 필요합니다."); + return; + } + } default -> { log.warn("Invalid sort field: {}", order.getProperty()); return;