Skip to content

Commit

Permalink
Merge pull request #147 from Troth-Cam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
aeeazip authored Aug 16, 2023
2 parents 7798bd0 + 09efe96 commit 921944a
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package trothly.trothcam.controller.web;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.ProductDetailResDto;
import trothly.trothcam.dto.web.certificate.ProductDto;
import trothly.trothcam.dto.web.certificate.PublicResDto;
import trothly.trothcam.exception.base.BaseResponse;
import trothly.trothcam.service.web.CertificateService;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/products")
public class CertificateController {

private final CertificateService certificateService;

// 공개 인증서 비공개 인증서로 변환 (비공개하기[판매취소] 클릭 시)
@PatchMapping("/{productId}/convert-to-private")
public BaseResponse<List<ProductDto>> getPrivateCertificates(@AuthenticationPrincipal Member member, @PathVariable Long productId) {
List<ProductDto> getPrivateCertificates = certificateService.getPrivateCertificates(member, productId);
return BaseResponse.onSuccess(getPrivateCertificates);
}

// 비공개 인증서 공개 인증서로 변환 (온라인에 게시하기[판매하기] 버튼 클릭 시)
@PatchMapping("/{productId}/convert-to-public")
public BaseResponse<String> updatePublicCertificates(@AuthenticationPrincipal Member member, @PathVariable Long productId, @RequestBody PublicResDto publicResDto) {
String result = certificateService.updatePublicCertificates(member, productId, publicResDto);
return BaseResponse.onSuccess(result);
}

// 비공개 인증서 product detail 조회

// 공개 인증서 product detail 조회
// @GetMapping("/public/{productId}")
// public BaseResponse<ProductDetailResDto> getPublicProductDetail(@PathVariable Long productId) {
// ProductDetailResDto getPublicProductDetail = certificateService.getPublicProductDetail(productId);
// return BaseResponse.onSuccess(getPublicProductDetail);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.domain.product.Product;

import java.util.Optional;

Expand All @@ -11,4 +13,6 @@ public interface LikeProductRepository extends JpaRepository<LikeProduct, Long>
Optional<LikeProduct> findByProductIdAndMemberId(Long productId, Long memberId);

Long countByProductId(Long productId);

Optional<LikeProduct> findByProductAndMember(Product product, Member member);
}
13 changes: 10 additions & 3 deletions src/main/java/trothly/trothcam/domain/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import trothly.trothcam.domain.core.BaseTimeEntity;
import trothly.trothcam.domain.image.Image;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.certificate.PublicResDto;

import javax.persistence.*;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -53,10 +54,16 @@ public class Product extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private PublicYn publicYn;

@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;

public void updateViews(int views) {
this.views = views;
}

public void updatePublicYn(PublicYn publicYn) {
this.publicYn = publicYn;
}

public void updateInfo(PublicResDto publicResDto) {
this.price = publicResDto.getPrice();
this.description = publicResDto.getDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.ProductRankDto;

import java.util.List;

import static trothly.trothcam.domain.product.PublicYn.N;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

Expand All @@ -19,4 +22,7 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
"order by price desc, sold_at asc\n" +
"LIMIT 10", nativeQuery = true)
List<ProductRankDto> findProductRandDto();

// 비공개 인증서 리스트 조회
List<Product> findAllByMemberAndIdAndPublicYn(Member member, Long productId, PublicYn publicYn);
}
25 changes: 25 additions & 0 deletions src/main/java/trothly/trothcam/dto/web/certificate/ProductDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package trothly.trothcam.dto.web.certificate;

import lombok.Builder;
import trothly.trothcam.domain.product.Product;

import java.time.LocalDateTime;

public class ProductDto {
private Long productId;
private String title;
private String webToken;
private Long price;
private boolean isLiked;
private LocalDateTime updatedAt;

@Builder
public ProductDto(Product product, String webToken, boolean isLiked) {
this.productId = product.getId();
this.title = product.getTitle();
this.webToken = webToken;
this.price = product.getPrice();
this.isLiked = isLiked;
this.updatedAt = product.getLastModifiedAt();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package trothly.trothcam.dto.web.certificate;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PublicResDto {
private Long price;
private String description;
}
75 changes: 75 additions & 0 deletions src/main/java/trothly/trothcam/service/web/CertificateService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package trothly.trothcam.service.web;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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.domain.product.PublicYn;
import trothly.trothcam.dto.web.ProductDetailResDto;
import trothly.trothcam.dto.web.ProductsResDto;
import trothly.trothcam.dto.web.certificate.ProductDto;
import trothly.trothcam.dto.web.certificate.PublicResDto;
import trothly.trothcam.exception.base.BaseException;
import trothly.trothcam.exception.base.ErrorCode;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CertificateService {
private final ProductRepository productRepository;
private final LikeProductRepository likeProductRepository;

// 공개 인증서 비공개 인증서로 변환 (비공개하기[판매취소] 클릭 시)
public List<ProductDto> getPrivateCertificates(Member member, Long productId) {
Optional<Product> getProduct = productRepository.findById(productId);
if(getProduct.isEmpty())
throw new BaseException(ErrorCode.PRODUCT_NOT_FOUND);

Product product = getProduct.get();
product.updatePublicYn(PublicYn.N); // 1. 해당 인증서 비공개로 전환
productRepository.save(product);

List<Product> productList = productRepository.findAllByMemberAndIdAndPublicYn(member, productId, PublicYn.N); // 2. 비공개 리스트 조회
List<ProductDto> productDtoList = productList.stream()
.map(p -> {
Member getMember = p.getMember();
String webToken = getMember.getWebToken(); // 1. webToken 가져오기

// 2. 좋아요 눌렀는지 체크
Optional<LikeProduct> getLikeProduct = likeProductRepository.findByProductAndMember(p, p.getMember());
boolean isLiked = getLikeProduct.isPresent();

return new ProductDto(p, webToken, isLiked);
})
.collect(Collectors.toList());

return productDtoList;
}

// 비공개 인증서 공개 인증서로 변환 (온라인에 게시하기[판매하기] 버튼 클릭 시)
public String updatePublicCertificates(Member member, Long productId, PublicResDto publicResDto) {
Optional<Product> getProduct = productRepository.findById(productId);
if(getProduct.isEmpty())
throw new BaseException(ErrorCode.PRODUCT_NOT_FOUND);

Product product = getProduct.get();
product.updatePublicYn(PublicYn.Y); // 1. 해당 인증서 공개로 변환
product.updateInfo(publicResDto); // 2. 가격, 설명 업데이트
productRepository.save(product);

return "공개 인증서로 변환 성공";
}

// 비공개 인증서 product detail 조회

// 공개 인증서 product detail 조회
}

0 comments on commit 921944a

Please sign in to comment.