-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from TRIP-Side-Project/dev
pr for merge
- Loading branch information
Showing
8 changed files
with
280 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.api.trip.common.init; | ||
|
||
import com.api.trip.common.naverapi.NaverApiService; | ||
import com.api.trip.common.naverapi.NaverClient; | ||
import com.api.trip.common.naverapi.dto.ShoppingItem; | ||
import com.api.trip.common.naverapi.dto.ShoppingRequest; | ||
import com.api.trip.common.naverapi.dto.ShoppingResponse; | ||
import com.api.trip.domain.item.controller.dto.CreateItemRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class InitData implements ApplicationRunner { | ||
|
||
private final NaverClient naverClient; | ||
private final NaverApiService naverApiService; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
|
||
/* | ||
ShoppingRequest request = ShoppingRequest.builder() | ||
.start(1) | ||
.display(100).build(); | ||
ShoppingResponse searchResponse = naverClient.search(request); | ||
List<ShoppingItem> shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems()); | ||
List<CreateItemRequest> createItemRequests = naverApiService.toCreateItemRequest(shoppingItems); | ||
for(CreateItemRequest createItemRequest :createItemRequests) | ||
System.out.println(createItemRequest.toString()); | ||
*/ | ||
|
||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/api/trip/common/naverapi/NaverApiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.api.trip.common.naverapi; | ||
|
||
|
||
import com.api.trip.common.naverapi.dto.ShoppingItem; | ||
import com.api.trip.domain.item.controller.dto.CreateItemRequest; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Service | ||
public class NaverApiService { | ||
|
||
private final List<String> tagList = Arrays.asList("부산", "제주", "서울", "크루즈", "일출", "해수욕장"); | ||
|
||
public List<ShoppingItem> doFilterCategory(List<ShoppingItem> items){ | ||
List<ShoppingItem> filteredItems = new ArrayList<>(); | ||
for (ShoppingItem item: items) | ||
{ | ||
if( | ||
item.getCategory1().equals("국내패키지/기타") || | ||
item.getCategory2().equals("국내패키지/기타") || | ||
item.getCategory3().equals("국내패키지/기타") || | ||
item.getCategory4().equals("국내패키지/기타") | ||
) | ||
filteredItems.add(item); | ||
} | ||
return filteredItems; | ||
} | ||
|
||
public List<CreateItemRequest> toCreateItemRequest(List<ShoppingItem> items){ | ||
List<CreateItemRequest> createItemRequests = new ArrayList<>(); | ||
|
||
|
||
for(ShoppingItem item : items){ | ||
|
||
List<String> tagsNames = extractTagNames(item.getTitle()); | ||
createItemRequests.add(CreateItemRequest.of(item, tagsNames)); | ||
|
||
} | ||
return createItemRequests; | ||
} | ||
|
||
public List<String> extractTagNames(String title){ | ||
return tagList.stream().filter(tag -> title.contains(tag)).toList(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/api/trip/common/naverapi/NaverClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.api.trip.common.naverapi; | ||
|
||
import com.api.trip.common.naverapi.dto.ShoppingRequest; | ||
import com.api.trip.common.naverapi.dto.ShoppingResponse; | ||
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.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.net.URI; | ||
|
||
@Component //빈으로 등록 | ||
public class NaverClient { | ||
@Value("${custom.naver.url}") | ||
private String naverUrl; | ||
@Value("${custom.naver.clientId}") | ||
private String clientId; | ||
@Value("${custom.naver.clientSecret}") | ||
private String clientSecret; | ||
|
||
public ShoppingResponse search(ShoppingRequest request) { | ||
URI uri = UriComponentsBuilder.fromUriString(naverUrl) | ||
.queryParams(request.map()) | ||
.build() | ||
.encode() | ||
.toUri(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("X-Naver-Client-Id", clientId); | ||
headers.set("X-Naver-Client-Secret", clientSecret); | ||
|
||
HttpEntity httpEntity = new HttpEntity<>(headers); | ||
|
||
ResponseEntity<ShoppingResponse> entity = new RestTemplate().exchange( | ||
uri, | ||
HttpMethod.GET, | ||
httpEntity, | ||
ShoppingResponse.class | ||
); | ||
|
||
return entity.getBody(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/api/trip/common/naverapi/dto/ShoppingItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.api.trip.common.naverapi.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ShoppingItem { | ||
private Long productId; | ||
private String title; //상품이름 | ||
private String link; //상품 url | ||
private String image; //상품 이미지 url | ||
private Integer lprice; //최저가 | ||
private Integer hprice; //최고가 | ||
private String mallName; //상품을 판매하는 쇼핑몰 | ||
private String maker; //제조사 | ||
private String brand; //브랜드 | ||
private String category1; | ||
private String category2; | ||
private String category3; | ||
private String category4; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/api/trip/common/naverapi/dto/ShoppingRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.api.trip.common.naverapi.dto; | ||
|
||
import lombok.*; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
@Getter | ||
@NoArgsConstructor //기본 생성자 만들기 | ||
public class ShoppingRequest { | ||
private String query; //검색어 | ||
private String sort; | ||
private Integer display; //검색결과 갯수는 1개만 | ||
private Integer start; | ||
|
||
@Builder | ||
public ShoppingRequest(Integer display, Integer start) { | ||
this.query = "국내패키지"; | ||
this.sort = "date"; | ||
this.display = display; | ||
this.start = start; | ||
} | ||
|
||
public MultiValueMap map() { //파라미터를 넘기기 위한 맵 | ||
LinkedMultiValueMap<String, String > map = new LinkedMultiValueMap(); | ||
map.add("query", query); | ||
map.add("display", String.valueOf(display)); | ||
map.add("sort", sort); | ||
map.add("start", String.valueOf(start)); | ||
return map; | ||
} | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/api/trip/common/naverapi/dto/ShoppingResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.api.trip.common.naverapi.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ShoppingResponse { | ||
private Date lastBuildDate; //검색결과를 생성한 시간 | ||
private String total; //총 검색 결과 | ||
private List<ShoppingItem> items; //아이템을 받을 리스트 | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters