diff --git a/src/main/java/umc/unimade/domain/accounts/controller/BuyerController.java b/src/main/java/umc/unimade/domain/accounts/controller/BuyerController.java index 0544664..d6ed470 100644 --- a/src/main/java/umc/unimade/domain/accounts/controller/BuyerController.java +++ b/src/main/java/umc/unimade/domain/accounts/controller/BuyerController.java @@ -5,9 +5,11 @@ import lombok.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import umc.unimade.domain.accounts.dto.BuyerOrderHistoryResponse; import umc.unimade.domain.accounts.dto.BuyerPageResponse; +import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.accounts.service.BuyerCommandService; import umc.unimade.domain.accounts.service.BuyerQueryService; import umc.unimade.domain.favorite.dto.FavoriteProductsListResponse; @@ -30,10 +32,10 @@ public class BuyerController { @Tag(name = "FavoriteSeller") @Operation(summary = "찜하지 않은 상태라면 찜하기. \n 찜한 상태라면 찜하기 취소") - @PostMapping("/favorite/{sellerId}/{buyerId}") - public ResponseEntity> toggleFavoriteSeller(@PathVariable Long sellerId, @PathVariable Long buyerId) { + @PostMapping("/favorite/{sellerId}") + public ResponseEntity> toggleFavoriteSeller(@AuthenticationPrincipal Buyer currentBuyer,@PathVariable Long sellerId) { try { - return ResponseEntity.ok(buyerCommandService.toggleFavoriteSeller(sellerId, buyerId)); + return ResponseEntity.ok(buyerCommandService.toggleFavoriteSeller(sellerId, currentBuyer)); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { @@ -43,10 +45,10 @@ public ResponseEntity> toggleFavoriteSeller(@PathVariable Long @Tag(name = "Buyer", description = "구매자 관련 API") @Operation(summary = "구매자 마이페이지에서 찜한 상품과 메이더를 보여준다.") - @GetMapping("/myPage/{buyerId}") - public ResponseEntity> getBuyerPage(@PathVariable Long buyerId) { + @GetMapping("/myPage") + public ResponseEntity> getBuyerPage(@AuthenticationPrincipal Buyer currentBuyer) { try { - return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getBuyerPage(buyerId))); + return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getBuyerPage(currentBuyer))); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { @@ -55,13 +57,13 @@ public ResponseEntity> getBuyerPage(@PathVariable } @Tag(name = "Buyer", description = "구매자 관련 API") @Operation(summary = "구매 내역 리스트") - @GetMapping("/history/{buyerId}") + @GetMapping("/history") public ResponseEntity> getOrderHistory( - @RequestParam Long buyerId, + @AuthenticationPrincipal Buyer currentBuyer, @RequestParam(required = false) Long cursor, @RequestParam int pageSize) { try { - return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getOrderHistory(buyerId, cursor, pageSize))); + return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getOrderHistory(currentBuyer, cursor, pageSize))); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { @@ -71,12 +73,13 @@ public ResponseEntity> getOrderHistory( @Tag(name = "FavoriteProduct") @Operation(summary = "찜한 상품 더보기") - @GetMapping("/{buyerId}/favorite-products") - public ResponseEntity> getFavoriteProductsList(@PathVariable Long buyerId, + @GetMapping("/favorite-products") + public ResponseEntity> getFavoriteProductsList(@AuthenticationPrincipal Buyer currentBuyer, @RequestParam(required = false) Long cursor, - @RequestParam int pageSize) { + @RequestParam int pageSize + ) { try { - return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getFavoriteProdutsList(buyerId, cursor, pageSize))); + return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getFavoriteProdutsList(currentBuyer, cursor, pageSize))); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { @@ -86,12 +89,12 @@ public ResponseEntity> getFavoriteProd @Tag(name = "FavoriteSeller") @Operation(summary = "찜한 메이더 더보기") - @GetMapping("/{buyerId}/favorite-sellers") - public ResponseEntity> getFavoriteSellersList(@PathVariable Long buyerId, + @GetMapping("/favorite-sellers") + public ResponseEntity> getFavoriteSellersList(@AuthenticationPrincipal Buyer currentBuyer, @RequestParam(required = false) Long cursor, @RequestParam int pageSize) { try { - return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getFavoriteSellersList(buyerId, cursor, pageSize))); + return ResponseEntity.ok(ApiResponse.onSuccess(buyerQueryService.getFavoriteSellersList(currentBuyer, cursor, pageSize))); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { diff --git a/src/main/java/umc/unimade/domain/accounts/service/BuyerCommandService.java b/src/main/java/umc/unimade/domain/accounts/service/BuyerCommandService.java index 4fcd289..a94d505 100644 --- a/src/main/java/umc/unimade/domain/accounts/service/BuyerCommandService.java +++ b/src/main/java/umc/unimade/domain/accounts/service/BuyerCommandService.java @@ -15,6 +15,8 @@ import java.util.Optional; +import static umc.unimade.domain.accounts.entity.QBuyer.buyer; + @Service @RequiredArgsConstructor public class BuyerCommandService { @@ -24,9 +26,8 @@ public class BuyerCommandService { private final FavoriteSellerRepository favoriteSellerRepository; @Transactional - public ApiResponse toggleFavoriteSeller(Long sellerId,Long buyerId){ + public ApiResponse toggleFavoriteSeller(Long sellerId, Buyer buyer){ Seller seller = findSellerById(sellerId); - Buyer buyer = findBuyerById(buyerId); Optional existingFavorite = findFavoriteSeller(seller,buyer); if(existingFavorite.isPresent()){ diff --git a/src/main/java/umc/unimade/domain/accounts/service/BuyerQueryService.java b/src/main/java/umc/unimade/domain/accounts/service/BuyerQueryService.java index 4af46f4..e35febe 100644 --- a/src/main/java/umc/unimade/domain/accounts/service/BuyerQueryService.java +++ b/src/main/java/umc/unimade/domain/accounts/service/BuyerQueryService.java @@ -35,8 +35,7 @@ public class BuyerQueryService { private final OrderRepository orderRepository; // 구매자 마이페이지 - public BuyerPageResponse getBuyerPage(Long buyerId){ - Buyer buyer = findBuyerById(buyerId); + public BuyerPageResponse getBuyerPage(Buyer buyer){ List favoriteProducts = favoriteProductRepository.findTop4ByBuyerOrderByCreatedAtDesc(buyer).stream() .map(FavoriteProductResponse::from) .collect(Collectors.toList()); @@ -47,9 +46,9 @@ public BuyerPageResponse getBuyerPage(Long buyerId){ } // 구매 내역 - public BuyerOrderHistoryResponse getOrderHistory(Long buyerId, Long cursor, Integer pageSize){ + public BuyerOrderHistoryResponse getOrderHistory(Buyer buyer, Long cursor, Integer pageSize){ Pageable pageable = PageRequest.of(0, pageSize); - List orders = orderRepository.findOrdersByBuyerIdWithCursorPagination(buyerId, cursor, pageable); + List orders = orderRepository.findOrdersByBuyerWithCursorPagination(buyer, cursor, pageable); Long nextCursor = orders.isEmpty() ? null : orders.get(orders.size() - 1).getId(); boolean isLast = orders.size() < pageSize; @@ -57,15 +56,17 @@ public BuyerOrderHistoryResponse getOrderHistory(Long buyerId, Long cursor, Inte } // 찜한 상품 더보기 - public FavoriteProductsListResponse getFavoriteProdutsList(Long buyerId,Long cursor, int pageSize){ + public FavoriteProductsListResponse getFavoriteProdutsList(Buyer currentBuyer,Long cursor, int pageSize){ + Buyer buyer = findBuyerById(currentBuyer.getId()); + Pageable pageable = PageRequest.of(0, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")); List favoriteProducts; if (cursor == null) { - favoriteProducts = favoriteProductRepository.findByBuyerIdOrderByCreatedAtDesc(buyerId, pageable); + favoriteProducts = favoriteProductRepository.findByBuyerOrderByCreatedAtDesc(buyer, pageable); } else { - favoriteProducts = favoriteProductRepository.findByBuyerIdAndIdLessThanOrderByCreatedAtDesc(buyerId, cursor, pageable); + favoriteProducts = favoriteProductRepository.findByBuyerAndIdLessThanOrderByCreatedAtDesc(buyer, cursor, pageable); } Long nextCursor = favoriteProducts.isEmpty() ? null : favoriteProducts.get(favoriteProducts.size() - 1).getId(); @@ -75,13 +76,13 @@ public FavoriteProductsListResponse getFavoriteProdutsList(Long buyerId,Long cur } // 찜한 메이더 더보기 - public FavoriteSellersListResponse getFavoriteSellersList(Long buyerId, Long cursor, int pageSize){ + public FavoriteSellersListResponse getFavoriteSellersList(Buyer buyer, Long cursor, int pageSize){ Pageable pageable = PageRequest.of(0, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")); List favoriteSellers; if (cursor == null){ - favoriteSellers = favoriteSellerRepository.findByBuyerIdOrderByCreatedAtDesc(buyerId, pageable); + favoriteSellers = favoriteSellerRepository.findByBuyerOrderByCreatedAtDesc(buyer, pageable); }else { - favoriteSellers = favoriteSellerRepository.findByBuyerIdAndIdLessThanOrderByCreatedAtDesc(buyerId, cursor, pageable); + favoriteSellers = favoriteSellerRepository.findByBuyerAndIdLessThanOrderByCreatedAtDesc(buyer, cursor, pageable); } Long nextCursor = favoriteSellers.isEmpty() ? null : favoriteSellers.get(favoriteSellers.size() - 1).getId(); Boolean isLast = favoriteSellers.size() < pageSize; diff --git a/src/main/java/umc/unimade/domain/favorite/repository/FavoriteProductRepository.java b/src/main/java/umc/unimade/domain/favorite/repository/FavoriteProductRepository.java index 81ab6d3..d0fc232 100644 --- a/src/main/java/umc/unimade/domain/favorite/repository/FavoriteProductRepository.java +++ b/src/main/java/umc/unimade/domain/favorite/repository/FavoriteProductRepository.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.favorite.entity.FavoriteProduct; import umc.unimade.domain.products.entity.Products; @@ -10,14 +11,16 @@ import java.util.List; import java.util.Optional; +@Repository public interface FavoriteProductRepository extends JpaRepository { + Optional findByProductAndBuyer(Products product, Buyer buyer); List findTop4ByBuyerOrderByCreatedAtDesc(Buyer buyer); - @Query("SELECT fp FROM FavoriteProduct fp WHERE fp.buyer.id = :buyerId ORDER BY fp.createdAt DESC") - List findByBuyerIdOrderByCreatedAtDesc(Long buyerId, Pageable pageable); + @Query("SELECT fp FROM FavoriteProduct fp WHERE fp.buyer = :buyer ORDER BY fp.createdAt DESC") + List findByBuyerOrderByCreatedAtDesc(Buyer buyer, Pageable pageable); - @Query("SELECT fp FROM FavoriteProduct fp WHERE fp.buyer.id = :buyerId AND fp.id < :cursor ORDER BY fp.createdAt DESC") - List findByBuyerIdAndIdLessThanOrderByCreatedAtDesc(Long buyerId, Long cursor, Pageable pageable); + @Query("SELECT fp FROM FavoriteProduct fp WHERE fp.buyer = :buyer AND fp.id < :cursor ORDER BY fp.createdAt DESC") + List findByBuyerAndIdLessThanOrderByCreatedAtDesc(Buyer buyer, Long cursor, Pageable pageable); } diff --git a/src/main/java/umc/unimade/domain/favorite/repository/FavoriteSellerRepository.java b/src/main/java/umc/unimade/domain/favorite/repository/FavoriteSellerRepository.java index 57dce22..f201b46 100644 --- a/src/main/java/umc/unimade/domain/favorite/repository/FavoriteSellerRepository.java +++ b/src/main/java/umc/unimade/domain/favorite/repository/FavoriteSellerRepository.java @@ -17,9 +17,9 @@ public interface FavoriteSellerRepository extends JpaRepository findTop4ByBuyerOrderByCreatedAtDesc(Buyer buyer); - @Query("SELECT fs FROM FavoriteSeller fs WHERE fs.buyer.id = :buyerId ORDER BY fs.createdAt DESC") - List findByBuyerIdOrderByCreatedAtDesc(Long buyerId, Pageable pageable); + @Query("SELECT fs FROM FavoriteSeller fs WHERE fs.buyer = :buyer ORDER BY fs.createdAt DESC") + List findByBuyerOrderByCreatedAtDesc(Buyer buyer, Pageable pageable); - @Query("SELECT fs FROM FavoriteSeller fs WHERE fs.buyer.id = :buyerId AND fs.id < :cursor ORDER BY fs.createdAt DESC") - List findByBuyerIdAndIdLessThanOrderByCreatedAtDesc(Long buyerId, Long cursor, Pageable pageable); + @Query("SELECT fs FROM FavoriteSeller fs WHERE fs.buyer = :buyer AND fs.id < :cursor ORDER BY fs.createdAt DESC") + List findByBuyerAndIdLessThanOrderByCreatedAtDesc(Buyer buyer, Long cursor, Pageable pageable); } diff --git a/src/main/java/umc/unimade/domain/orders/controller/OrderController.java b/src/main/java/umc/unimade/domain/orders/controller/OrderController.java index 8127ad9..96373cd 100644 --- a/src/main/java/umc/unimade/domain/orders/controller/OrderController.java +++ b/src/main/java/umc/unimade/domain/orders/controller/OrderController.java @@ -6,7 +6,9 @@ import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; +import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.accounts.entity.Seller; import umc.unimade.domain.orders.dto.*; import umc.unimade.domain.accounts.exception.UserExceptionHandler; @@ -34,10 +36,10 @@ public class OrderController { @Tag(name = "Order", description = "구매 관련 API") @Operation(summary = "상품 구매하기") - @PostMapping("/{productId}/{buyerId}") - public ResponseEntity> createOrder (@PathVariable Long productId, @PathVariable Long buyerId, @RequestBody OrderRequest orderRequest){ + @PostMapping("/{productId}") + public ResponseEntity> createOrder (@AuthenticationPrincipal Buyer currentBuyer, @PathVariable Long productId, @RequestBody OrderRequest orderRequest){ try { - return ResponseEntity.ok(ApiResponse.onSuccess(orderCommandService.createOrder(productId, buyerId, orderRequest))); + return ResponseEntity.ok(ApiResponse.onSuccess(orderCommandService.createOrder(productId, currentBuyer, orderRequest))); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); diff --git a/src/main/java/umc/unimade/domain/orders/repository/OrderRepository.java b/src/main/java/umc/unimade/domain/orders/repository/OrderRepository.java index ec73824..bec709f 100644 --- a/src/main/java/umc/unimade/domain/orders/repository/OrderRepository.java +++ b/src/main/java/umc/unimade/domain/orders/repository/OrderRepository.java @@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.orders.entity.OrderStatus; import umc.unimade.domain.orders.entity.Orders; @@ -18,8 +19,8 @@ public interface OrderRepository extends JpaRepository { @Query("SELECT o FROM Orders o WHERE o.product.id = :productId") List findOrdersByProductId(Long productId, Pageable pageable); - @Query("SELECT o FROM Orders o WHERE o.buyer.id = :buyerId AND (:cursor IS NULL OR o.id < :cursor) ORDER BY o.id DESC") - List findOrdersByBuyerIdWithCursorPagination(@Param("buyerId") Long buyerId, @Param("cursor") Long cursor, Pageable pageable); + @Query("SELECT o FROM Orders o WHERE o.buyer = :buyer AND (:cursor IS NULL OR o.id < :cursor) ORDER BY o.id DESC") + List findOrdersByBuyerWithCursorPagination(Buyer buyer, @Param("cursor") Long cursor, Pageable pageable); @Query("SELECT o FROM Orders o WHERE o.purchaseForm.createdAt < :threeDaysAgo AND o.status = :status") List findOrdersCreatedBefore(LocalDateTime threeDaysAgo, OrderStatus status); diff --git a/src/main/java/umc/unimade/domain/orders/service/OrderCommandService.java b/src/main/java/umc/unimade/domain/orders/service/OrderCommandService.java index 082b544..b197a19 100644 --- a/src/main/java/umc/unimade/domain/orders/service/OrderCommandService.java +++ b/src/main/java/umc/unimade/domain/orders/service/OrderCommandService.java @@ -35,12 +35,10 @@ public class OrderCommandService { private final OrderItemRepository orderItemRepository; @Transactional - public OrderResponse createOrder(Long productId, Long buyerId, OrderRequest orderRequest) { + public OrderResponse createOrder(Long productId, Buyer buyer, OrderRequest orderRequest) { if (!orderRequest.getPurchaseForm().getIsAgree()) { throw new OrderExceptionHandler(ErrorCode.ORDER_AGREEMENT_REQUIRED); } - - Buyer buyer = findBuyerById(buyerId); Products product = findProductById(productId); // PurchaseForm 엔티티 생성 및 저장 diff --git a/src/main/java/umc/unimade/domain/products/controller/ProductsController.java b/src/main/java/umc/unimade/domain/products/controller/ProductsController.java index 392bf88..2347d89 100644 --- a/src/main/java/umc/unimade/domain/products/controller/ProductsController.java +++ b/src/main/java/umc/unimade/domain/products/controller/ProductsController.java @@ -6,9 +6,11 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import lombok.*; import org.springframework.web.multipart.MultipartFile; +import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.products.dto.ProductRegisterResponse; import umc.unimade.domain.products.dto.ProductRequest.UpdateProductDto; import umc.unimade.domain.products.dto.ProductResponse; @@ -36,10 +38,10 @@ public class ProductsController extends BaseEntity { @Tag(name = "Products", description = "판매 상품 관련 API") @Operation(summary = "판매 상품 정보 가져오기") @GetMapping("/{productId}") - //To do : 토큰 구현 시 user 정보 추가 - public ResponseEntity> getProductDetails - (@PathVariable Long productId , @RequestParam ViewType viewType, @RequestParam(required = false) Long cursor, - @RequestParam(defaultValue = "10") int pageSize) { + public ResponseEntity> getProductDetails (@PathVariable Long productId , + @RequestParam ViewType viewType, + @RequestParam(required = false) Long cursor, + @RequestParam(defaultValue = "10") int pageSize) { try { return ResponseEntity.ok(ApiResponse.onSuccess(productsQueryService.getProduct(productId, viewType, cursor, pageSize))); } @@ -50,13 +52,12 @@ public class ProductsController extends BaseEntity { } } - //To do : buyerId 추후에 토큰으로 변경 @Tag(name = "FavoriteProduct") @Operation(summary = "찜하지 않은 상태라면 찜하기. \n 찜한 상태라면 찜하기 취소") - @PostMapping("/favorite/{productId}/{buyerId}") - public ResponseEntity> toggleFavoriteProduct(@PathVariable Long productId, @PathVariable Long buyerId) { + @PostMapping("/favorite/{productId}") + public ResponseEntity> toggleFavoriteProduct(@AuthenticationPrincipal Buyer currentBuyer, @PathVariable Long productId) { try { - return ResponseEntity.ok(productsCommandService.toggleFavoriteProduct(productId, buyerId)); + return ResponseEntity.ok(productsCommandService.toggleFavoriteProduct(productId, currentBuyer)); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); } catch (UserExceptionHandler e) { diff --git a/src/main/java/umc/unimade/domain/products/dto/OptionResponse.java b/src/main/java/umc/unimade/domain/products/dto/OptionResponse.java index 8f7eaec..8069295 100644 --- a/src/main/java/umc/unimade/domain/products/dto/OptionResponse.java +++ b/src/main/java/umc/unimade/domain/products/dto/OptionResponse.java @@ -1,6 +1,11 @@ package umc.unimade.domain.products.dto; + import lombok.*; import umc.unimade.domain.products.entity.OptionCategory; +import umc.unimade.domain.products.entity.OptionValue; + +import java.util.List; +import java.util.stream.Collectors; @Getter @AllArgsConstructor @@ -9,12 +14,17 @@ public class OptionResponse { private Long optionId; private String name; + private List values; + public static OptionResponse from(OptionCategory category) { + List values = category.getValues().stream() + .map(OptionValue::getValue) + .collect(Collectors.toList()); - public static OptionResponse from(OptionCategory option){ return OptionResponse.builder() - .optionId(option.getId()) - .name(option.getName()) + .optionId(category.getId()) + .name(category.getName()) + .values(values) .build(); } } diff --git a/src/main/java/umc/unimade/domain/products/service/ProductsCommandService.java b/src/main/java/umc/unimade/domain/products/service/ProductsCommandService.java index 3c2e64a..e95ea76 100644 --- a/src/main/java/umc/unimade/domain/products/service/ProductsCommandService.java +++ b/src/main/java/umc/unimade/domain/products/service/ProductsCommandService.java @@ -48,9 +48,8 @@ public class ProductsCommandService { private final SellerRepository sellerRepository; @Transactional - public ApiResponse toggleFavoriteProduct(Long productId, Long buyerId) { + public ApiResponse toggleFavoriteProduct(Long productId, Buyer buyer) { Products product = findProductById(productId); - Buyer buyer = findBuyerById(buyerId); Optional existingFavorite = findFavoriteProduct(product,buyer); if (existingFavorite.isPresent()) { diff --git a/src/main/java/umc/unimade/domain/qna/controller/QnAController.java b/src/main/java/umc/unimade/domain/qna/controller/QnAController.java index 26de241..3bdc5ca 100644 --- a/src/main/java/umc/unimade/domain/qna/controller/QnAController.java +++ b/src/main/java/umc/unimade/domain/qna/controller/QnAController.java @@ -6,8 +6,11 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import umc.unimade.domain.accounts.entity.Buyer; +import umc.unimade.domain.accounts.entity.Seller; import umc.unimade.domain.qna.dto.AnswerCreateRequest; import umc.unimade.domain.qna.dto.AnswerResponse; import umc.unimade.domain.qna.dto.QuestionCreateRequest; @@ -27,13 +30,14 @@ public class QnAController { private final QnACommandService qnaCommandService; private final QnAQueryService qnaQueryService; - //To do : QnA DIR 생성 @Tag(name = "QnA", description = "qna 관련 API") @Operation(summary = "질문 생성") - @PostMapping(value = "/question/{productId}/{buyerId}") - public ResponseEntity> createQuestion(@PathVariable Long productId, @PathVariable Long buyerId, @RequestPart("questionCreateRequest") QuestionCreateRequest questionCreateRequest) { + @PostMapping(value = "/question/{productId}") + public ResponseEntity> createQuestion(@AuthenticationPrincipal Buyer currentBuyer, + @PathVariable Long productId, + @RequestPart("questionCreateRequest") QuestionCreateRequest questionCreateRequest) { try { - qnaCommandService.createQuestion(productId, buyerId, questionCreateRequest); + qnaCommandService.createQuestion(productId, currentBuyer, questionCreateRequest); return ResponseEntity.ok(ApiResponse.onSuccess(null)); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); @@ -56,13 +60,12 @@ public ResponseEntity> getQuestion(@PathVariable L } } - //To do : QnA DIR 생성 @Tag(name = "QnA", description = "qna 관련 API") @Operation(summary = "답변 생성") - @PostMapping(value = "/answer/{questionId}/{sellerId}") - public ResponseEntity> createAnswer(@PathVariable Long questionId, @PathVariable Long sellerId, @RequestPart("answerCreateRequest") AnswerCreateRequest answerCreateRequest) { + @PostMapping(value = "/answer/{questionId}") + public ResponseEntity> createAnswer(@PathVariable Long questionId, @AuthenticationPrincipal Seller currentSeller, @RequestPart("answerCreateRequest") AnswerCreateRequest answerCreateRequest) { try { - qnaCommandService.createAnswer(questionId, sellerId, answerCreateRequest); + qnaCommandService.createAnswer(questionId, currentSeller, answerCreateRequest); return ResponseEntity.ok(ApiResponse.onSuccess(null)); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); diff --git a/src/main/java/umc/unimade/domain/qna/service/QnACommandService.java b/src/main/java/umc/unimade/domain/qna/service/QnACommandService.java index a0307d8..c81f434 100644 --- a/src/main/java/umc/unimade/domain/qna/service/QnACommandService.java +++ b/src/main/java/umc/unimade/domain/qna/service/QnACommandService.java @@ -37,17 +37,15 @@ public class QnACommandService { private final AnswersRespository answersRespository; @Transactional - public void createQuestion(Long productId, Long buyerId, QuestionCreateRequest questionCreateRequest) { + public void createQuestion(Long productId, Buyer buyer, QuestionCreateRequest questionCreateRequest) { Products product = findProductById(productId); - Buyer buyer = findBuyerById(buyerId); Questions question = questionCreateRequest.toEntity(product, buyer); questionsRepository.save(question); } @Transactional - public void createAnswer(Long questionId,Long sellerId, AnswerCreateRequest answerCreateRequest){ + public void createAnswer(Long questionId, Seller seller , AnswerCreateRequest answerCreateRequest){ Questions question = findQuestionById(questionId); - Seller seller = findSellerById(sellerId); Answers answer = answerCreateRequest.toEntity(seller, question); answersRespository.save(answer); } diff --git a/src/main/java/umc/unimade/domain/review/controller/ReviewController.java b/src/main/java/umc/unimade/domain/review/controller/ReviewController.java index 3daf95d..41ef388 100644 --- a/src/main/java/umc/unimade/domain/review/controller/ReviewController.java +++ b/src/main/java/umc/unimade/domain/review/controller/ReviewController.java @@ -6,8 +6,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import umc.unimade.domain.accounts.entity.Buyer; import umc.unimade.domain.review.dto.ReviewCreateRequest; import umc.unimade.domain.review.dto.ReviewResponse; import umc.unimade.domain.review.service.ReviewCommandService; @@ -27,12 +29,13 @@ public class ReviewController { @Tag(name = "Review", description = "리뷰 관련 API") @Operation(summary = "리뷰 생성") - @PostMapping(value = "/{productId}/{buyerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ResponseEntity> createReview(@PathVariable Long productId, @PathVariable Long buyerId, + @PostMapping(value = "/{productId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity> createReview(@PathVariable Long productId, + @AuthenticationPrincipal Buyer currentBuyer, @RequestPart("reviewCreateRequest") ReviewCreateRequest reviewCreateRequest, @RequestPart(value = "reviewImages", required = false) List reviewImages) { try { - reviewCommandService.createReview(productId, buyerId, reviewCreateRequest, reviewImages); + reviewCommandService.createReview(productId, currentBuyer, reviewCreateRequest, reviewImages); return ResponseEntity.ok(ApiResponse.onSuccess(null)); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage())); diff --git a/src/main/java/umc/unimade/domain/review/service/ReviewCommandService.java b/src/main/java/umc/unimade/domain/review/service/ReviewCommandService.java index 074634a..49e23be 100644 --- a/src/main/java/umc/unimade/domain/review/service/ReviewCommandService.java +++ b/src/main/java/umc/unimade/domain/review/service/ReviewCommandService.java @@ -28,11 +28,10 @@ public class ReviewCommandService { private final S3Provider s3Provider; @Transactional - public void createReview(Long productId, Long buyerId, ReviewCreateRequest reviewCreateRequest, List images) { + public void createReview(Long productId, Buyer buyer, ReviewCreateRequest reviewCreateRequest, List images) { Products product = findProductById(productId); - Buyer buyer = findBuyerById(buyerId); Review review = reviewCreateRequest.toEntity(product, buyer); - List reviewImages = reviewCreateRequest.toReviewImages(images, s3Provider, buyerId, review); + List reviewImages = reviewCreateRequest.toReviewImages(images, s3Provider, buyer.getId(), review); if (reviewImages != null) { review.setReviewImages(reviewImages); }