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: 어드민 크롤링 기능 추가 #37

Merged
merged 3 commits into from
Feb 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
15 changes: 15 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ 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');
implementation project(':core:core-infra-feign');
implementation project(':core:core-infra-qdsl');
implementation project(':core:core-infra-redis');
implementation project(':core:core-infra-s3');
Expand Down Expand Up @@ -37,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mm.api.domain.admin.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mm.api.common.response.CommonResponse;
import com.mm.api.domain.admin.service.AdminService;
import com.mm.api.domain.item.dto.response.ItemDetailResponse;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/v1/admin")
@RequiredArgsConstructor
public class AdminController {
private final AdminService adminService;

@PostMapping("/items/crawl")
public CommonResponse<ItemDetailResponse> crawlItem(@RequestParam String url) {
ItemDetailResponse response = adminService.crawlItem(url);
return CommonResponse.created(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mm.api.domain.admin.service;

import java.util.List;

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

import com.mm.api.domain.item.dto.response.ItemDetailResponse;
import com.mm.coredomain.domain.Item;
import com.mm.coredomain.domain.ItemImage;
import com.mm.coredomain.domain.ItemVideo;
import com.mm.coredomain.repository.ItemRepository;
import com.mm.coreinfrafeign.service.CrawlerService;

import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class AdminService {
private final ItemRepository itemRepository;
private final CrawlerService crawlerService;

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

private ItemDetailResponse getItemDetailResponseByItem(Item savedItem) {
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,7 @@
import com.mm.coredomain.domain.Item;
import com.mm.coredomain.domain.ItemCategoryType;

public record ItemCreateRequest(String detail,
public record ItemCreateRequest(String title,
String redirectUrl,
String categoryType,
Integer price,
Expand All @@ -17,7 +17,7 @@ public record ItemCreateRequest(String detail,
) {
public Item toEntity() {
return Item.builder()
.detail(detail)
.title(title)
.redirectUrl(redirectUrl)
.categoryType(ItemCategoryType.of(categoryType))
.price(price)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

public record ItemUpdateRequest(String detail,
public record ItemUpdateRequest(String title,
String redirectUrl,
String categoryType,
Integer price,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mm.coredomain.domain.ItemCategoryType;

public record ItemDetailResponse(Long id,
String detail,
String title,
String redirectUrl,
ItemCategoryType categoryType,
Integer price,
Expand All @@ -19,7 +19,7 @@ public record ItemDetailResponse(Long id,
public static ItemDetailResponse of(Item item, List<String> imageUrls, List<String> videoUrls, Boolean isDib) {
return new ItemDetailResponse(
item.getId(),
item.getDetail(),
item.getTitle(),
item.getRedirectUrl(),
item.getCategoryType(),
item.getPrice(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mm.coredomain.domain.ItemCategoryType;

public record ItemResponse(Long id,
String detail,
String title,
String redirectUrl,
ItemCategoryType categoryType,
Integer price,
Expand All @@ -15,7 +15,7 @@ public record ItemResponse(Long id,
public static ItemResponse of(Item item, Boolean isDib) {
return new ItemResponse(
item.getId(),
item.getDetail(),
item.getTitle(),
item.getRedirectUrl(),
item.getCategoryType(),
item.getPrice(),
Expand Down
Loading
Loading