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

feat: 학교 맛집 kakao api 사용하여 받아옴 #173

Merged
merged 1 commit into from
Nov 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
@@ -0,0 +1,25 @@
package yerong.wedle.category.restaurant.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yerong.wedle.category.restaurant.dto.RestaurantResponse;
import yerong.wedle.category.restaurant.service.KakaoSearchApiService;

@RequiredArgsConstructor
@RequestMapping("/api/kakao-search")
@RestController
public class KakaoSearchApiController {

private final KakaoSearchApiService kakaoSearchApiService;

@GetMapping("/kakao")
public ResponseEntity<List<RestaurantResponse>> kakaoSearchDynamic(@RequestParam String universityName) {
List<RestaurantResponse> restaurantResponses = kakaoSearchApiService.searchRestaurant(universityName + " 맛집");
return ResponseEntity.ok(restaurantResponses);
}
}

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/yerong/wedle/category/restaurant/domain/Hashtag.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package yerong.wedle.category.restaurant.dto;

import lombok.Data;

@Data
public class KakaoApiDocument {
private String place_name;
private String road_address_name;
private String address_name;
private String phone;
private String category_name;
private String place_url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package yerong.wedle.category.restaurant.dto;

import java.util.List;
import lombok.Data;

@Data
public class KakaoApiResponse {
private List<KakaoApiDocument> documents;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package yerong.wedle.category.restaurant.dto;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import yerong.wedle.category.restaurant.domain.Hashtag;
import yerong.wedle.university.domain.University;

import java.util.ArrayList;
import java.util.List;

import static jakarta.persistence.FetchType.LAZY;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class RestaurantResponse {
private String name;
private String location;
private String roadAddressName;
private String addressName;
private String phone;
private String categoryName;
private String placeUrl;
private List<String> hashtags;
private String imageUrl;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package yerong.wedle.category.restaurant.service;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import yerong.wedle.category.restaurant.dto.RestaurantResponse;
import yerong.wedle.category.restaurant.dto.KakaoApiDocument;
import yerong.wedle.category.restaurant.dto.KakaoApiResponse;

@Service
@RequiredArgsConstructor
public class KakaoSearchApiService {

@Value("${kakao.client-id}")
private String KAKAO_CLIENT_ID;

private final RestTemplate restTemplate;

public List<RestaurantResponse> searchRestaurant(String query) {
String url = "https://dapi.kakao.com/v2/local/search/keyword.json?query=" + query + "&size=15";

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "KakaoAK " + KAKAO_CLIENT_ID);

HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<KakaoApiResponse> response = restTemplate.exchange(url, HttpMethod.GET, entity, KakaoApiResponse.class);

List<RestaurantResponse> restaurantResponses = new ArrayList<>();

if (response.getBody() != null && response.getBody().getDocuments() != null) {
for (KakaoApiDocument document : response.getBody().getDocuments()) {
String fullCategoryName = document.getCategory_name();
String lastCategory = null;

if (fullCategoryName != null && !fullCategoryName.isEmpty()) {
String[] categories = fullCategoryName.split(" > ");
if (categories.length > 0) {
lastCategory = categories[categories.length - 1]; // 마지막 부분
}
}

restaurantResponses.add(RestaurantResponse.builder()
.name(document.getPlace_name())
.roadAddressName(document.getRoad_address_name())
.addressName(document.getAddress_name())
.categoryName(lastCategory)
.phone(document.getPhone())
.placeUrl(document.getPlace_url())
.build());
}
}

return restaurantResponses; // 맛집 정보 반환
}
}

This file was deleted.

Loading
Loading