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));