Skip to content

Commit 01bbe66

Browse files
authored
[BACKEND] 상품 카테고리 검색어 변경 (#117)
1 parent 4fa2a3f commit 01bbe66

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

backend/src/main/java/com/cmg/comtogether/common/exception/ErrorCode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public enum ErrorCode {
5757
CERTIFICATION_ACCESS_DENIED(403, "CERTIFICATION-004", "해당 인증서에 대한 접근 권한이 없습니다."),
5858

5959
// 업로드
60-
INVALID_UPLOAD_TYPE(400,"UPLOAD-001", "올바르지 않은 업로드 타입입니다.");
60+
INVALID_UPLOAD_TYPE(400,"UPLOAD-001", "올바르지 않은 업로드 타입입니다."),
61+
62+
// 상품
63+
INVALID_PRODUCT_CATEGORY(400, "PRODUCT-001", "유효하지 않은 상품 카테고리입니다.");
6164

6265

6366
private final int status;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cmg.comtogether.product.entity;
2+
3+
import com.cmg.comtogether.common.exception.BusinessException;
4+
import com.cmg.comtogether.common.exception.ErrorCode;
5+
import lombok.Getter;
6+
7+
@Getter
8+
public enum ProductCategory {
9+
CPU("CPU", "CPU"),
10+
MAINBOARD("메인보드", "메인보드"),
11+
RAM("RAM", "RAM"),
12+
GRAPHICS_CARD("그래픽카드", "그래픽카드"),
13+
STORAGE("저장장치", "SSD"),
14+
POWER_SUPPLY("파워서플라이", "파워서플라이"),
15+
CASE("케이스", "PC 케이스"),
16+
COOLER("쿨러/팬", "CPU 쿨러"),
17+
IO_DEVICE("기타 입출력 장치", "키보드 마우스 모니터 스피커");
18+
19+
private final String displayName;
20+
private final String searchQuery;
21+
22+
ProductCategory(String displayName, String searchQuery) {
23+
this.displayName = displayName;
24+
this.searchQuery = searchQuery;
25+
}
26+
27+
public static ProductCategory fromDisplayName(String displayName) {
28+
for (ProductCategory category : values()) {
29+
if (category.displayName.equals(displayName)) {
30+
return category;
31+
}
32+
}
33+
throw new BusinessException(ErrorCode.INVALID_PRODUCT_CATEGORY);
34+
}
35+
}
36+

backend/src/main/java/com/cmg/comtogether/product/service/ProductService.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cmg.comtogether.common.exception.ErrorCode;
66
import com.cmg.comtogether.interest.entity.Interest;
77
import com.cmg.comtogether.product.dto.NaverProductResponseDto;
8+
import com.cmg.comtogether.product.entity.ProductCategory;
89
import com.cmg.comtogether.user.entity.User;
910
import com.cmg.comtogether.user.entity.UserInterest;
1011
import com.cmg.comtogether.user.repository.UserRepository;
@@ -21,8 +22,12 @@ public class ProductService {
2122
private final NaverProductService naverProductService;
2223

2324
public NaverProductResponseDto searchProducts(String category, String query, int display, int start, String sort, String exclude) {
24-
String searchQuery = category + " " + query;
25-
NaverProductResponseDto result = naverProductService.getNaverProducts(searchQuery, display, start, sort, exclude);
25+
ProductCategory productCategory;
26+
productCategory = ProductCategory.fromDisplayName(category);
27+
28+
String searchQuery = productCategory.getSearchQuery() + " " + query;
29+
30+
NaverProductResponseDto result = naverProductService.getNaverProducts(searchQuery.trim(), display, start, sort, exclude);
2631
cacheMonitorService.printCacheStats("naverProducts");
2732
return result;
2833
}
@@ -31,12 +36,16 @@ public NaverProductResponseDto recommendProducts(Long userId, String category, S
3136
User user = userRepository.findByIdWithInterests(userId)
3237
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
3338

39+
ProductCategory productCategory;
40+
productCategory = ProductCategory.fromDisplayName(category);
41+
3442
String interestString = user.getInterests().stream()
3543
.map(UserInterest::getInterest)
3644
.map(Interest::getName)
3745
.collect(Collectors.joining(" "));
38-
String searchQuery = category + " " + interestString + query;
39-
NaverProductResponseDto result = naverProductService.getNaverProducts(searchQuery, display, start, sort, exclude);
46+
String searchQuery = productCategory.getSearchQuery() + " " + interestString + " " + query;
47+
48+
NaverProductResponseDto result = naverProductService.getNaverProducts(searchQuery.trim(), display, start, sort, exclude);
4049
cacheMonitorService.printCacheStats("naverProducts");
4150
return result;
4251
}

0 commit comments

Comments
 (0)