MoonSeokPyo / Papaya_nlpApp

nlpApp, PAPAYA
HomePage
0 stars 0 forks source link

[기능] searchByCategory #06 #6

Open akaneblue opened 1 week ago

akaneblue commented 1 week ago

카테고리로 음식점 검색 추가

@Query("SELECT new com.hangw.model.RestaurantDTO(r.id, r.name, r.address, r.score, r.latitude, r.longitude, r.category, "
            + "(6371 * acos(cos(radians(:latitude)) * cos(radians(r.latitude)) * cos(radians(r.longitude) - radians(:longitude)) + sin(radians(:latitude)) * "
            + "sin(radians(r.latitude)))) AS distance) " + "FROM Restaurant r "
            + "WHERE r.latitude IS NOT NULL AND r.longitude IS NOT NULL AND r.category = :category "
            + "ORDER BY r.score DESC")
    List<RestaurantDTO> getRestaurantsByCategory(@Param("category") String category, double latitude, double longitude, Pageable pageable);

category로 음식점 찾기(repository)

@GetMapping("/search/category")
    public String searchRestaurantByCategory(@RequestParam String address, @RequestParam String category, Model model) {
        try {
            Location location = geocodingService.getCoordinates(address);
            List<RestaurantDTO> restaurants = restaurantService.getRestaurantByCategory(category, location.getLatitude(), location.getLongitude());
            model.addAttribute("restaurants", restaurants);
            model.addAttribute("location", location);

            if (restaurants.isEmpty())
                model.addAttribute("errorMessage", "카테고리의 맛집을 찾을 수 없습니다.");
        } catch (Exception e) {
            model.addAttribute("errorMessage", "카테고리의 맛집을 찾을 수 없습니다.");
        }
        return "result";
    }

(controller)