Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

어드민 상품 생성 시 이미지 추가 #261

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AdminController {
private final CouponProvider couponProvider;

@GetMapping("/refunds")
@Operation(summary = "신청된 환급 리스트 조회 요청", description = "사용자가 신청한 환급 요청 리스트를 반환합니다.")
@Operation(summary = "신청된 환급 리스트 조회 요청 [어드민 ONLY]", description = "어드민 권한의 관리자가 유저의 환급 요청 리스트를 반환합니다.")
public RestResponse<List<AdminGetRefundResponseDto>> getRefunds(AdminGetRefundRequestDto requestDto) {

List<AdminGetRefundResponseDto> response = refundQueryService.getRefunds()
Expand All @@ -50,16 +50,17 @@ public RestResponse<List<AdminGetRefundResponseDto>> getRefunds(AdminGetRefundRe
}

@PostMapping("/products")
@Operation(summary = "상품 생성 요청 [어드민 ONLY]", description = "어드민 권한의 관리자의 상품 생성 요청을 처리합니다.")
public RestResponse<AdminProductResponseDto> addProducts(
@RequestBody AdminProductRequestDto adminProductRequestDto
@Valid @RequestBody AdminProductRequestDto adminProductRequestDto
) {
productCommandService.addProduct(adminProductRequestDto);

return RestResponse.success(new AdminProductResponseDto());
}

@DeleteMapping("/refund/{id}")
@Operation(summary = "유저 환급 승인", description = "유저가 신청한 환급 요청을 승인해주는 기능입니다.")
@Operation(summary = "유저 환급 승인 [어드민 ONLY]", description = "어드민 권한의 관리자가 유저의 환급 요청을 승인해주는 기능입니다.")
public RestResponse<AdminRefundPassResponseDto> approveRefund(
@PathVariable Long id
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package bc1.gream.domain.admin.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Builder;
import org.springframework.web.multipart.MultipartFile;

@Builder
public record AdminProductRequestDto(
@NotBlank(message = "상품 브랜드명을 입력해주세요")
String brand,
@NotBlank(message = "상품명을 입력해주세요.")
String name,
String imageUrl,
@NotNull(message = "상품 이미지를 입력해주세요.")
MultipartFile file,
@NotBlank(message = "상품 상세 설명을 입력해주세요.")
String description,
@NotNull(message = "가격 필드를 입력해주세요.")
@Positive(message = "가격은 양수 입력만 가능합니다.")
Long price
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import bc1.gream.domain.product.repository.ProductRepository;
import bc1.gream.global.common.ResultCase;
import bc1.gream.global.exception.GlobalException;
import bc1.gream.infra.s3.S3ImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,24 +16,29 @@
public class ProductCommandService {

private final ProductRepository productRepository;
private final S3ImageService s3ImageService;

public void addProduct(AdminProductRequestDto adminProductRequestDto) {
verfiyExistenceByName(adminProductRequestDto);
Product product = buildProductFromRequest(adminProductRequestDto);
productRepository.save(product);
}

private void verfiyExistenceByName(AdminProductRequestDto adminProductRequestDto) {
String productName = adminProductRequestDto.name();

if (productRepository.existsByName(productName)) {
throw new GlobalException(ResultCase.DUPLICATED_PRODUCT_NAME);
}

Product product = buildProductFromRequest(adminProductRequestDto);
productRepository.save(product);
}

private Product buildProductFromRequest(AdminProductRequestDto adminProductRequestDto) {
String imageUrl = s3ImageService.getUrlAfterUpload(adminProductRequestDto.file());
return Product.builder()
.name(adminProductRequestDto.name())
.brand(adminProductRequestDto.brand())
.description(adminProductRequestDto.description())
.imageUrl(adminProductRequestDto.imageUrl())
.imageUrl(imageUrl)
.price(adminProductRequestDto.price())
.build();
}
Expand Down
Loading