Skip to content

Commit

Permalink
#2 - Feat: Add Keyword based Search basic MVCs
Browse files Browse the repository at this point in the history
  • Loading branch information
eun61n00 committed Jun 18, 2023
1 parent 19d26e4 commit c2a0909
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.routemaster.api.total.domain.attraction.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.routemaster.api.total.domain.attraction.service.AttractionSearchService;
import org.routemaster.api.total.infra.tourapi.vo.AttractionSearchVO;
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 reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/attraction-search")
@RequiredArgsConstructor
public class KeywordBasedAttractionRestController {

private final AttractionSearchService attractionSearchService;

@GetMapping("/keyword-based")
public ResponseEntity<Mono<AttractionSearchVO>> keywordBased(
@RequestParam(required = false) Integer numOfRows,
@RequestParam(required = false) Integer pageNo,
@RequestParam String MobileOS,
@RequestParam String MobileApp,
@RequestParam(required = false) String _type,
@RequestParam(required = false) String listYN,
@RequestParam(required = false) String arrange,
@RequestParam String keyword,
@RequestParam(required = false) Integer contentTypeId,
@RequestParam(required = false) String areaCode,
@RequestParam(required = false) String sigunguCode,
@RequestParam(required = false) String cat1,
@RequestParam(required = false) String cat2,
@RequestParam(required = false) String cat3
) {
Mono<AttractionSearchVO> result = attractionSearchService.searchKeywordBasedAttraction(
numOfRows,
pageNo,
MobileOS,
MobileApp,
_type,
listYN,
arrange,
keyword,
contentTypeId,
areaCode,
sigunguCode,
cat1,
cat2,
cat3
);
return ResponseEntity.ok(result);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.routemaster.api.total.domain.attraction.service;

import org.routemaster.api.total.infra.tourapi.vo.AreaBasedAttractionVO;
import org.routemaster.api.total.infra.tourapi.vo.AttractionSearchVO;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -35,7 +34,24 @@ Mono<AttractionSearchVO> searchAreaBasedAttraction(
String cat1,
String cat2,
String cat3,
String modifiedtime
String modifiedTime
);

Mono<AttractionSearchVO> searchKeywordBasedAttraction(
Integer numOfRows,
Integer pageNo,
String MobileOS,
String MobileApp,
String _type,
String listYN,
String arrange,
String keyword,
Integer contentTypeId,
String areaCode,
String sigunguCode,
String cat1,
String cat2,
String cat3
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


@Slf4j
Expand Down Expand Up @@ -149,4 +151,69 @@ public Mono<AttractionSearchVO> searchAreaBasedAttraction(
);
return result;
}

@Override
public Mono<AttractionSearchVO> searchKeywordBasedAttraction(
Integer numOfRows,
Integer pageNo,
String MobileOS,
String MobileApp,
String _type,
String listYN,
String arrange,
String keyword,
Integer contentTypeId,
String areaCode,
String sigunguCode,
String cat1,
String cat2,
String cat3
) {
ObjectMapper mapper = new ObjectMapper();
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(TourAPI.baseUrl);
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
WebClient webClient = WebClient.builder()
.uriBuilderFactory(factory)
.baseUrl(TourAPI.baseUrl)
.build();
Mono<AttractionSearchVO> result = webClient.get()
.uri(uriBuilder -> uriBuilder
.path("/searchKeyword1")
.queryParam("serviceKey", TourAPI.encodingKey)
.queryParam("MobileOS", MobileOS)
.queryParam("MobileApp", MobileApp)
.queryParam("_type", "json")
.queryParam("numOfRows", numOfRows)
.queryParam("pageNo", pageNo)
.queryParam("listYN", listYN)
.queryParam("arrange", arrange)
.queryParam("keyword", URLEncoder.encode(keyword, StandardCharsets.UTF_8))
.queryParam("contentTypeId", contentTypeId)
.queryParam("areaCode", areaCode)
.queryParam("sigunguCode", sigunguCode)
.queryParam("cat1", cat1)
.queryParam("cat2", cat2)
.queryParam("cat3", cat3)
.build()
)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class)
.map(str -> {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode jsonNode = mapper.readTree(str);
AttractionSearchVO attractionSearchVO = AttractionSearchVO.builder()
.resultCode(jsonNode.get("response").get("header").get("resultCode").asText())
.resultMessage(jsonNode.get("response").get("header").get("resultMsg").asText())
.keywordBasedItems(jsonNode.get("response").get("body"))
.build();
return attractionSearchVO;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public AttractionSearchVOBuilder areaBasedItems(JsonNode jsonNode) {
this.totalCount = jsonNode.get("totalCount").asInt();
return this;
}

public AttractionSearchVOBuilder keywordBasedItems(JsonNode jsonNode) {
this.attractions = new ArrayList<>();
jsonNode.get("items").get("item").forEach(item -> {
try {
AttractionVO attractionVO = new KeywordBasedAttractionVO(item);
this.attractions.add(attractionVO);
} catch (Exception e) {
e.printStackTrace();
}
});
this.numOfRows = jsonNode.get("numOfRows").asInt();
this.pageNo = jsonNode.get("pageNo").asInt();
this.totalCount = jsonNode.get("totalCount").asInt();
return this;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.routemaster.api.total.infra.tourapi.vo;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PROTECTED)
//@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
public class KeywordBasedAttractionVO extends AttractionVO {

public KeywordBasedAttractionVO(JsonNode item) throws ParseException {
super(item);
}

}

0 comments on commit c2a0909

Please sign in to comment.