Skip to content

Commit

Permalink
feat(#125) : 거래내역 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yeon015 committed Aug 12, 2023
1 parent 4133b71 commit ec81c33
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +19,7 @@ public class LikeProductController {

/* 좋아요 저장 */
@PostMapping("")
public BaseResponse<LikeResDto> saveLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) {
public BaseResponse<LikeResDto> saveLike(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}
Expand All @@ -30,7 +30,7 @@ public BaseResponse<LikeResDto> saveLike(@RequestBody LikeProductReqDto req, @Au

/* 좋아요 삭제 */
@DeleteMapping("")
public BaseResponse<LikeResDto> deleteLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) {
public BaseResponse<LikeResDto> deleteLike(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/trothly/trothcam/domain/history/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,6 +14,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@Table(name = "history")
public class History {

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<History, Long> {

List<History> findAllByProductId(Long productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface LikeProductRepository extends JpaRepository<LikeProduct, Long>

Optional<LikeProduct> findByProductIdAndMemberId(Long productId, Long memberId);

Long countById(Long id);
Long countByProductId(Long productId);
}
34 changes: 34 additions & 0 deletions src/main/java/trothly/trothcam/dto/web/ProductDetailResDto.java
Original file line number Diff line number Diff line change
@@ -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<History> histories;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class LikeProductReqDto {
public class ProductReqDto {
private Long productId;
}
3 changes: 3 additions & 0 deletions src/main/java/trothly/trothcam/exception/base/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),


/**
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/trothly/trothcam/service/web/HistoryService.java
Original file line number Diff line number Diff line change
@@ -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<History> findAllHistory(ProductReqDto req) {
List<History> findHistories = historyRepository.findAllByProductId(req.getProductId());
if(findHistories == null || findHistories.isEmpty()) {
throw new BaseException(HISTORIES_NOT_FOUND);
}

return findHistories;
}

// 거래 내역 저장
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<LikeProduct> 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(
Expand All @@ -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());
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +31,8 @@
@RequiredArgsConstructor
public class ProductService {

// private final ProductRepository productRepository;
private final ProductRepository productRepository;

//
// /* 공개 인증서 조회 */
// @Transactional(readOnly = true)
Expand All @@ -36,4 +43,5 @@ public class ProductService {
//
// return findProducts;
// }

}

0 comments on commit ec81c33

Please sign in to comment.