diff --git a/src/main/java/trothly/trothcam/controller/web/HistoryController.java b/src/main/java/trothly/trothcam/controller/web/HistoryController.java new file mode 100644 index 0000000..192a8c5 --- /dev/null +++ b/src/main/java/trothly/trothcam/controller/web/HistoryController.java @@ -0,0 +1,11 @@ +package trothly.trothcam.controller.web; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/history") +public class HistoryController { +} diff --git a/src/main/java/trothly/trothcam/controller/web/LikeProductController.java b/src/main/java/trothly/trothcam/controller/web/LikeProductController.java index e215ba5..2bd1cba 100644 --- a/src/main/java/trothly/trothcam/controller/web/LikeProductController.java +++ b/src/main/java/trothly/trothcam/controller/web/LikeProductController.java @@ -4,7 +4,7 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import trothly.trothcam.domain.member.Member; -import trothly.trothcam.dto.web.LikeProductReqDto; +import trothly.trothcam.dto.web.ProductReqDto; import trothly.trothcam.dto.web.LikeResDto; import trothly.trothcam.exception.base.BaseResponse; import trothly.trothcam.exception.custom.BadRequestException; @@ -19,7 +19,7 @@ public class LikeProductController { /* 좋아요 저장 */ @PostMapping("") - public BaseResponse saveLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) { + public BaseResponse saveLike(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) { if(req.getProductId() == null) { throw new BadRequestException("존재하지 않는 상품 아이디 입니다."); } @@ -30,7 +30,7 @@ public BaseResponse saveLike(@RequestBody LikeProductReqDto req, @Au /* 좋아요 삭제 */ @DeleteMapping("") - public BaseResponse deleteLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) { + public BaseResponse deleteLike(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) { if(req.getProductId() == null) { throw new BadRequestException("존재하지 않는 상품 아이디 입니다."); } diff --git a/src/main/java/trothly/trothcam/domain/history/History.java b/src/main/java/trothly/trothcam/domain/history/History.java index 32b807d..a98b27a 100644 --- a/src/main/java/trothly/trothcam/domain/history/History.java +++ b/src/main/java/trothly/trothcam/domain/history/History.java @@ -3,6 +3,8 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; import trothly.trothcam.domain.member.Member; import trothly.trothcam.domain.product.Product; @@ -12,6 +14,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) +@EntityListeners(AuditingEntityListener.class) @Table(name = "history") public class History { @@ -34,6 +37,7 @@ public class History { @Column(name = "price", nullable = false) private Long price; + @CreatedDate @Column(name = "sold_at", updatable = false, nullable = false) private LocalDateTime soldAt; } diff --git a/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java b/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java new file mode 100644 index 0000000..5c4bc10 --- /dev/null +++ b/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java @@ -0,0 +1,12 @@ +package trothly.trothcam.domain.history; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface HistoryRepository extends JpaRepository { + + List findAllByProductId(Long productId); +} diff --git a/src/main/java/trothly/trothcam/domain/like/LikeProductRepository.java b/src/main/java/trothly/trothcam/domain/like/LikeProductRepository.java index 86db10f..35c6edd 100644 --- a/src/main/java/trothly/trothcam/domain/like/LikeProductRepository.java +++ b/src/main/java/trothly/trothcam/domain/like/LikeProductRepository.java @@ -10,5 +10,5 @@ public interface LikeProductRepository extends JpaRepository Optional findByProductIdAndMemberId(Long productId, Long memberId); - Long countById(Long id); + Long countByProductId(Long productId); } diff --git a/src/main/java/trothly/trothcam/dto/web/ProductDetailResDto.java b/src/main/java/trothly/trothcam/dto/web/ProductDetailResDto.java new file mode 100644 index 0000000..740eaf5 --- /dev/null +++ b/src/main/java/trothly/trothcam/dto/web/ProductDetailResDto.java @@ -0,0 +1,34 @@ +package trothly.trothcam.dto.web; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import trothly.trothcam.domain.history.History; +import trothly.trothcam.domain.product.Public; + +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class ProductDetailResDto { + + private Long productId; + private Long imageId; + private Long ownerId; + private String title; + private int tags; + private Long price; + private String description; + private int views; + private Long likes; + private Public publicYN; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + private boolean liked; + private List histories; + +} diff --git a/src/main/java/trothly/trothcam/dto/web/LikeProductReqDto.java b/src/main/java/trothly/trothcam/dto/web/ProductReqDto.java similarity index 88% rename from src/main/java/trothly/trothcam/dto/web/LikeProductReqDto.java rename to src/main/java/trothly/trothcam/dto/web/ProductReqDto.java index ef484b0..c0ae49d 100644 --- a/src/main/java/trothly/trothcam/dto/web/LikeProductReqDto.java +++ b/src/main/java/trothly/trothcam/dto/web/ProductReqDto.java @@ -8,6 +8,6 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor -public class LikeProductReqDto { +public class ProductReqDto { private Long productId; } diff --git a/src/main/java/trothly/trothcam/exception/base/ErrorCode.java b/src/main/java/trothly/trothcam/exception/base/ErrorCode.java index 9cb1545..58c07fc 100644 --- a/src/main/java/trothly/trothcam/exception/base/ErrorCode.java +++ b/src/main/java/trothly/trothcam/exception/base/ErrorCode.java @@ -28,6 +28,9 @@ public enum ErrorCode { // BaseResponseStatus와 같은 역할 INVALID_PROVIDER(2010, "잘못된 PROVIDER 입니다.", BAD_REQUEST), TOKEN_EXPIRED(2011, "토큰 유효시간이 만료되었습니다.", BAD_REQUEST), ALREADY_LOGOUT(2012, "이미 로그아웃 되었습니다.", BAD_REQUEST), + ALREADY_LIKED(2013, "이미 좋아요를 누른 상품입니다.",BAD_REQUEST), + NOT_LIKED(2014, "좋아요를 누르지 않은 상품입니다.", BAD_REQUEST), + HISTORIES_NOT_FOUND(2015, "거래 내역이 존재하지 않습니다.", BAD_REQUEST), /** diff --git a/src/main/java/trothly/trothcam/service/web/HistoryService.java b/src/main/java/trothly/trothcam/service/web/HistoryService.java new file mode 100644 index 0000000..bb257e6 --- /dev/null +++ b/src/main/java/trothly/trothcam/service/web/HistoryService.java @@ -0,0 +1,35 @@ +package trothly.trothcam.service.web; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import trothly.trothcam.domain.history.History; +import trothly.trothcam.domain.history.HistoryRepository; +import trothly.trothcam.domain.product.ProductRepository; +import trothly.trothcam.dto.web.ProductReqDto; +import trothly.trothcam.exception.base.BaseException; + +import java.util.List; + +import static trothly.trothcam.exception.base.ErrorCode.HISTORIES_NOT_FOUND; + +@Service +@Transactional +@RequiredArgsConstructor +public class HistoryService { + + private final HistoryRepository historyRepository; + private final ProductRepository productRepository; + + // 거래 내역 전체 조회 + public List findAllHistory(ProductReqDto req) { + List findHistories = historyRepository.findAllByProductId(req.getProductId()); + if(findHistories == null || findHistories.isEmpty()) { + throw new BaseException(HISTORIES_NOT_FOUND); + } + + return findHistories; + } + + // 거래 내역 저장 +} diff --git a/src/main/java/trothly/trothcam/service/web/LikeProductService.java b/src/main/java/trothly/trothcam/service/web/LikeProductService.java index 77cc6d7..82eda35 100644 --- a/src/main/java/trothly/trothcam/service/web/LikeProductService.java +++ b/src/main/java/trothly/trothcam/service/web/LikeProductService.java @@ -8,13 +8,16 @@ import trothly.trothcam.domain.member.Member; import trothly.trothcam.domain.product.Product; import trothly.trothcam.domain.product.ProductRepository; -import trothly.trothcam.dto.web.LikeProductReqDto; +import trothly.trothcam.dto.web.ProductReqDto; import trothly.trothcam.dto.web.LikeResDto; import trothly.trothcam.exception.base.BaseException; import trothly.trothcam.exception.custom.BadRequestException; import java.util.Optional; +import static trothly.trothcam.exception.base.ErrorCode.ALREADY_LIKED; +import static trothly.trothcam.exception.base.ErrorCode.NOT_LIKED; + @Service @Transactional @RequiredArgsConstructor @@ -24,11 +27,11 @@ public class LikeProductService { private final ProductRepository productRepository; // 좋아요 저장 - public LikeResDto saveLike(LikeProductReqDto req, Member member) throws BaseException { + public LikeResDto saveLike(ProductReqDto req, Member member) { Optional like = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()); if(like.isPresent()) { - throw new BadRequestException("이미 좋아요를 누른 상품입니다."); + throw new BaseException(ALREADY_LIKED); } Product product = productRepository.findById(req.getProductId()).orElseThrow( @@ -41,9 +44,9 @@ public LikeResDto saveLike(LikeProductReqDto req, Member member) throws BaseExce } // 좋아요 삭제 - public LikeResDto deleteLike(LikeProductReqDto req, Member member) throws BaseException { + public LikeResDto deleteLike(ProductReqDto req, Member member) { LikeProduct likeProduct = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()).orElseThrow( - () -> new BadRequestException("좋아요를 누르지 않은 상품입니다.") + () -> new BaseException(NOT_LIKED) ); likeProductRepository.deleteById(likeProduct.getId()); diff --git a/src/main/java/trothly/trothcam/service/web/ProductService.java b/src/main/java/trothly/trothcam/service/web/ProductService.java index e2383d6..a64aad8 100644 --- a/src/main/java/trothly/trothcam/service/web/ProductService.java +++ b/src/main/java/trothly/trothcam/service/web/ProductService.java @@ -3,13 +3,19 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import trothly.trothcam.domain.history.History; +import trothly.trothcam.domain.history.HistoryRepository; import trothly.trothcam.domain.image.Image; +import trothly.trothcam.domain.like.LikeProduct; +import trothly.trothcam.domain.like.LikeProductRepository; import trothly.trothcam.domain.member.Member; import trothly.trothcam.domain.product.Product; import trothly.trothcam.domain.product.ProductRepository; import trothly.trothcam.dto.app.CheckImgHashResDto; import trothly.trothcam.dto.auth.web.CheckIdResDto; import trothly.trothcam.dto.auth.web.ValidateWebTokenResDto; +import trothly.trothcam.dto.web.ProductDetailResDto; +import trothly.trothcam.dto.web.ProductReqDto; import trothly.trothcam.dto.web.ProductsResDto; import trothly.trothcam.exception.base.BaseException; import trothly.trothcam.exception.base.ErrorCode; @@ -25,7 +31,8 @@ @RequiredArgsConstructor public class ProductService { -// private final ProductRepository productRepository; + private final ProductRepository productRepository; + // // /* 공개 인증서 조회 */ // @Transactional(readOnly = true) @@ -36,4 +43,5 @@ public class ProductService { // // return findProducts; // } + }