Skip to content

Commit

Permalink
SCRUM-44 feat: 상품 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeopyeop-82 committed Aug 13, 2024
1 parent b7b58b0 commit 79a1b53
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kakaoteck.golagola.domain.product.controller;

import com.kakaoteck.golagola.domain.product.dto.ProductRequest;
import com.kakaoteck.golagola.domain.product.dto.ProductResponse;
import com.kakaoteck.golagola.domain.product.service.ProductService;
import com.kakaoteck.golagola.domain.seller.dto.SellerResponse;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
Expand All @@ -26,4 +27,14 @@ public ApiResponse<String> postProduct(
) {
return ApiResponse.onSuccess(productService.postProduct(seller, request));
}

@Operation(summary = "상품 수정", description = "상품 정보를 수정합니다.")
@PutMapping()
public ApiResponse<ProductResponse> modifyProduct(
@AuthenticationPrincipal Seller seller,
@PathVariable Long productId,
@RequestBody ProductRequest request
) {
return ApiResponse.onSuccess(productService.modifyProduct(seller, productId, request));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package com.kakaoteck.golagola.domain.product.dto;

public record ProductResponse() {
import com.kakaoteck.golagola.global.common.enums.Category;
import com.kakaoteck.golagola.global.common.enums.DetailCategory;
import lombok.Builder;

@Builder
public record ProductResponse(
Long productId,
String productName,
String productExplanation,
String productImage,
Long productPrice,
Long productInventory,
Category category,
DetailCategory detailCategory,
Long discount,
Long productQuantity,
Float predictReviewStar,
Float productStar
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public class Product extends BaseEntity {
@Column()
private Float productStar;


public void updateProduct(String productName, String productExplanation, String productImage, Long productPrice,
Long productInventory, Category category, DetailCategory detailCategory,
Long discount, Long productQuantity, LocalTime updateTime) {
this.productName = productName;
this.productExplanation = productExplanation;
this.productImage = productImage;
this.productPrice = productPrice;
this.productInventory = productInventory;
this.category = category;
this.detailCategory = detailCategory;
this.discount = discount;
this.productQuantity = productQuantity;
this.updateTime = updateTime;
}

public static Product from(Long productId, Seller seller, Cart cart, List<Review> reviewList,
List<OrderProduct> orderProductList, String productName,
String productExplanation, String productImage, Long productPrice,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.kakaoteck.golagola.domain.product.service;

import com.kakaoteck.golagola.domain.product.dto.ProductRequest;
import com.kakaoteck.golagola.domain.product.dto.ProductResponse;
import com.kakaoteck.golagola.domain.product.entity.Product;
import com.kakaoteck.golagola.domain.product.repository.ProductRepository;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
import com.kakaoteck.golagola.domain.seller.repository.SellerRepository;
import com.kakaoteck.golagola.global.common.code.status.ErrorStatus;
import com.kakaoteck.golagola.global.common.exception.GeneralException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -45,4 +48,45 @@ public String postProduct(Seller seller, ProductRequest request) {

return "상품등록 성공";
}

public ProductResponse modifyProduct(Seller seller, Long productId, ProductRequest request) {
// productId로 해당 상품을 찾음
Product product = productRepository.findById(productId)
.orElseThrow(() -> new GeneralException(ErrorStatus._NOT_FOUND_PRODUCT));

// 해당 상품이 현재 로그인한 seller가 등록한 것인지 확인
if (!product.getSeller().getSellerId().equals(seller.getSellerId())) {
throw new GeneralException(ErrorStatus._UNAUTHORIZED_ACCESS);
}

// Product 정보 업데이트
product.updateProduct(
request.productName(),
request.productExplanation(),
request.productImage(),
request.productPrice(),
request.productInventory(),
request.category(),
request.detailCategory(),
request.discount(),
request.productQuantity(),
LocalTime.now() // updateTime을 현재 시간으로 업데이트
);

// 업데이트된 Product 저장
productRepository.save(product);

return ProductResponse.builder()
.productId(product.getProductId())
.productName(product.getProductName())
.productExplanation(product.getProductExplanation())
.productImage(product.getProductImage())
.productPrice(product.getProductPrice())
.productInventory(product.getProductInventory())
.category(product.getCategory())
.detailCategory(product.getDetailCategory())
.discount(product.getDiscount())
.productQuantity(product.getProductQuantity())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorStatus implements BaseErrorCode {
_NOT_FOUND_USER(HttpStatus.NOT_FOUND, "USER400", "사용자가 존재하지 않습니다."),
_LOGIN_USER_INVALID(HttpStatus.BAD_REQUEST, "USER401", "로그인 중 오류가 발생하였습니다."),
_INVALID_USER(HttpStatus.BAD_REQUEST, "USER401" , "아이디 또는 비밀번호가 틀렸습니다."),
_UNAUTHORIZED_ACCESS(HttpStatus.UNAUTHORIZED, "USER401", "본인이 등록한 제품만 수정할 수 있습니다."),

// Product 에러
_NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "USER400", "제품이 존재하지 않습니다."),
Expand Down

0 comments on commit 79a1b53

Please sign in to comment.