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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ttakkeun.ttakkeun_server.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ttakkeun.ttakkeun_server.dto.ProductApiResponseDTO;
import ttakkeun.ttakkeun_server.dto.ProductDTO;
import ttakkeun.ttakkeun_server.service.ProductService;

import java.util.List;

@RestController
@RequestMapping("/api/product")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;

//ai 추천 제품
@GetMapping("/ai")
public ResponseEntity<ProductApiResponseDTO> getAiProducts() {
try {
List<ProductDTO> products = productService.getResultProducts();
ProductApiResponseDTO response = ProductApiResponseDTO.builder()
.isSuccess(true)
.code(200)
.message("성공")
.result(products)
.build();

return ResponseEntity.ok(response);
} catch (Exception e) {
ProductApiResponseDTO response = ProductApiResponseDTO.builder()
.isSuccess(false)
.code(500)
.message("에러: " + e.getMessage())
.build();

return ResponseEntity.status(500).body(response);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ttakkeun.ttakkeun_server.converter;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import ttakkeun.ttakkeun_server.dto.ProductDTO;
import ttakkeun.ttakkeun_server.entity.Product;
import ttakkeun.ttakkeun_server.service.LikeService;

@Component
@RequiredArgsConstructor
public class ProductConverter {
private final LikeService likeService;

//Product를 ProductDTO로 convert
public ProductDTO toDTO(Product product) {
Long productId = product.getProductId();
//임시 멤버값
Integer totalLikes = likeService.getTotalLikes(productId);
boolean isLike = likeService.getLikeStatus(productId);

return ProductDTO.builder()
.product_id(product.getProductId())
.title(product.getProductTitle())
.image(product.getProductImage())
.price(product.getLprice())
.brand(product.getBrand())
.link(product.getProductLink())
.category1(product.getCategory1())
.category2(product.getCategory2())
.category3(product.getCategory3())
.category4(product.getCategory4())
.total_likes(totalLikes)
.isLike(isLike)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ttakkeun.ttakkeun_server.dto;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
public class ProductApiResponseDTO {
private Boolean isSuccess;
private int code;
private String message;
private List<ProductDTO> result;
}
21 changes: 21 additions & 0 deletions src/main/java/ttakkeun/ttakkeun_server/dto/ProductDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ttakkeun.ttakkeun_server.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ProductDTO {
private Long product_id;
private String title;
private String image;
private Integer price;
private String brand;
private String link;
private String category1;
private String category2;
private String category3;
private String category4;
private Integer total_likes;
private Boolean isLike;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ttakkeun.ttakkeun_server.repository;

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import ttakkeun.ttakkeun_server.entity.Member;
import ttakkeun.ttakkeun_server.entity.Product;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class LikeRepository {
private final EntityManager em;

//멤버와 연관된 제품들 반환(특정 사용자의 좋아요 목록)
public List<Product> findProductsByMemberId(Long memberId) {
return em.createQuery("SELECT lp.product FROM LikeProduct lp WHERE lp.member.memberId = :memberId", Product.class)
.setParameter("memberId", memberId)
.getResultList();
}

//제품과 연관된 멤버들 반환(특정 제품에 좋아요한 사용자 목록)
public List<Member> findMembersByProductId(Long productId) {
return em.createQuery("SELECT lp.member FROM LikeProduct lp WHERE lp.product.productId = :productId", Member.class)
.setParameter("productId", productId)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ttakkeun.ttakkeun_server.repository;

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import ttakkeun.ttakkeun_server.entity.Product;

import java.time.LocalDateTime;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class ProductRepository {
private final EntityManager em;

//제품id로 제품 불러오기
public Product findById(Long id) {
return em.find(Product.class, id);
}

//진단결과 id를 이용해 진단제품 불러오기
public List<Product> findByResultId(Long resultId) {
return em.createQuery("SELECT p FROM Product p WHERE p.result.resultId = :resultId", Product.class)
.setParameter("resultId", resultId)
.setMaxResults(5)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ttakkeun.ttakkeun_server.repository;

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class ResultRepository {

private final EntityManager em;

//가장 최근 진단결과 id불러오기
public Long findLatestResultId() {
return em.createQuery("SELECT r.id FROM Result r ORDER BY createdAt desc", Long.class)
.setMaxResults(1)
.getSingleResult();
}
}
33 changes: 33 additions & 0 deletions src/main/java/ttakkeun/ttakkeun_server/service/LikeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ttakkeun.ttakkeun_server.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.relational.core.sql.Like;
import org.springframework.stereotype.Service;
import ttakkeun.ttakkeun_server.entity.Product;
import ttakkeun.ttakkeun_server.repository.LikeRepository;
import ttakkeun.ttakkeun_server.repository.ProductRepository;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class LikeService {
private final LikeRepository likeRepository;
private final ProductRepository productRepository;

//제품의 좋아요 수 반환
public int getTotalLikes(Long productId) {
return likeRepository.findMembersByProductId(productId).size();
}

//사용자의 좋아요 판단
public Boolean getLikeStatus(Long productId) {

//임시 멤버값
Long memberId = 526L;
Product targetProduct = productRepository.findById(productId);
return likeRepository.findProductsByMemberId(memberId).contains(targetProduct);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ttakkeun.ttakkeun_server.entity.Member;
import ttakkeun.ttakkeun_server.repository.MemberRepository;

import java.util.Optional;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;

public Optional<Member> findMemberById(Long id) {
return memberRepository.findById(id);
}
}
31 changes: 31 additions & 0 deletions src/main/java/ttakkeun/ttakkeun_server/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ttakkeun.ttakkeun_server.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ttakkeun.ttakkeun_server.converter.ProductConverter;
import ttakkeun.ttakkeun_server.dto.ProductDTO;
import ttakkeun.ttakkeun_server.entity.Product;
import ttakkeun.ttakkeun_server.repository.ProductRepository;
import ttakkeun.ttakkeun_server.repository.ResultRepository;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class ProductService {
private final ProductRepository productRepository;
private final ResultRepository resultRepository;
private final LikeService likeService;
private final ProductConverter productConverter;

//진단의 ai추천제품
public List<ProductDTO> getResultProducts() {
Long latestResultId = resultRepository.findLatestResultId();
List<Product> products = productRepository.findByResultId(latestResultId);

return products.stream().map(productConverter::toDTO).collect(Collectors.toList());
}
}
Loading