From 5842f85fcb1345ed586b854dd24a6aac0d83dc96 Mon Sep 17 00:00:00 2001 From: lcw729 Date: Tue, 4 Jun 2024 16:19:35 +0900 Subject: [PATCH] =?UTF-8?q?feature:=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EB=B3=84,=20=EB=8F=84=EC=8B=9C=EB=B3=84=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=20=ED=95=84=ED=84=B0=20=EC=B2=98=EB=A6=AC=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../place/controller/PlaceController.java | 12 ++++-------- .../triptogether/place/domain/PlaceRepository.java | 5 +++++ .../triptogether/place/service/PlaceService.java | 1 + .../place/service/impl/PlaceServiceImpl.java | 9 +++++++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/hanaro/triptogether/place/controller/PlaceController.java b/src/main/java/com/hanaro/triptogether/place/controller/PlaceController.java index 2b9251c..9ac4770 100644 --- a/src/main/java/com/hanaro/triptogether/place/controller/PlaceController.java +++ b/src/main/java/com/hanaro/triptogether/place/controller/PlaceController.java @@ -7,10 +7,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Repository; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -23,13 +20,12 @@ public class PlaceController { private final PlaceService service; @GetMapping - public ResponseEntity> getPlaces() { + public ResponseEntity> getPlacesByCategoryIdAndCityId(@RequestParam(required = false) Long category_id, + @RequestParam(required = false) Long city_id) { return ResponseEntity .status(HttpStatus.OK) - .body(service.getAll()); + .body(service.getPlacesByCategoryIdAndCityId(category_id, city_id)); } - - @GetMapping("/categories/{category_idx}") public ResponseEntity> getPlacesByCategoryId(@PathVariable Long category_idx) { return ResponseEntity diff --git a/src/main/java/com/hanaro/triptogether/place/domain/PlaceRepository.java b/src/main/java/com/hanaro/triptogether/place/domain/PlaceRepository.java index e8ac2c3..324ee69 100644 --- a/src/main/java/com/hanaro/triptogether/place/domain/PlaceRepository.java +++ b/src/main/java/com/hanaro/triptogether/place/domain/PlaceRepository.java @@ -1,10 +1,15 @@ package com.hanaro.triptogether.place.domain; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface PlaceRepository extends JpaRepository { public List findByCategoryIdx(Long category_id); + + @Query("SELECT p FROM PlaceEntity p WHERE (:category_id IS NULL OR p.categoryIdx = :category_id) AND (:city_id IS NULL OR p.city.cityIdx = :city_id)") + List findByCategoryIdxAndCityIdx(@Param("category_id") Long category_id, @Param("city_id") Long city_id); } diff --git a/src/main/java/com/hanaro/triptogether/place/service/PlaceService.java b/src/main/java/com/hanaro/triptogether/place/service/PlaceService.java index c3fd349..ebaf4e0 100644 --- a/src/main/java/com/hanaro/triptogether/place/service/PlaceService.java +++ b/src/main/java/com/hanaro/triptogether/place/service/PlaceService.java @@ -8,5 +8,6 @@ public interface PlaceService { public List getAll(); public List getPlacesByCategoryId(Long categoryId); + public List getPlacesByCategoryIdAndCityId(Long categoryId, Long cityId); PlaceEntity findByPlaceIdx(Long placeIdx); } diff --git a/src/main/java/com/hanaro/triptogether/place/service/impl/PlaceServiceImpl.java b/src/main/java/com/hanaro/triptogether/place/service/impl/PlaceServiceImpl.java index 1e34f6f..19c0878 100644 --- a/src/main/java/com/hanaro/triptogether/place/service/impl/PlaceServiceImpl.java +++ b/src/main/java/com/hanaro/triptogether/place/service/impl/PlaceServiceImpl.java @@ -37,6 +37,15 @@ public List getPlacesByCategoryId(Long category_id) { return places; } + @Override + public List getPlacesByCategoryIdAndCityId(Long category_id, Long city_id) { + List entities = repository.findByCategoryIdxAndCityIdx(category_id, city_id); + List places = entities.stream().map((entity) -> entity.toPlace()) + .collect(Collectors.toList()); + + return places; + }; + @Override public PlaceEntity findByPlaceIdx(Long placeIdx) { return repository.findById(placeIdx).orElseThrow(()->new ApiException(ExceptionEnum.PLACE_NOT_FOUND));