Skip to content
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
11 changes: 11 additions & 0 deletions src/main/java/com/ureca/unity/domain/category/dto/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ureca.unity.domain.category.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Category {
private int categoryId;
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ureca.unity.domain.category.mapper;

import com.ureca.unity.domain.category.dto.Category;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface CategoryMapper {
List<Category> selectAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ureca.unity.domain.recommend.client;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Value;
import com.ureca.unity.domain.recommend.dto.RecommendRequest;
import com.ureca.unity.domain.recommend.dto.RecommendResponse;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class RecommendClient {

private final RestTemplate restTemplate;

@Value("${python.recommend.base-url:http://localhost:8000}")
private String baseUrl;
Comment thread
Chaejy marked this conversation as resolved.

public RecommendResponse recommend(RecommendRequest req) {
return restTemplate.postForObject(
baseUrl + "/api/v1/summary/" + req.getSummaryId() + "/recommend?k=" + req.getK(),
null,
RecommendResponse.class
);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ureca.unity.domain.recommend.controller;

import com.ureca.unity.domain.recommend.dto.RecommendResponse;
import com.ureca.unity.domain.recommend.service.RecommendService;
import com.ureca.unity.global.exception.CustomException;
import com.ureca.unity.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/recommend")
@RequiredArgsConstructor
public class RecommendController {

private static final Logger log = LoggerFactory.getLogger(RecommendController.class);

private final RecommendService recommendService;

// 요약 추천 생성 (FastAPI 호출 + DB 저장)
@PostMapping("/{summaryId}/generate")
public RecommendResponse generate(@PathVariable long summaryId) {
return recommendService.generateAndSave(summaryId);
}

// 요약 추천 조회 (DB 조회만)
@GetMapping("/{summaryId}")
public RecommendResponse get(@PathVariable long summaryId) {
try {
return recommendService.getFromDb(summaryId);
} catch (CustomException e) {
log.error("get failed for summaryId={}", summaryId, e);
throw e;
} catch (Exception e) {
log.error("unexpected error for summaryId={}", summaryId, e);
throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR);
}
}

// 전체 추천 조회
@GetMapping("/me")
public RecommendResponse getAll() {
// undefined 'currentUserId' 제거 — 서비스의 카테고리 기반 조회 재사용
return recommendService.getRandomByCategory();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ureca.unity.domain.recommend.dto;

import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class RecommendItem {
private long productId;
private int categoryId;
private double score;
private int rankNo;
private String name;
private Double price;
private String img;
private String link;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ureca.unity.domain.recommend.dto;

import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class RecommendRequest {
private long summaryId;
private int k = 5; //기본값
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ureca.unity.domain.recommend.dto;

import java.util.List;
import lombok.Getter;
import lombok.Setter;


@Getter @Setter
public class RecommendResponse {
private long summaryId;
private List<RecommendItem> items;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ureca.unity.domain.recommend.mapper;

import com.ureca.unity.domain.recommend.dto.RecommendItem;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface RecommendMapper {
void deleteItems(@Param("summaryId") long summaryId);
void insertItems(@Param("summaryId") long summaryId, @Param("items") List<RecommendItem> items);
List<RecommendItem> selectBySummaryId(@Param("summaryId") long summaryId);
List<RecommendItem> selectRandomByCategory(@Param("categoryId") int categoryId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.ureca.unity.domain.recommend.service;

import com.ureca.unity.domain.category.mapper.CategoryMapper;
import com.ureca.unity.domain.recommend.client.RecommendClient;
import com.ureca.unity.domain.recommend.dto.RecommendItem;
import com.ureca.unity.domain.recommend.dto.RecommendRequest;
import com.ureca.unity.domain.recommend.dto.RecommendResponse;
import com.ureca.unity.domain.recommend.mapper.RecommendMapper;
import com.ureca.unity.global.exception.CustomException;
import com.ureca.unity.global.exception.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.ureca.unity.domain.category.dto.Category;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class RecommendService {

private final RecommendClient recommendClient;
private final RecommendMapper recommendMapper;
private final CategoryMapper categoryMapper;
private static final Logger log = LoggerFactory.getLogger(RecommendService.class);

// 추천 생성 + 저장
@Transactional
public RecommendResponse generateAndSave(long summaryId) {
RecommendRequest req = new RecommendRequest();
req.setSummaryId(summaryId);
req.setK(5);

RecommendResponse response;

// FastAPI 호출
try {
response = recommendClient.recommend(req);
} catch (Exception e) {
throw new CustomException(ErrorCode.FASTAPI_CALL_FAILED);
}

if (response == null || response.getItems() == null || response.getItems().isEmpty()) {
throw new CustomException(ErrorCode.RECOMMEND_EMPTY);
}

// DB 삭제
try {
recommendMapper.deleteItems(summaryId);
} catch (Exception e) {
throw new CustomException(ErrorCode.RECOMMEND_DELETE_FAILED);
}

List<RecommendItem> items = response.getItems();
items.sort((a, b) -> Double.compare(b.getScore(), a.getScore()));
for (int i = 0; i < items.size(); i++) {
items.get(i).setRankNo(i); // 0,1,2,... 순서대로
}

// DB 삽입
try {
recommendMapper.insertItems(summaryId, items);
} catch (Exception e) {
throw new CustomException(ErrorCode.RECOMMEND_INSERT_FAILED);
}

return response;
}

// DB 조회만
public RecommendResponse getFromDb(long summaryId) {
List<RecommendItem> items;
try {
items = recommendMapper.selectBySummaryId(summaryId);
log.debug("DB 조회 결과: {}", items);
} catch (Exception e) {
log.error("DB 조회 실패 summaryId={}", summaryId, e);
throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR);
}

if (items == null || items.isEmpty()) {
throw new CustomException(ErrorCode.RECOMMEND_EMPTY);
}

RecommendResponse response = new RecommendResponse();
response.setSummaryId(summaryId);
response.setItems(items);
return response;
}

@Transactional(readOnly = true)
public RecommendResponse getRandomByCategory() {
List<Category> categories = categoryMapper.selectAll();
List<RecommendItem> results = new ArrayList<>();

for (Category c : categories) {
List<RecommendItem> fallback = recommendMapper.selectRandomByCategory(c.getCategoryId());
results.addAll(fallback);
}

RecommendResponse response = new RecommendResponse();
response.setSummaryId(0);
response.setItems(results);
return response;
}


// public List<RecommendItem> getRecommendationsWithFallback(long summaryId) {
// List<RecommendItem> items = recommendMapper.selectBySummaryId(summaryId);
//
// // 추천 없으면 빈 리스트 처리
// if (items == null) {
// items = new ArrayList<>();
// }
//
// // 카테고리별로 그룹화
// Map<Integer, List<RecommendItem>> byCategory = items.stream()
// .collect(Collectors.groupingBy(RecommendItem::getCategoryId));
//
// List<Category> categories = categoryMapper.selectAll();
// for (Category c : categories) {
// if (!byCategory.containsKey(c.getCategoryId()) || byCategory.get(c.getCategoryId()).isEmpty()) {
// // 추천 없으면 fallback
// List<RecommendItem> fallback = recommendMapper.selectRandomByCategory(c.getCategoryId());
// items.addAll(fallback);
// }
// }
//
// // score=0, rankNo=0인 fallback도 이미 Mapper에서 설정됨
// return items;
// }
}
63 changes: 32 additions & 31 deletions src/main/java/com/ureca/unity/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
package com.ureca.unity.global.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public enum ErrorCode {

TOKEN_MISSING(HttpStatus.UNAUTHORIZED, "토큰이 없습니다."),
TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "로그인이 만료되었습니다."),
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다."),
TOKEN_MISSING(HttpStatus.UNAUTHORIZED, "토큰이 없습니다."),
TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "로그인이 만료되었습니다."),
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다."),

REFRESH_TOKEN_MISSING(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 없습니다."),
REFRESH_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "유효하지 않은 리프레시 토큰입니다."),
REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다."),
REFRESH_TOKEN_MISSING(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 없습니다."),
REFRESH_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "유효하지 않은 리프레시 토큰입니다."),
REFRESH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다."),

INVALID_OAUTH_PROVIDER(HttpStatus.BAD_REQUEST,"지원하지 않는 OAuth 제공자입니다."),
INVALID_OAUTH_PROVIDER(HttpStatus.BAD_REQUEST,"지원하지 않는 OAuth 제공자입니다."),

USER_NOT_FOUND(HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다."),
USER_ALREADY_DELETED(HttpStatus.BAD_REQUEST, "이미 탈퇴한 사용자입니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다."),
USER_ALREADY_DELETED(HttpStatus.BAD_REQUEST, "이미 탈퇴한 사용자입니다."),

// 기타 공용 exception code
INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "입력값이 잘못되었습니다."),
INVALID_TYPE_VALUE(HttpStatus.BAD_REQUEST, "데이터 타입이 일치하지 않습니다."),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않은 메소드입니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "대상을 찾을 수 없습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 로직 오류입니다."),
FASTAPI_CALL_FAILED(HttpStatus.BAD_GATEWAY, "추천 서버 호출 실패"),
RECOMMEND_DELETE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "기존 추천 삭제 실패"),
RECOMMEND_INSERT_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "추천 결과 저장 실패"),
RECOMMEND_EMPTY(HttpStatus.NOT_FOUND, "추천 결과가 없습니다"),
RECOMMEND_RETRIEVE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "추천 결과 조회 실패"),

OAUTH_TOKEN_NOT_FOUND(HttpStatus.BAD_REQUEST, "소셜 토큰이 없어 연결 해제를 진행할 수 없습니다. 다시 로그인 후 탈퇴해주세요."),
SOCIAL_UNLINK_FAILED(HttpStatus.BAD_GATEWAY, "소셜 연결 해제에 실패했습니다. 잠시 후 다시 시도해주세요."),
// 기타 공용 exception code
INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "입력값이 잘못되었습니다."),
INVALID_TYPE_VALUE(HttpStatus.BAD_REQUEST, "데이터 타입이 일치하지 않습니다."),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않은 메소드입니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "대상을 찾을 수 없습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 로직 오류입니다."),

AUTH_STORAGE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "소셜 로그인 정보 저장에 실패했습니다. 잠시 후 다시 시도해주세요.");
OAUTH_TOKEN_NOT_FOUND(HttpStatus.BAD_REQUEST, "소셜 토큰이 없어 연결 해제를 진행할 수 없습니다. 다시 로그인 후 탈퇴해주세요."),
SOCIAL_UNLINK_FAILED(HttpStatus.BAD_GATEWAY, "소셜 연결 해제에 실패했습니다. 잠시 후 다시 시도해주세요."),

private final HttpStatus status;
private final String message;
AUTH_STORAGE_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "소셜 로그인 정보 저장에 실패했습니다. 잠시 후 다시 시도해주세요.");

ErrorCode(HttpStatus status, String message) {
this.status = status;
this.message = message;
}
private final HttpStatus status;
private final String message;

public HttpStatus getStatus() {
return status;
}
ErrorCode(HttpStatus status, String message) {
this.status = status;
this.message = message;
}

public String getMessage() {
return message;
}
}
}
12 changes: 12 additions & 0 deletions src/main/resources/mapper/category/CategoryMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ureca.unity.domain.category.mapper.CategoryMapper">
<select id="selectAll" resultType="com.ureca.unity.domain.category.dto.Category">
SELECT category_id AS categoryId, name FROM category
ORDER BY category_id
</select>
</mapper>

Loading