Skip to content

Commit

Permalink
Merge pull request #7 from SOPT-all/feat/6
Browse files Browse the repository at this point in the history
[FEAT] 장소 DTO 구현, 장소 검색 API 반환 형식 수정
  • Loading branch information
airoca authored Jan 15, 2025
2 parents 52e29fb + aa64de6 commit 09e2b15
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.spoony.spoony_server.common.dto.ResponseDTO;
import com.spoony.spoony_server.common.exception.BusinessException;
import com.spoony.spoony_server.common.message.PlaceErrorMessage;
import com.spoony.spoony_server.domain.place.dto.PlaceListResponseDTO;
import com.spoony.spoony_server.domain.place.dto.PlaceResponseDTO;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -18,14 +20,16 @@

import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/place")
public class PlaceController {

@GetMapping(value = "/search")
public ResponseEntity<ResponseDTO<JsonNode>> getPlaceList(
@RequestParam(name = "query") String text,
public ResponseEntity<ResponseDTO<PlaceListResponseDTO>> getPlaceList(
@RequestParam(name = "query") String query,
@RequestParam(name = "display", required = false, defaultValue = "5") int display) {

// 네이버 지역 검색 API
Expand All @@ -35,7 +39,7 @@ public ResponseEntity<ResponseDTO<JsonNode>> getPlaceList(
URI uri = UriComponentsBuilder
.fromUriString("https://openapi.naver.com")
.path("/v1/search/local.json")
.queryParam("query", text)
.queryParam("query", query)
.queryParam("display", display)
.queryParam("start", 1)
.queryParam("sort", "comment")
Expand All @@ -60,8 +64,21 @@ public ResponseEntity<ResponseDTO<JsonNode>> getPlaceList(
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode placeList = objectMapper.readTree(searchResultBody);
List<PlaceResponseDTO> places = new ArrayList<>();

return ResponseEntity.status(HttpStatus.OK).body(ResponseDTO.success(placeList));
placeList.get("items").forEach(item -> {
String title = item.get("title").asText().replaceAll("<[^>]*>", ""); // HTML 태그 제거
String address = item.get("address").asText();
String roadAddress = item.get("roadAddress").asText();
String mapx = item.get("mapx").asText();
String mapy = item.get("mapy").asText();

places.add(new PlaceResponseDTO(title, address, roadAddress, mapx, mapy));
});

PlaceListResponseDTO placeListResponseDTO = new PlaceListResponseDTO(places);

return ResponseEntity.status(HttpStatus.OK).body(ResponseDTO.success(placeListResponseDTO));

} catch (Exception e) {
throw new BusinessException(PlaceErrorMessage.JSON_PARSE_ERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.spoony.spoony_server.domain.place.dto;

import java.util.List;

public record PlaceListResponseDTO(List<PlaceResponseDTO> diaryList) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.spoony.spoony_server.domain.place.dto;

public record PlaceResponseDTO(String placeName,
String placeAddress,
String placeRoadAddress,
String latitude,
String longitude) {
}

0 comments on commit 09e2b15

Please sign in to comment.