Skip to content

Commit

Permalink
feat(#125) : product detail api 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
yeon015 committed Aug 16, 2023
1 parent 7f21db4 commit 6ba34a3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
5 changes: 5 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.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import trothly.trothcam.domain.member.Member;
Expand All @@ -15,6 +17,8 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@DynamicInsert
@DynamicUpdate
@Table(name = "history")
public class History {

Expand All @@ -40,4 +44,5 @@ public class History {
@CreatedDate
@Column(name = "sold_at", updatable = false, nullable = false)
private LocalDateTime soldAt;

}
4 changes: 2 additions & 2 deletions src/main/java/trothly/trothcam/domain/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class Image extends BaseTimeEntity {
private Member member;

@Column(name = "image_share", nullable = false)
@ColumnDefault("N")
@Enumerated(EnumType.STRING)
private Share share;

Expand All @@ -50,8 +49,9 @@ public class Image extends BaseTimeEntity {
@Column(name = "image_size", nullable = true)
private String size;

public Image(String imageHash, Member member) {
public Image(String imageHash, Member member, Share share) {
this.imageHash = imageHash;
this.member = member;
this.share = share;
}
}
17 changes: 17 additions & 0 deletions src/main/java/trothly/trothcam/dto/web/HistoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package trothly.trothcam.dto.web;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class HistoryDto {
private Long historyId;
private Long productId;
private Long sellerId;
private Long buyerId;
private Long price;
private LocalDateTime soldAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import trothly.trothcam.domain.history.History;
import trothly.trothcam.domain.product.PublicYn;

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

Expand All @@ -28,6 +29,6 @@ public class ProductDetailResDto {
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private boolean liked;
private List<History> histories;
private List<HistoryDto> histories;

}
3 changes: 2 additions & 1 deletion src/main/java/trothly/trothcam/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.transaction.annotation.Transactional;
import trothly.trothcam.domain.image.Image;
import trothly.trothcam.domain.image.ImageRepository;
import trothly.trothcam.domain.image.Share;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.app.CheckImgHashResDto;
import trothly.trothcam.dto.app.ImgHashReqDto;
Expand All @@ -31,7 +32,7 @@ public SaveImgHashResDto saveImgHash(ImgHashReqDto req, Member member) throws Ba
throw new BadRequestException("이미 존재하는 해시 값입니다.");
}

Image image = imageRepository.save(new Image(req.getImageHash(), member));
Image image = imageRepository.save(new Image(req.getImageHash(), member, Share.N));

return new SaveImgHashResDto(image.getId());
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/trothly/trothcam/service/web/HistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import trothly.trothcam.domain.history.History;
import trothly.trothcam.domain.history.HistoryRepository;
import trothly.trothcam.domain.product.ProductRepository;
import trothly.trothcam.dto.web.HistoryDto;
import trothly.trothcam.dto.web.ProductReqDto;
import trothly.trothcam.exception.base.BaseException;

import java.util.ArrayList;
import java.util.List;

import static trothly.trothcam.exception.base.ErrorCode.HISTORIES_NOT_FOUND;
Expand All @@ -22,13 +24,20 @@ public class HistoryService {
private final ProductRepository productRepository;

// 거래 내역 전체 조회
public List<History> findAllHistory(ProductReqDto req) {
public List<HistoryDto> findAllHistory(ProductReqDto req) {
List<History> findHistories = historyRepository.findAllByProductId(req.getProductId());
if(findHistories == null || findHistories.isEmpty()) {
throw new BaseException(HISTORIES_NOT_FOUND);
// if(findHistories == null || findHistories.isEmpty()) {
// throw new BaseException(HISTORIES_NOT_FOUND);
// }

List<HistoryDto> historyDto = new ArrayList<>();

for (History history : findHistories) {
HistoryDto historyOne = new HistoryDto(history.getId(), history.getProduct().getId(), history.getSeller().getId(), history.getBuyer().getId(), history.getPrice(), history.getSoldAt());
historyDto.add(historyOne);
}

return findHistories;
return historyDto;
}

// 거래 내역 저장
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import trothly.trothcam.domain.product.Product;
import trothly.trothcam.domain.product.ProductRepository;
import trothly.trothcam.domain.product.PublicYn;
import trothly.trothcam.dto.web.HistoryDto;
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;
import trothly.trothcam.exception.custom.BadRequestException;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -34,6 +36,7 @@ public class ProductService {
private final HistoryRepository historyRepository;
private final LikeProductRepository likeProductRepository;
private final ImageRepository imageRepository;
private final HistoryService historyService;

/* 공개 인증서 조회 */
@Transactional(readOnly = true)
Expand All @@ -54,7 +57,7 @@ public List<ProductsResDto> findPublicProducts(String webId) throws BaseExceptio
}

/* 상품 detail 화면 조회 */
@Transactional(readOnly = true)
@Transactional
public ProductDetailResDto findProductDetail(ProductReqDto req, Member member) {
Boolean liked = false;

Expand All @@ -76,10 +79,10 @@ public ProductDetailResDto findProductDetail(ProductReqDto req, Member member) {
liked = false;
}

List<History> histories = historyRepository.findAllByProductId(req.getProductId());
List<HistoryDto> historyDto = historyService.findAllHistory(req);

return new ProductDetailResDto(req.getProductId(), product.getImage().getId(), product.getMember().getId(), product.getTitle(),
product.getTags(), product.getPrice(), product.getDescription(),product.getViews(), likes, product.getPublicYn(), product.getCreatedAt(),
product.getLastModifiedAt(), liked, histories);
product.getLastModifiedAt(), liked, historyDto);
}
}
}

0 comments on commit 6ba34a3

Please sign in to comment.