Skip to content

Commit

Permalink
fix(#125) : request body 삭제 및 uri 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeon015 committed Aug 22, 2023
1 parent 8cfa6ea commit bd4f153
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 113 deletions.
4 changes: 2 additions & 2 deletions src/main/java/trothly/trothcam/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void configure(WebSecurity web) {
"/auth/check-id/**", "/auth/signup",
"/auth/login", "/auth/logout",
"/h2-console/**", "/health-check", "/sample/**", "/api/image/authenticate",
"/api/product-detail", "/api/product-ranking/**", "/api/view-all/**");
"/api/product-detail/**", "/api/product-ranking/**", "/api/view-all/**");
}

// 스프링시큐리티 설정
Expand All @@ -64,7 +64,7 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/health-check").permitAll()
.antMatchers("/sample/**").permitAll()
.antMatchers("/api/image/authenticate").permitAll()
.antMatchers("/api/product-detail").permitAll()
.antMatchers("/api/product-detail/**").permitAll()
.antMatchers("/api/product-ranking/**").permitAll()
.antMatchers("/api/view-all/**").permitAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.dto.web.HistoryResDto;
import trothly.trothcam.dto.web.TransactionReqDto;
import trothly.trothcam.exception.base.BaseResponse;
import trothly.trothcam.exception.custom.BadRequestException;
import trothly.trothcam.service.web.HistoryService;

@RequiredArgsConstructor
Expand All @@ -20,13 +15,9 @@ public class HistoryController {

private final HistoryService historyService;

@PostMapping("/transaction")
public BaseResponse<HistoryResDto> saveHistory(@RequestBody TransactionReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

HistoryResDto res = historyService.saveTransaction(req, member);
@PostMapping("/transaction/{productId}/{price}")
public BaseResponse<HistoryResDto> saveHistory(@PathVariable Long productId, @PathVariable Long price, @AuthenticationPrincipal Member member) {
HistoryResDto res = historyService.saveTransaction(productId, price, member);
return BaseResponse.onSuccess(res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import trothly.trothcam.domain.member.Member;
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 @@ -18,24 +17,16 @@ public class LikeProductController {
private final LikeProductService likeProductService;

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

LikeResDto res = likeProductService.saveLike(req, member);
@PostMapping("/{productId}")
public BaseResponse<LikeResDto> saveLike(@PathVariable Long productId, @AuthenticationPrincipal Member member) {
LikeResDto res = likeProductService.saveLike(productId, member);
return BaseResponse.onSuccess(res);
}

/* 좋아요 삭제 */
@DeleteMapping("")
public BaseResponse<LikeResDto> deleteLike(@RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

LikeResDto res = likeProductService.deleteLike(req, member);
@DeleteMapping("/{productId}")
public BaseResponse<LikeResDto> deleteLike(@PathVariable Long productId, @AuthenticationPrincipal Member member) {
LikeResDto res = likeProductService.deleteLike(productId, member);
return BaseResponse.onSuccess(res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,16 @@ public BaseResponse<List<ProductsResDto>> findPublicProducts(
}

/* 상품 detail 화면 조회 - 로그인 0*/
@GetMapping("/product-detail/{webId}")
public BaseResponse<ProductDetailResDto> findProductDetailOn(@PathVariable String webId, @RequestBody ProductReqDto req, @AuthenticationPrincipal Member member) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

ProductDetailResDto res = productService.findProductDetailOn(req, member);
@GetMapping("/{webId}/product-detail/{productId}")
public BaseResponse<ProductDetailResDto> findProductDetailOn(@PathVariable String webId, @PathVariable Long productId, @AuthenticationPrincipal Member member) {
ProductDetailResDto res = productService.findProductDetailOn(productId, member);
return BaseResponse.onSuccess(res);
}

/* 상품 detail 화면 조회 - 로그인 X*/
@GetMapping("/product-detail")
public BaseResponse<ProductDetailResDto> findProductDetail(@RequestBody ProductReqDto req) {
if(req.getProductId() == null) {
throw new BadRequestException("존재하지 않는 상품 아이디 입니다.");
}

ProductDetailResDto res = productService.findProductDetail(req);
@GetMapping("/product-detail/{productId}")
public BaseResponse<ProductDetailResDto> findProductDetail(@PathVariable Long productId) {
ProductDetailResDto res = productService.findProductDetail(productId);
return BaseResponse.onSuccess(res);
}

Expand Down
13 changes: 0 additions & 13 deletions src/main/java/trothly/trothcam/dto/web/ProductReqDto.java

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/trothly/trothcam/dto/web/TransactionReqDto.java

This file was deleted.

15 changes: 6 additions & 9 deletions src/main/java/trothly/trothcam/service/web/HistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import trothly.trothcam.domain.product.ProductRepository;
import trothly.trothcam.dto.web.HistoryDto;
import trothly.trothcam.dto.web.HistoryResDto;
import trothly.trothcam.dto.web.ProductReqDto;
import trothly.trothcam.dto.web.TransactionReqDto;
import trothly.trothcam.exception.base.BaseException;
import trothly.trothcam.exception.custom.BadRequestException;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -31,8 +28,8 @@ public class HistoryService {
private final MemberRepository memberRepository;

// 거래 내역 전체 조회
public List<HistoryDto> findAllHistory(ProductReqDto req) {
List<History> findHistories = historyRepository.findAllByProductIdOrderBySoldAtDesc(req.getProductId());
public List<HistoryDto> findAllHistory(Long productId) {
List<History> findHistories = historyRepository.findAllByProductIdOrderBySoldAtDesc(productId);
// if(findHistories == null || findHistories.isEmpty()) {
// throw new BaseException(HISTORIES_NOT_FOUND);
// }
Expand All @@ -48,8 +45,8 @@ public List<HistoryDto> findAllHistory(ProductReqDto req) {
}

// 거래 내역 저장
public HistoryResDto saveTransaction(TransactionReqDto req, Member member) {
Product product = productRepository.findById(req.getProductId()).orElseThrow(
public HistoryResDto saveTransaction(Long productId, Long price, Member member) {
Product product = productRepository.findById(productId).orElseThrow(
() -> new BaseException(PRODUCT_IS_NOT_FOUND)
);

Expand All @@ -61,10 +58,10 @@ public HistoryResDto saveTransaction(TransactionReqDto req, Member member) {
throw new BaseException(SAME_MEMBER);
}

History newHistory = historyRepository.save(new History(product, seller, member, req.getPrice()));
History newHistory = historyRepository.save(new History(product, seller, member, price));

product.updateOwner(member);

return new HistoryResDto(newHistory.getId(), product.getId(), member.getId(), seller.getId(), req.getPrice(), newHistory.getSoldAt(), member.getId());
return new HistoryResDto(newHistory.getId(), product.getId(), member.getId(), seller.getId(), price, newHistory.getSoldAt(), member.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import trothly.trothcam.domain.member.Member;
import trothly.trothcam.domain.product.Product;
import trothly.trothcam.domain.product.ProductRepository;
import trothly.trothcam.dto.web.ProductReqDto;
import trothly.trothcam.dto.web.LikeResDto;
import trothly.trothcam.exception.base.BaseException;
import trothly.trothcam.exception.custom.BadRequestException;
Expand All @@ -26,14 +25,14 @@ public class LikeProductService {
private final ProductRepository productRepository;

// 좋아요 저장
public LikeResDto saveLike(ProductReqDto req, Member member) {
Optional<LikeProduct> like = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId());
public LikeResDto saveLike(Long productId, Member member) {
Optional<LikeProduct> like = likeProductRepository.findByProductIdAndMemberId(productId, member.getId());

if(like.isPresent()) {
throw new BaseException(ALREADY_LIKED);
}

Product product = productRepository.findById(req.getProductId()).orElseThrow(
Product product = productRepository.findById(productId).orElseThrow(
() -> new BaseException(PRODUCT_IS_NOT_FOUND)
);

Expand All @@ -43,8 +42,8 @@ public LikeResDto saveLike(ProductReqDto req, Member member) {
}

// 좋아요 삭제
public LikeResDto deleteLike(ProductReqDto req, Member member) {
LikeProduct likeProduct = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()).orElseThrow(
public LikeResDto deleteLike(Long productId, Member member) {
LikeProduct likeProduct = likeProductRepository.findByProductIdAndMemberId(productId, member.getId()).orElseThrow(
() -> new BaseException(NOT_LIKED)
);

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/trothly/trothcam/service/web/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public List<ProductsResDto> findPrivateProducts(String webToken) throws BaseExce

/* 상품 detail 화면 조회 - 로그인 0 */
@Transactional
public ProductDetailResDto findProductDetailOn(ProductReqDto req, Member member) {
public ProductDetailResDto findProductDetailOn(Long productId, Member member) {
Boolean liked = false;

Product product = productRepository.findById(req.getProductId()).orElseThrow(
Product product = productRepository.findById(productId).orElseThrow(
() -> new BaseException(PRODUCT_IS_NOT_FOUND)
);

Expand All @@ -121,30 +121,30 @@ public ProductDetailResDto findProductDetailOn(ProductReqDto req, Member member)
// 조회수 갱신
product.updateViews(product.getViews() + 1);

Long likes = likeProductRepository.countByProductId(req.getProductId());
Long likes = likeProductRepository.countByProductId(productId);

//좋아요 여부
Optional<LikeProduct> isLiked = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId());
Optional<LikeProduct> isLiked = likeProductRepository.findByProductIdAndMemberId(productId, member.getId());

if(isLiked.isPresent()) {
liked = true;
} else {
liked = false;
}

List<HistoryDto> historyDto = historyService.findAllHistory(req);
List<HistoryDto> historyDto = historyService.findAllHistory(productId);

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

/* 상품 detail 화면 조회 - 로그인 x */
@Transactional
public ProductDetailResDto findProductDetail(ProductReqDto req) {
public ProductDetailResDto findProductDetail(Long productId) {
Boolean liked = false;

Product product = productRepository.findById(req.getProductId()).orElseThrow(
Product product = productRepository.findById(productId).orElseThrow(
() -> new BaseException(PRODUCT_IS_NOT_FOUND)
);

Expand All @@ -163,11 +163,11 @@ public ProductDetailResDto findProductDetail(ProductReqDto req) {
// 조회수 갱신
product.updateViews(product.getViews() + 1);

Long likes = likeProductRepository.countByProductId(req.getProductId());
Long likes = likeProductRepository.countByProductId(productId);

List<HistoryDto> historyDto = historyService.findAllHistory(req);
List<HistoryDto> historyDto = historyService.findAllHistory(productId);

return new ProductDetailResDto(req.getProductId(), product.getImage().getId(), findOwner.getWebToken(), findAuthorship.getWebToken(), product.getTitle(),
return new ProductDetailResDto(productId, product.getImage().getId(), findOwner.getWebToken(), findAuthorship.getWebToken(), product.getTitle(),
product.getTags(), product.getPrice(), product.getDescription(),product.getViews(), likes, product.getPublicYn(), product.getCreatedAt(),
product.getLastModifiedAt(), liked, historyDto);
}
Expand Down

0 comments on commit bd4f153

Please sign in to comment.