Skip to content

Commit

Permalink
feat: #36 상품 생성 크롤링 동적 Url 연동 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaPol committed Feb 4, 2024
1 parent 79d0517 commit 9b4af14
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 63 deletions.
14 changes: 14 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ bootJar { enabled = true }
// 외부에서 의존하기 위한 jar로 생성하는 옵션, main이 없는 라이브러리에서는 true로 비활성화함
jar { enabled = false }

ext {
set('springCloudVersion', "2022.0.5")
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}


dependencies {
implementation project(':core:core-domain');
implementation project(':core:core-infra-rdb');
Expand Down Expand Up @@ -38,4 +49,7 @@ dependencies {

// sentry
implementation 'io.sentry:sentry-spring-boot-starter-jakarta:7.1.0'

// feign
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
}
3 changes: 3 additions & 0 deletions api/http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ Content-Type: application/json

### 상품 페이지 조회
GET http://localhost:8080/api/v1/items

### 어드민 크롤링 기능
POST http://localhost:8080/api/v1/admin/items/crawl?url=https://s.zigzag.kr/f2KRWpFiXx
26 changes: 16 additions & 10 deletions api/src/main/java/com/mm/api/domain/admin/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
@Transactional
@RequiredArgsConstructor
public class AdminService {
private ItemRepository itemRepository;

private CrawlerService crawlerService;
private final ItemRepository itemRepository;
private final CrawlerService crawlerService;

public ItemDetailResponse crawlItem(String url) {
Item savedItem = itemRepository.save(crawlerService.getZigZagItemByCrawler(url));
Item item = crawlerService.getZigZagItemByCrawler(url);
Item savedItem = itemRepository.save(item);
return getItemDetailResponseByItem(savedItem);
}

private ItemDetailResponse getItemDetailResponseByItem(Item savedItem) {
List<String> images = savedItem.getItemImages().stream()
.map(ItemImage::getUrl)
.toList();
List<String> videos = savedItem.getItemVideos().stream()
.map(ItemVideo::getUrl)
.toList();
List<String> images = null;
if (savedItem.getItemImages() != null) {
images = savedItem.getItemImages().stream()
.map(ItemImage::getUrl)
.toList();
}
List<String> videos = null;
if (savedItem.getItemVideos() != null) {
videos = savedItem.getItemVideos().stream()
.map(ItemVideo::getUrl)
.toList();
}
return ItemDetailResponse.of(savedItem, images, videos, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public record ItemResponse(Long id,
String title,
String detail,
String redirectUrl,
ItemCategoryType categoryType,
Integer price,
Expand All @@ -17,7 +16,6 @@ public static ItemResponse of(Item item, Boolean isDib) {
return new ItemResponse(
item.getId(),
item.getTitle(),
item.getDetail(),
item.getRedirectUrl(),
item.getCategoryType(),
item.getPrice(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
Expand All @@ -19,6 +21,7 @@
@AllArgsConstructor
public class ItemImage extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Lob
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package com.mm.coredomain.domain;

import jakarta.persistence.*;
import lombok.*;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ItemVideo extends BaseEntity {
@Id
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Lob
private String url;
@Lob
private String url;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.mm.coreinfrafeign.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.mm.coreinfrafeign.dto.requset.ZigZagCrawlerRequest;
import com.mm.coreinfrafeign.dto.response.ZigZagCrawlerResponse;

@Component
@FeignClient(name = "ZigZagCrawler", url = "https://87vlx0pzf0.execute-api.ap-northeast-2.amazonaws.com/crawler/zigzag")
public interface ZigZagCrawlerClient {
@GetMapping
@PostMapping
ZigZagCrawlerResponse call(
@RequestBody ZigZagCrawlerRequest request
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.mm.coreinfrafeign.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableFeignClients("com.mm.coreinfrafeign")
@ComponentScan(basePackages = "com.mm")
@EnableFeignClients(basePackages = "com.mm")
public class FeignConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public record ZigZagCrawlerResponse(
String type,
String pageUrl,
String siteName,
String price,
String brand,
String thumbnailUrl,
String name,
String categoryKey,
List<Category> categoryList,
List<ProductImage> productImageList,
List<Category> managedCategoryList,
int originalPrice,
int finalPrice
@JsonProperty("type") String type,
@JsonProperty("page_url") String pageUrl,
@JsonProperty("site_name") String siteName,
@JsonProperty("price") String price,
@JsonProperty("brand") String brand,
@JsonProperty("thumbnail_url") String thumbnailUrl,
@JsonProperty("name") String name,
@JsonProperty("category_key") String categoryKey,
@JsonProperty("category_list") List<Category> categoryList,
@JsonProperty("product_image_list") List<ProductImage> productImageList,
@JsonProperty("managed_category_list") List<Category> managedCategoryList,
@JsonProperty("original_price") int originalPrice,
@JsonProperty("final_price") int finalPrice
) {
public static record Category(int categoryId, String value) {
public static record Category(@JsonProperty("category_id") int categoryId, @JsonProperty("value") String value) {
}

public static record ProductImage(
String id,
String url,
String originUrl,
String pdpThumbnailUrl,
String pdpStaticImageUrl,
String imageType,
int originWidth,
int originHeight
@JsonProperty("id") String id,
@JsonProperty("url") String url,
@JsonProperty("origin_url") String originUrl,
@JsonProperty("pdp_thumbnail_url") String pdpThumbnailUrl,
@JsonProperty("pdp_static_image_url") String pdpStaticImageUrl,
@JsonProperty("image_type") String imageType,
@JsonProperty("origin_width") int originWidth,
@JsonProperty("origin_height") int originHeight
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mm.coredomain.domain.Item;
import com.mm.coredomain.domain.ItemCategoryType;
Expand All @@ -14,6 +15,7 @@
import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class CrawlerService {
private final ZigZagCrawlerClient zigZagCrawlerClient;
Expand Down Expand Up @@ -46,6 +48,6 @@ private static ItemCategoryType getCategoryType(ZigZagCrawlerResponse response)
}

private Integer getRefundPrice(Integer price, Integer percent) {
return (100 - percent) / 100 * price;
return price * (100 - percent) / 100;
}
}
16 changes: 0 additions & 16 deletions core/core-infra-qdsl/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,3 @@ insert into groups(id, name) values(2, 'USER_GROUP');

insert into group_permission(id, groups_id, permission_id) values(1, 1, 1);
insert into group_permission(id, groups_id, permission_id) values(2, 2, 2);

insert into item(id, detail, redirect_url, category_type, price, refund, rating, thumbnail_url)
values (1, 'test', 'https://s.zigzag.kr/3HSmoWF6Xa?af=1', 'BEAUTY', 10800, 1000, 5.0, 'https://cf.product-image.s.zigzag.kr/original/d/2023/9/21/21909_202309210952510497_81140.jpeg?width=720&height=720&quality=80&format=webp');
insert into item_video(id, url, item_id) values (1, 'https://youtu.be/TDmzXuma4Lw?si=9RFwafiFgTUOMsvE', 1);

insert into item(id, detail, redirect_url, category_type, price, refund, rating, thumbnail_url)
values (2, 'test', 'https://s.zigzag.kr/3HSmoWF6Xa?af=1', 'FOOD', 10800, 1000, 5.0, 'https://cf.product-image.s.zigzag.kr/original/d/2023/9/21/21909_202309210952510497_81140.jpeg?width=720&height=720&quality=80&format=webp');
insert into item_video(id, url, item_id) values (2, 'https://youtu.be/TDmzXuma4Lw?si=9RFwafiFgTUOMsvE', 2);

insert into item(id, detail, redirect_url, category_type, price, refund, rating, thumbnail_url)
values (3, 'test', 'https://s.zigzag.kr/3HSmoWF6Xa?af=1', 'LIFE', 10800, 1000, 5.0, 'https://cf.product-image.s.zigzag.kr/original/d/2023/9/21/21909_202309210952510497_81140.jpeg?width=720&height=720&quality=80&format=webp');
insert into item_video(id, url, item_id) values (3, 'https://youtu.be/TDmzXuma4Lw?si=9RFwafiFgTUOMsvE', 3);

insert into item(id, detail, redirect_url, category_type, price, refund, rating, thumbnail_url)
values (4, 'test', 'https://s.zigzag.kr/3HSmoWF6Xa?af=1', 'BEAUTY', 10800, 1000, 5.0, 'https://cf.product-image.s.zigzag.kr/original/d/2023/9/21/21909_202309210952510497_81140.jpeg?width=720&height=720&quality=80&format=webp');
insert into item_video(id, url, item_id) values (4, 'https://youtu.be/TDmzXuma4Lw?si=9RFwafiFgTUOMsvE', 4);

0 comments on commit 9b4af14

Please sign in to comment.