Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 카테고리별, 도시별 중복 필터 처리 조회 API #119

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,13 +20,12 @@ public class PlaceController {
private final PlaceService service;

@GetMapping
public ResponseEntity<List<Place>> getPlaces() {
public ResponseEntity<List<Place>> 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<List<Place>> getPlacesByCategoryId(@PathVariable Long category_idx) {
return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PlaceEntity, Long> {
public List<PlaceEntity> 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<PlaceEntity> findByCategoryIdxAndCityIdx(@Param("category_id") Long category_id, @Param("city_id") Long city_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public interface PlaceService {
public List<Place> getAll();
public List<Place> getPlacesByCategoryId(Long categoryId);
public List<Place> getPlacesByCategoryIdAndCityId(Long categoryId, Long cityId);
PlaceEntity findByPlaceIdx(Long placeIdx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public List<Place> getPlacesByCategoryId(Long category_id) {
return places;
}

@Override
public List<Place> getPlacesByCategoryIdAndCityId(Long category_id, Long city_id) {
List<PlaceEntity> entities = repository.findByCategoryIdxAndCityIdx(category_id, city_id);
List<Place> 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));
Expand Down
Loading