Skip to content

Commit

Permalink
Merge pull request #138 from Troth-Cam/dev
Browse files Browse the repository at this point in the history
feat(#126): ProductController 수정
  • Loading branch information
juhee-dev authored Aug 15, 2023
2 parents 4fa2298 + 234fcc7 commit 7f21db4
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 77 deletions.
5 changes: 4 additions & 1 deletion src/main/java/trothly/trothcam/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void configure(WebSecurity web) {
"/auth/google", "/auth/validate-token",
"/auth/check-id/**", "/auth/signup",
"/auth/login", "/auth/logout",
"/h2-console/**", "/health-check", "/sample/**", "/api/image/**");
"/h2-console/**", "/health-check", "/sample/**", "/api/image/**",
"/api/products/**", "/product-detail"); // TODO: 2023/08/13 개발 완료 후 경로 삭제
}

// 스프링시큐리티 설정
Expand All @@ -63,6 +64,8 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/health-check").permitAll()
.antMatchers("/sample/**").permitAll()
.antMatchers("/api/image/**").permitAll()


//.antMatchers("/**").permitAll() // 로그인 개발 끝나면 삭제
.anyRequest().authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
package trothly.trothcam.controller.web;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.domain.product.Product;
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.BaseResponse;
import trothly.trothcam.exception.base.ErrorCode;
import trothly.trothcam.exception.custom.BadRequestException;
import trothly.trothcam.service.web.ProductService;

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

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class ProductController {

private final ProductService productService;

// TODO: 2023/08/13 응답 값이 비어있음. 엥? 로그도 안찍힘.. JPA 문제? query 직접 짜보기.
/* 공개 인증서 조회 */
// @GetMapping("/products")
// public<T> BaseResponse<ProductsResDto> findPublicProducts(@RequestParam(value = "id") String id) {
// List<Product> findProducts = productService.findPublicProducts(id);
//
// // TODO: 2023/08/11 liked 여부 확인하는 로직 필요
// // TODO: 2023/08/11 createdAt LocalDateTime -> String 변환 로직 필요
// List<ProductsResDto> collect = findProducts.stream()
// .map(m -> new ProductsResDto(m.getTitle(), m.getMember().getWebId(), "20230605", m.getPrice(), true))
// .collect(Collectors.toList());
//
// return new BaseResponse<ProductsResDto>(collect);
// }
@GetMapping("/products")
public BaseResponse<List<ProductsResDto>> findPublicProducts(@RequestParam(value = "web-id") String webId) {
List<ProductsResDto> result = productService.findPublicProducts(webId);
log.trace("인증서 가져오기 controller");
if (result.isEmpty()) {
throw new BaseException(ErrorCode.PRODUCT_NOT_FOUND);
}

return BaseResponse.onSuccess(result);
}

/* 상품 detail 화면 조회 */ // 구현은 했으나 테스트 아직 못해봄
// @GetMapping("/product-detail")
// public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
// if(req.getProductId() == null) {
// throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
// }
//
// ProductDetailResDto res = productService.findProductDetail(req, member);
// return BaseResponse.onSuccess(res);
// }
@GetMapping("/product-detail")
public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

ProductDetailResDto res = productService.findProductDetail(req, member);
return BaseResponse.onSuccess(res);
}
}
2 changes: 2 additions & 0 deletions src/main/java/trothly/trothcam/domain/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import trothly.trothcam.domain.core.BaseTimeEntity;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class Image extends BaseTimeEntity {
private Member member;

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

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/trothly/trothcam/domain/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import trothly.trothcam.domain.image.Share;
import trothly.trothcam.domain.core.BaseTimeEntity;
import trothly.trothcam.domain.image.Image;
import trothly.trothcam.domain.member.Member;
Expand Down Expand Up @@ -34,13 +33,13 @@ public class Product extends BaseTimeEntity {
@JoinColumn(name = "owner_id")
private Member member;

@Column(name = "title", nullable = false)
@Column(name = "title")
private String title;

@Column(name = "tags", nullable = false)
private int tags;

@Column(name = "price", nullable = false)
@Column(name = "price")
private Long price;

@Column(name = "description", nullable = false)
Expand All @@ -50,12 +49,9 @@ public class Product extends BaseTimeEntity {
@ColumnDefault("0")
private int views;

@Column(name = "likes", nullable = false)
private Long likes;

@Column(name = "public_yn", nullable = false)
@Enumerated(EnumType.STRING)
private Public publicYn;
private PublicYn publicYn;

@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

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

// List<Product> findAllByMember_WebIdAndPublicYn_Y(String webId); // 공개 인증서 조회
// List<Product> findAllByMember_WebIdAndPublicYn_N(String webId); // 비공개 인증서 조회
List<Product> findAllByMember_WebIdAndPublicYn(String webId, PublicYn publicYn); // 인증서 조회
List<Product> findAllByMember_IdAndPublicYn(Long id, PublicYn publicYn); // 인증서 조회
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package trothly.trothcam.domain.product;

public enum Public {
public enum PublicYn {
Y, N
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import trothly.trothcam.domain.history.History;
import trothly.trothcam.domain.product.Public;
import trothly.trothcam.domain.product.PublicYn;

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

Expand All @@ -25,7 +24,7 @@ public class ProductDetailResDto {
private String description;
private int views;
private Long likes;
private Public publicYN;
private PublicYn publicYN;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private boolean liked;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/trothly/trothcam/dto/web/ProductsResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
public class ProductsResDto<T> {

private String title;
private String ownerId;
private String createdAt;
private String ownerWebId;
private String soldAt;
private Long price;
private boolean isLiked;
}

This file was deleted.

39 changes: 23 additions & 16 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package trothly.trothcam.service.web;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.image.ImageRepository;
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.domain.product.PublicYn;
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 trothly.trothcam.exception.custom.ProductNotFoundException;
import trothly.trothcam.exception.custom.SignupException;

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

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -34,17 +33,25 @@ public class ProductService {
private final ProductRepository productRepository;
private final HistoryRepository historyRepository;
private final LikeProductRepository likeProductRepository;
private final ImageRepository imageRepository;

//
// /* 공개 인증서 조회 */
// @Transactional(readOnly = true)
// public List<Product> findPublicProducts(String webId) throws BaseException {
// List<Product> findProducts = productRepository.findAllByMember_WebIdAndPublicYn_Y(webId);
// if (findProducts == null || findProducts.isEmpty())
// throw new ProductNotFoundException(ErrorCode.PRODUCT_NOT_FOUND);
//
// return findProducts;
// }
/* 공개 인증서 조회 */
@Transactional(readOnly = true)
public List<ProductsResDto> findPublicProducts(String webId) throws BaseException {
log.trace("인증서 가져오기 service1");
List<Product> findProducts = productRepository.findAllByMember_WebIdAndPublicYn(webId, PublicYn.Y);
log.trace("인증서 가져오기 service2", findProducts.toString());
if (findProducts == null || findProducts.isEmpty())
throw new BaseException(ErrorCode.PRODUCT_NOT_FOUND);

// TODO: 2023/08/11 liked 여부 확인하는 로직 필요
// TODO: 2023/08/11 createdAt LocalDateTime -> String 변환 로직 필요
List<ProductsResDto> collect = findProducts.stream()
.map(m -> new ProductsResDto(m.getTitle(), m.getMember().getWebId(), "20230605", m.getPrice(), true))
.collect(Collectors.toList());

return collect;
}

/* 상품 detail 화면 조회 */
@Transactional(readOnly = true)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
password:
jpa:
hibernate:
ddl-auto: update
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
Expand Down

0 comments on commit 7f21db4

Please sign in to comment.