Skip to content

Commit

Permalink
Merge pull request #202 from Team-BC-1/feat/product-add
Browse files Browse the repository at this point in the history
관리자 상품 생성 api
  • Loading branch information
Binsreoun authored Jan 30, 2024
2 parents 5b60f0f + de6a4df commit 45ddebe
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package bc1.gream.domain.admin.controller;


import bc1.gream.domain.admin.dto.request.AdminGetRefundRequestDto;
import bc1.gream.domain.admin.dto.request.AdminProductRequestDto;
import bc1.gream.domain.admin.dto.response.AdminGetRefundResponseDto;
import bc1.gream.domain.admin.dto.response.AdminProductResponseDto;
import bc1.gream.domain.admin.mapper.RefundMapper;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.user.service.query.RefundQueryService;
import bc1.gream.global.common.RestResponse;
import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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;

Expand All @@ -17,6 +23,8 @@
@RequestMapping("/api/admin")
public class AdminController {


private final ProductService productService;
private final RefundQueryService refundQueryService;

@GetMapping("/refunds")
Expand All @@ -29,5 +37,15 @@ public RestResponse<List<AdminGetRefundResponseDto>> getRefunds(AdminGetRefundRe
.toList();

return RestResponse.success(response);

}

@PostMapping("/products")
public RestResponse<AdminProductResponseDto> addProducts(
@RequestBody AdminProductRequestDto adminProductRequestDto
) {
productService.addProduct(adminProductRequestDto);
return RestResponse.success(new AdminProductResponseDto());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bc1.gream.domain.admin.dto.request;

import lombok.Builder;

@Builder
public record AdminProductRequestDto(
String brand,
String name,
String imageUrl,
String description,
Long price
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bc1.gream.domain.admin.dto.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties
public record AdminProductResponseDto() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface ProductRepository extends JpaRepository<Product, Long>, ProductRepositoryCustom {

List<Product> findAllByNameContaining(String name);

boolean existsByName(String productName);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bc1.gream.domain.product.service.query;

import bc1.gream.domain.admin.dto.request.AdminProductRequestDto;
import bc1.gream.domain.product.dto.unit.ProductCondition;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.ProductRepository;
Expand Down Expand Up @@ -45,7 +46,26 @@ public Product findBy(Long id) throws GlobalException {
.orElseThrow(() -> new GlobalException(ResultCase.PRODUCT_NOT_FOUND));
}

public List<Product> findAllByNameContaining(String name) {
return productRepository.findAllByNameContaining(name);

public void addProduct(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) {
return Product.builder()
.name(adminProductRequestDto.name())
.brand(adminProductRequestDto.brand())
.description(adminProductRequestDto.description())
.imageUrl(adminProductRequestDto.imageUrl())
.price(adminProductRequestDto.price())
.build();
}

}
2 changes: 2 additions & 0 deletions src/main/java/bc1/gream/global/common/ResultCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public enum ResultCase {
// 존재하지 않는 상품 404
PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND, 2001, "해당 상품은 존재하지 않습니다."),

DUPLICATED_PRODUCT_NAME(HttpStatus.BAD_REQUEST, 2002, "해당 상품은 존재하지 않습니다."),

// 구매 3000번대
// 구매 요청 대상 상품이 이미 판매되었음 409
BUY_PRODUCT_SOLD_OUT(HttpStatus.CONFLICT, 3000, "구매 요청 대상 상품이 이미 판매되었습니다."),
Expand Down

0 comments on commit 45ddebe

Please sign in to comment.