From 59565830917ac4f9c912ba52b3c272a200f89a49 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:01:10 +0900 Subject: [PATCH 01/17] =?UTF-8?q?[refactor]=20db=20postgresql=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20-=20=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- carrot/build.gradle | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/carrot/build.gradle b/carrot/build.gradle index 2f33910..b949825 100644 --- a/carrot/build.gradle +++ b/carrot/build.gradle @@ -29,7 +29,10 @@ dependencies { compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' - runtimeOnly 'com.h2database:h2' + implementation 'org.mapstruct:mapstruct:1.5.3.Final' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.0.Beta1' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + runtimeOnly 'org.postgresql:postgresql' } tasks.named('test') { From b897d02a45e1a32906624aca4fc06094000ea3cc Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:01:56 +0900 Subject: [PATCH 02/17] =?UTF-8?q?[init]=20dummy=20data=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20-#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/InitializeDefaultConfig.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 carrot/src/main/java/server/sopt/carrot/config/InitializeDefaultConfig.java diff --git a/carrot/src/main/java/server/sopt/carrot/config/InitializeDefaultConfig.java b/carrot/src/main/java/server/sopt/carrot/config/InitializeDefaultConfig.java new file mode 100644 index 0000000..182c7db --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/config/InitializeDefaultConfig.java @@ -0,0 +1,96 @@ +//package server.sopt.carrot.config; +// +//import jakarta.transaction.Transactional; +//import lombok.RequiredArgsConstructor; +//import org.springframework.boot.ApplicationArguments; +//import org.springframework.boot.ApplicationRunner; +//import org.springframework.stereotype.Component; +//import server.sopt.carrot.constant.CellingStatus; +//import server.sopt.carrot.constant.Place; +//import server.sopt.carrot.dto.customer.CustomerCreate; +//import server.sopt.carrot.entity.Customer; +//import server.sopt.carrot.entity.Product; +//import server.sopt.carrot.repo.CustomerRepository; +//import server.sopt.carrot.repo.OrderRepository; +//import server.sopt.carrot.repo.ProductRepository; +// +// +///** +// * 초기 상태 등록 Config +// */ +//@Component +//@RequiredArgsConstructor +////@Profile(value = "!test") // test 에서는 제외 +//public class InitializeDefaultConfig implements ApplicationRunner { +// +// private final CustomerRepository customerRepository; +// private final OrderRepository orderRepository; +// private final ProductRepository productRepository; +// +// /** +// * 유저등록, note 4개 등록 +// */ +//// @Bean +// @Override +// public void run(ApplicationArguments args) throws Exception { +// Customer customer = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// Customer customer1 = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// Customer customer2 = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// Customer customer3 = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// Customer customer4 = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// Customer customer5 = Customer.builder() +// .age(27) +// .name("member1") +// .build(); +// customerRepository.save(customer); +// customerRepository.save(customer1); +// customerRepository.save(customer2); +// customerRepository.save(customer3); +// customerRepository.save(customer4); +// customerRepository.save(customer5); +// +// +// Product product = Product.builder() +// .customer(customer1) +// .price(127000) +//// .cellingStatus(CellingStatus.NOT_SOLD) +// .place(Place.BELLSTREET) +// .itemName("bike") +// .itemDescription("이것은 제가 어릴적 할머니께 받은 미국식 바이크입니다. 어릴적 할머니가 미국에서 바이크를 열심히 타셔서 제가 물려받았습니다.") +// .build(); +// Product product2 = Product.builder() +// .customer(customer2) +// .price(127000) +//// .cellingStatus(CellingStatus.NOT_SOLD) +// .place(Place.CASTLENORTH) +// .itemName("iphonPro13") +// .itemDescription("\"새 상품입니다!.") +// .build(); +// Product product3 = Product.builder() +// .customer(customer4) +// .price(123455) +//// .cellingStatus(CellingStatus.NOT_SOLD) +// .place(Place.BELLSTREET) +// .itemName("new things!") +// .itemDescription("아무것도아닙니다..껄껄.") +// .build(); +// productRepository.save(product); +// productRepository.save(product2); +// productRepository.save(product3); +// } +//} From fad56cd964873e9b5c3fd2b0326e82fc2ef498d2 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:03:55 +0900 Subject: [PATCH 03/17] =?UTF-8?q?[feat]=20Place=20Enum=EA=B3=BC=20Product?= =?UTF-8?q?=20=ED=95=84=EB=93=9C=EC=97=90=20Place=20=EA=B5=AC=ED=98=84=20-?= =?UTF-8?q?=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/sopt/carrot/constant/Place.java | 10 +++++ .../server/sopt/carrot/entity/Product.java | 38 +++++++++++++++++-- .../sopt/carrot/repo/ProductRepository.java | 1 + 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 carrot/src/main/java/server/sopt/carrot/constant/Place.java diff --git a/carrot/src/main/java/server/sopt/carrot/constant/Place.java b/carrot/src/main/java/server/sopt/carrot/constant/Place.java new file mode 100644 index 0000000..ba4a069 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/constant/Place.java @@ -0,0 +1,10 @@ +package server.sopt.carrot.constant; +public enum Place { + BELLSTREET,CENTRAL,DRAGONMOUNTATIN, + CASTLEEAST,WIDEDOCK,GREATEASTGATE, + MIDDLEWAVE,CASTLENORTH,RIVERNORTH, + REVEREAST,NINEELDERY,COLLARCREEK, + +// LongClimbPort,Coppersparrow,ShadeMountain, +// Blessdgrass,RiverSouth,PineHill,RiverEast +} diff --git a/carrot/src/main/java/server/sopt/carrot/entity/Product.java b/carrot/src/main/java/server/sopt/carrot/entity/Product.java index 91dfbb9..f46917a 100644 --- a/carrot/src/main/java/server/sopt/carrot/entity/Product.java +++ b/carrot/src/main/java/server/sopt/carrot/entity/Product.java @@ -2,35 +2,65 @@ import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.ColumnDefault; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import server.sopt.carrot.constant.CellingStatus; +import server.sopt.carrot.constant.Place; import java.time.LocalDateTime; @Getter -@Setter @Builder @NoArgsConstructor @AllArgsConstructor @Entity @EntityListeners(AuditingEntityListener.class) public class Product { + + + @Getter @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + + @Setter + @Column(nullable = false) private String itemName; + + @Setter + @Column(nullable = false) private String itemDescription; + @Setter + @Column(nullable = false) private Integer price; - private Long customerId; - private CellingStatus cellingStatus; + + @Getter + @JoinColumn(nullable = false) + @ManyToOne(fetch = FetchType.LAZY) + private Customer customer; + + @Column(nullable = false) + @Enumerated(EnumType.STRING) + private Place place; + + @ColumnDefault("0") + @Builder.Default + @Setter + private Integer good=0; + + @Setter + @Column(nullable = false) + @Builder.Default + @Enumerated(EnumType.STRING) +// @ColumnDefault("NOT_SOLD") + private CellingStatus cellingStatus = CellingStatus.NOT_SOLD; @CreatedDate private LocalDateTime createdAt; @LastModifiedDate private LocalDateTime modifiedAt; - } diff --git a/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java b/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java index 52056aa..ecedd58 100644 --- a/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java +++ b/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java @@ -15,4 +15,5 @@ public interface ProductRepository extends JpaRepository { List findProductsByCustomerId(Long customerId); List findProductsByCellingStatusEquals(CellingStatus cellingStatus); + List findProductsByPlace(Place place); } From c52cc56badc7a4e9fff1b1e2020fb5e68abaa2e4 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:04:54 +0900 Subject: [PATCH 04/17] =?UTF-8?q?[feat]=20entity->=20dto=20Mapper=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/carrot/mapper/CustomerMapper.java | 17 +++++++++++++++++ .../sopt/carrot/mapper/ProductMapper.java | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 carrot/src/main/java/server/sopt/carrot/mapper/CustomerMapper.java create mode 100644 carrot/src/main/java/server/sopt/carrot/mapper/ProductMapper.java diff --git a/carrot/src/main/java/server/sopt/carrot/mapper/CustomerMapper.java b/carrot/src/main/java/server/sopt/carrot/mapper/CustomerMapper.java new file mode 100644 index 0000000..6d3f699 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/mapper/CustomerMapper.java @@ -0,0 +1,17 @@ +package server.sopt.carrot.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import server.sopt.carrot.dto.customer.CustomerCreate; +import server.sopt.carrot.dto.customer.CustomerFindDto; +import server.sopt.carrot.entity.Customer; + +@Mapper(componentModel = "spring") +public interface CustomerMapper { +// CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class); +// @Mapping() + CustomerFindDto toCustomerDto(Customer customer); +} diff --git a/carrot/src/main/java/server/sopt/carrot/mapper/ProductMapper.java b/carrot/src/main/java/server/sopt/carrot/mapper/ProductMapper.java new file mode 100644 index 0000000..4ff1e69 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/mapper/ProductMapper.java @@ -0,0 +1,17 @@ +package server.sopt.carrot.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.entity.Product; + +@Mapper(componentModel = "spring") +public interface ProductMapper { + // ProductMapper INSTNACE = Mappers.getMapper(ProductMapper.class) + @Mapping(target = "productId", source = "id") + @Mapping(target = "customerId", source = "product.customer.id") + ProductFindDto toProductFindDto(Product product); +} From e854db547c70091211a324902ddfb2a62ce9cdca Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:10:30 +0900 Subject: [PATCH 05/17] =?UTF-8?q?[refactor]=20Order=20request=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD=20-#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrot/controller/OrderController.java | 6 ++--- .../sopt/carrot/service/OrderService.java | 2 +- .../sopt/carrot/service/OrderServiceImpl.java | 22 +++++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/carrot/src/main/java/server/sopt/carrot/controller/OrderController.java b/carrot/src/main/java/server/sopt/carrot/controller/OrderController.java index 899d449..2b10587 100644 --- a/carrot/src/main/java/server/sopt/carrot/controller/OrderController.java +++ b/carrot/src/main/java/server/sopt/carrot/controller/OrderController.java @@ -5,7 +5,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import server.sopt.carrot.dto.OrderCreate; -import server.sopt.carrot.dto.ProductCreate; import server.sopt.carrot.service.OrderService; @RestController @@ -13,11 +12,12 @@ @RequestMapping("/api/v1/order") public class OrderController { private final OrderService orderService; - @PostMapping("/{productId}") + @PostMapping("{customerWhoSellId}/{productId}") public OrderCreate.Response createOrder( @Valid @RequestBody OrderCreate.Request req, + @PathVariable Long customerWhoSellId, @PathVariable Long productId ) { - return orderService.createOrder(req,productId); + return orderService.createOrder(req,customerWhoSellId,productId); } } diff --git a/carrot/src/main/java/server/sopt/carrot/service/OrderService.java b/carrot/src/main/java/server/sopt/carrot/service/OrderService.java index d5ba522..45417fe 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/OrderService.java +++ b/carrot/src/main/java/server/sopt/carrot/service/OrderService.java @@ -3,5 +3,5 @@ import server.sopt.carrot.dto.OrderCreate; public interface OrderService { - OrderCreate.Response createOrder(OrderCreate.Request req,Long productId); + OrderCreate.Response createOrder(OrderCreate.Request req,Long customerWhoSellId,Long productId); } diff --git a/carrot/src/main/java/server/sopt/carrot/service/OrderServiceImpl.java b/carrot/src/main/java/server/sopt/carrot/service/OrderServiceImpl.java index 40e2c02..05bf869 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/OrderServiceImpl.java +++ b/carrot/src/main/java/server/sopt/carrot/service/OrderServiceImpl.java @@ -5,6 +5,9 @@ import org.springframework.stereotype.Service; import server.sopt.carrot.dto.OrderCreate; import server.sopt.carrot.entity.OrderProcessor; +import server.sopt.carrot.entity.Product; +import server.sopt.carrot.error.ErrorMessage; +import server.sopt.carrot.exception.BusinessException; import server.sopt.carrot.repo.OrderRepository; @Service @@ -15,14 +18,15 @@ public class OrderServiceImpl implements OrderService { @Override @Transactional - public OrderCreate.Response createOrder(OrderCreate.Request req,Long productId) { - OrderProcessor orderProcessor = OrderProcessor.builder() - .productId(productId) - .customerWhoSellId(req.getCustomerWhoSellId()) - .customerWhoBuyId(req.getCustomerWhoBuyId()) - .build(); - productService.soldProduct(productId); - orderRepository.save(orderProcessor); - return OrderCreate.Response.fromEntity(orderProcessor); + public OrderCreate.Response createOrder(OrderCreate.Request req,Long customerWhoSellId,Long productId) { + Product product = productService.findProductById(productId); + if(product.getCustomer().getId().equals(customerWhoSellId)){ + OrderProcessor orderProcessor = OrderProcessor.of(customerWhoSellId, req.getCustomerWhoBuyId(), productId); + productService.soldProduct(product); + orderRepository.save(orderProcessor); + return OrderCreate.Response.fromEntity(orderProcessor); + }else{ + throw new BusinessException(ErrorMessage.NOT_MATCH_PRODUCT_CELLER_AND_PRODUCT); + } } } From e2673ec974ba92819cd16e18c193d90d76f41622 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:12:21 +0900 Subject: [PATCH 06/17] =?UTF-8?q?[feat]=20Product=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EA=B5=AC=ED=98=84=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrot/controller/ProductController.java | 36 +++++++++-- .../sopt/carrot/repo/ProductRepository.java | 7 +-- .../sopt/carrot/service/ProductService.java | 16 +++-- .../carrot/service/ProductServiceImpl.java | 59 +++++++++++++++---- 4 files changed, 94 insertions(+), 24 deletions(-) diff --git a/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java b/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java index 52bafb3..e80f172 100644 --- a/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java +++ b/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java @@ -3,8 +3,14 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import server.sopt.carrot.dto.*; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.dto.product.ProductCreate; +import server.sopt.carrot.dto.product.ProductEdit; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.mapper.ProductMapper; import server.sopt.carrot.service.ProductService; import java.util.List; @@ -14,7 +20,7 @@ @RequestMapping("/api/v1/product") public class ProductController { private final ProductService productService; - + private final ProductMapper productMapper; // 새로운 상품을 등록해줘! @PostMapping("") public ProductCreate.Response createProduct( @@ -22,23 +28,43 @@ public ProductCreate.Response createProduct( ) { return productService.createProductwithCustomerId(req); } - // 특정 상품 조회 + @PatchMapping("/{productId}") + public ProductFindDto productGoodUporDown( + @PathVariable Long productId, + @Valid @RequestBody ProductGoodUpdateDto productGoodUpdateDto + ) { + return productService.productGoodUporDown(productId,productGoodUpdateDto); + } + @GetMapping("/{productId}") public ProductFindDto getProduct( @PathVariable Long productId ) { - return productService.getProductById(productId); + return productMapper.toProductFindDto(productService.findProductById(productId)); + } + + @GetMapping("") + public List getAllProductbyCustomerId( + @RequestHeader Long customerId + ) { + return productService.getAllProductByCustomerId(customerId); } - // 구매안한 상품들을 전부 보여줘! : 사용자 view에 사용할려고 ? 후에 개선한다면 ? @GetMapping("/not-sold") public List getNotSoldProducts( ) { return productService.getAllNotSellProduct(); } + @GetMapping("/place/{place}") + public List getProductByPlace( + @PathVariable Place place + ) { + return productService.getProductbyPlace(place); + } + // Product의 이름이나 dsecription, 가격등 변경가능 @PatchMapping("/{productId}/edit") public ProductFindDto sellProduct( diff --git a/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java b/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java index ecedd58..e09de5c 100644 --- a/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java +++ b/carrot/src/main/java/server/sopt/carrot/repo/ProductRepository.java @@ -3,17 +3,16 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import server.sopt.carrot.constant.CellingStatus; -import server.sopt.carrot.dto.ProductFindDto; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.dto.product.ProductFindDto; import server.sopt.carrot.entity.Product; import java.util.List; @Repository public interface ProductRepository extends JpaRepository { - - ProductFindDto getProductById(Long id); + Product getProductById(Long id); List findProductsByCustomerId(Long customerId); - List findProductsByCellingStatusEquals(CellingStatus cellingStatus); List findProductsByPlace(Place place); } diff --git a/carrot/src/main/java/server/sopt/carrot/service/ProductService.java b/carrot/src/main/java/server/sopt/carrot/service/ProductService.java index 722c1e9..2f7a275 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/ProductService.java +++ b/carrot/src/main/java/server/sopt/carrot/service/ProductService.java @@ -1,13 +1,17 @@ package server.sopt.carrot.service; -import server.sopt.carrot.dto.ProductCreate; -import server.sopt.carrot.dto.ProductFindDto; -import server.sopt.carrot.dto.ProductEdit; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.dto.product.ProductCreate; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.dto.product.ProductEdit; +import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.entity.Product; import java.util.List; public interface ProductService { + Product findProductById(Long id); ProductFindDto getProductById(Long id); ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req); @@ -17,5 +21,9 @@ public interface ProductService { ProductFindDto editProduct(Long productId, ProductEdit.Request req); - ProductFindDto soldProduct(Long productId); + ProductFindDto soldProduct(Product product); + + List getProductbyPlace(Place place); + + ProductFindDto productGoodUporDown(Long productId, ProductGoodUpdateDto productGoodUpdateDto); } diff --git a/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java b/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java index 3495994..c0b026e 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java +++ b/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java @@ -6,10 +6,17 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import server.sopt.carrot.constant.CellingStatus; -import server.sopt.carrot.dto.ProductEdit; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.dto.product.ProductEdit; +import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.entity.Customer; import server.sopt.carrot.entity.Product; -import server.sopt.carrot.dto.ProductCreate; -import server.sopt.carrot.dto.ProductFindDto; +import server.sopt.carrot.dto.product.ProductCreate; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.error.ErrorMessage; +import server.sopt.carrot.exception.BusinessException; +import server.sopt.carrot.exception.NotFoundPlaceException; +import server.sopt.carrot.mapper.ProductMapper; import server.sopt.carrot.repo.ProductRepository; import java.util.List; @@ -20,22 +27,34 @@ public class ProductServiceImpl implements ProductService { private final ProductRepository productRepository; + private final CustomerService customerService; + private final ProductMapper productMapper; + @Override + public Product findProductById(Long id) { + return productRepository.findById(id) + .orElseThrow( + () -> new BusinessException(ErrorMessage.PRODUCT_NOT_FOUND) + ); + } @Override public ProductFindDto getProductById(Long id) { - return productRepository.getProductById(id); + return productMapper.toProductFindDto(productRepository.getProductById(id)); } @Transactional @Override public ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req) { + Customer customer = customerService.getCustomerById(req.getCustomerId()); Product product = Product.builder() .price(req.getPrice()) .cellingStatus(CellingStatus.NOT_SOLD) .itemName(req.getItemName()) - .customerId(req.getCustomerId()) + .customer(customer) .itemDescription(req.getItemDescription()) + .place(req.getPlace()) .build(); + productRepository.save(product); return ProductCreate.Response.fromEntity(product); } @@ -61,18 +80,36 @@ public ProductFindDto editProduct( ProductEdit.@Valid Request req) { Product product = productRepository.findById(productId) .orElseThrow(() -> new EntityNotFoundException("Not Found Product")); - product.setPrice(req.getPrice()); - product.setItemDescription(req.getItemDescription()); - product.setItemName(req.getItemName()); + product.setPrice(req.getPrice() !=null ? req.getPrice() : product.getPrice()); + product.setItemName(req.getItemName() !=null ? req.getItemName() : product.getItemName()); + product.setItemDescription(req.getItemDescription()!=null ? req.getItemDescription() : product.getItemDescription()); return ProductFindDto.of(product); } @Override @Transactional - public ProductFindDto soldProduct(Long productId) { - Product product = productRepository.findById(productId) - .orElseThrow(() -> new EntityNotFoundException("Not Found Product")); + public ProductFindDto soldProduct(Product product) { +// Product product = productRepository.findById(productId) +// .orElseThrow(() -> new BusinessException(ErrorMessage.PRODUC)T_NOT_FOUND)); product.setCellingStatus(CellingStatus.SOLD); return ProductFindDto.of(product); } + + @Override + @Transactional + public List getProductbyPlace(Place place) throws NotFoundPlaceException { + return productRepository.findProductsByPlace(place).stream().map(ProductFindDto::of) + .collect(Collectors.toList()); + } + + @Transactional + @Override + public ProductFindDto productGoodUporDown(Long productId, ProductGoodUpdateDto productGoodUpdateDto) { + Product product = productRepository.findById(productId).orElseThrow( + () -> new BusinessException(ErrorMessage.PRODUCT_NOT_FOUND) + ); + int point = productGoodUpdateDto.UporDown() ? 1 : -1; + product.setGood(product.getGood() + point); + return productMapper.toProductFindDto(product); + } } From 112625a8eac78fe41d4091ad11c7ce64bb4817a0 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:13:27 +0900 Subject: [PATCH 07/17] =?UTF-8?q?[refactor]=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/sopt/carrot/dto/CustomerFindDto.java | 9 --------- .../server/sopt/carrot/dto/ProductFindDto.java | 12 ------------ .../carrot/dto/customer/CustomerFindDto.java | 12 ++++++++++++ .../sopt/carrot/dto/product/ProductFindDto.java | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 21 deletions(-) delete mode 100644 carrot/src/main/java/server/sopt/carrot/dto/CustomerFindDto.java delete mode 100644 carrot/src/main/java/server/sopt/carrot/dto/ProductFindDto.java create mode 100644 carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerFindDto.java create mode 100644 carrot/src/main/java/server/sopt/carrot/dto/product/ProductFindDto.java diff --git a/carrot/src/main/java/server/sopt/carrot/dto/CustomerFindDto.java b/carrot/src/main/java/server/sopt/carrot/dto/CustomerFindDto.java deleted file mode 100644 index 2a8b02b..0000000 --- a/carrot/src/main/java/server/sopt/carrot/dto/CustomerFindDto.java +++ /dev/null @@ -1,9 +0,0 @@ -package server.sopt.carrot.dto; - -import server.sopt.carrot.entity.Customer; - -public record CustomerFindDto (String name, int age){ - public static CustomerFindDto of(Customer customer){ - return new CustomerFindDto(customer.getName(),customer.getAge()); - } -} diff --git a/carrot/src/main/java/server/sopt/carrot/dto/ProductFindDto.java b/carrot/src/main/java/server/sopt/carrot/dto/ProductFindDto.java deleted file mode 100644 index b8a8321..0000000 --- a/carrot/src/main/java/server/sopt/carrot/dto/ProductFindDto.java +++ /dev/null @@ -1,12 +0,0 @@ -package server.sopt.carrot.dto; - -import server.sopt.carrot.constant.CellingStatus; -import server.sopt.carrot.entity.Product; - -public record ProductFindDto( - String itemName,int price , String itemDescription , CellingStatus cellingStatus -) { - public static ProductFindDto of(Product product) { - return new ProductFindDto(product.getItemName(), product.getPrice(),product.getItemDescription(), product.getCellingStatus()); - } -} diff --git a/carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerFindDto.java b/carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerFindDto.java new file mode 100644 index 0000000..80e2e00 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerFindDto.java @@ -0,0 +1,12 @@ +package server.sopt.carrot.dto.customer; + +import jdk.jfr.StackTrace; +import lombok.Getter; +import server.sopt.carrot.entity.Customer; + + +public record CustomerFindDto (Long id,String name, int age){ + public static CustomerFindDto of(Customer customer){ + return new CustomerFindDto(customer.getId(),customer.getName(),customer.getAge()); + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/dto/product/ProductFindDto.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductFindDto.java new file mode 100644 index 0000000..f19266a --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductFindDto.java @@ -0,0 +1,17 @@ +package server.sopt.carrot.dto.product; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import server.sopt.carrot.constant.CellingStatus; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.entity.Customer; +import server.sopt.carrot.entity.Product; + +public record ProductFindDto( + Long productId, String itemName, int price , String itemDescription , CellingStatus cellingStatus, Place place, int good, + Long customerId + ) { + public static ProductFindDto of(Product product) { + return new ProductFindDto(product.getId(),product.getItemName(), product.getPrice(),product.getItemDescription(), product.getCellingStatus(),product.getPlace(),product.getGood(),product.getCustomer().getId()); + } +} From 0c2281fad4693071bf677d4701f52f7b74ae5bc1 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:14:38 +0900 Subject: [PATCH 08/17] =?UTF-8?q?[add]=20validation=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=B6=94=EA=B0=80=20-=20#1?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/{ => customer}/CustomerCreate.java | 6 ++-- .../dto/{ => product}/ProductCreate.java | 30 ++++++++++------ .../server/sopt/carrot/entity/Customer.java | 7 ++-- .../sopt/carrot/entity/OrderProcessor.java | 34 ++++++++++++++++--- 4 files changed, 58 insertions(+), 19 deletions(-) rename carrot/src/main/java/server/sopt/carrot/dto/{ => customer}/CustomerCreate.java (90%) rename carrot/src/main/java/server/sopt/carrot/dto/{ => product}/ProductCreate.java (69%) diff --git a/carrot/src/main/java/server/sopt/carrot/dto/CustomerCreate.java b/carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerCreate.java similarity index 90% rename from carrot/src/main/java/server/sopt/carrot/dto/CustomerCreate.java rename to carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerCreate.java index 221fb14..d41c07e 100644 --- a/carrot/src/main/java/server/sopt/carrot/dto/CustomerCreate.java +++ b/carrot/src/main/java/server/sopt/carrot/dto/customer/CustomerCreate.java @@ -1,10 +1,11 @@ -package server.sopt.carrot.dto; +package server.sopt.carrot.dto.customer; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Size; import lombok.*; import jakarta.validation.constraints.NotNull; +import org.springframework.http.ResponseEntity; import server.sopt.carrot.entity.Customer; public class CustomerCreate{ @@ -15,14 +16,13 @@ public class CustomerCreate{ @Builder public static class Request { @NotNull - @Min(-1) + @Min(0) @Max(99) private Integer age; @NotNull @Size(min=3,max=50,message= "Name is 3~50") private String name; - } @Getter @Setter diff --git a/carrot/src/main/java/server/sopt/carrot/dto/ProductCreate.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java similarity index 69% rename from carrot/src/main/java/server/sopt/carrot/dto/ProductCreate.java rename to carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java index 74446e1..6a5afb6 100644 --- a/carrot/src/main/java/server/sopt/carrot/dto/ProductCreate.java +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java @@ -1,20 +1,24 @@ -package server.sopt.carrot.dto; +package server.sopt.carrot.dto.product; +import jakarta.persistence.Column; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.*; import server.sopt.carrot.constant.CellingStatus; +import server.sopt.carrot.constant.Place; +import server.sopt.carrot.entity.Customer; import server.sopt.carrot.entity.Product; +@Getter public class ProductCreate { + @Builder @Getter - @Setter @AllArgsConstructor @NoArgsConstructor - @Builder - public static class Request { @NotNull @Min(-1) @@ -27,32 +31,38 @@ public static class Request { @NotNull private Long customerId; - @NotNull - private CellingStatus cellingStatus; +// @NotNull +// private CellingStatus cellingStatus; @NotNull @Size(min=1,max=512,message= "Description is 3~50") private String itemDescription; + + @NotNull + @Enumerated(EnumType.STRING) + private Place place; } @Getter - @Setter + @Builder @AllArgsConstructor @NoArgsConstructor - @Builder public static class Response { private Integer price; private String itemName; - private Long customerId; +// 후에 id값으로 바꿀수도있음 문제발생시 + private Customer customer; private String itemDescription; private CellingStatus cellingStatus; + private Place place; public static Response fromEntity(Product product) { return Response.builder() .price(product.getPrice()) .itemName(product.getItemName()) .cellingStatus(product.getCellingStatus()) - .customerId(product.getCustomerId()) + .customer(product.getCustomer()) .itemDescription(product.getItemDescription()) + .place(product.getPlace()) .build(); } diff --git a/carrot/src/main/java/server/sopt/carrot/entity/Customer.java b/carrot/src/main/java/server/sopt/carrot/entity/Customer.java index cf6aff3..cb7877e 100644 --- a/carrot/src/main/java/server/sopt/carrot/entity/Customer.java +++ b/carrot/src/main/java/server/sopt/carrot/entity/Customer.java @@ -11,7 +11,6 @@ import java.time.LocalDateTime; @Getter -@Setter @Builder @NoArgsConstructor @AllArgsConstructor @@ -23,13 +22,17 @@ public class Customer { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Setter + @Column(nullable = false) private String name; + @Setter + @Column(nullable = false) private int age; @CreatedDate private LocalDateTime createdAt; @LastModifiedDate private LocalDateTime modifiedAt; - // @Enumerated( + } diff --git a/carrot/src/main/java/server/sopt/carrot/entity/OrderProcessor.java b/carrot/src/main/java/server/sopt/carrot/entity/OrderProcessor.java index b48758a..c031273 100644 --- a/carrot/src/main/java/server/sopt/carrot/entity/OrderProcessor.java +++ b/carrot/src/main/java/server/sopt/carrot/entity/OrderProcessor.java @@ -5,13 +5,15 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.*; import lombok.*; +import org.hibernate.validator.constraints.br.CPF; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import java.time.LocalDateTime; + @Getter -@Setter -@Builder -@NoArgsConstructor -@AllArgsConstructor +@RequiredArgsConstructor @Entity @EntityListeners(AuditingEntityListener.class) public class OrderProcessor { @@ -19,7 +21,31 @@ public class OrderProcessor { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Setter + @Column(nullable = false) private Long customerWhoSellId; + + @Setter + @Column(nullable = false) private Long customerWhoBuyId; + + @CreatedDate + private LocalDateTime createAt; + + @LastModifiedDate + private LocalDateTime modifiedAt; + + @Column(nullable = false) private Long productId; + + + public OrderProcessor(Long customerWhoSellId, Long customerWhoBuyId, Long productId) { + this.customerWhoSellId = customerWhoSellId; + this.customerWhoBuyId = customerWhoBuyId; + this.productId = productId; + } + + public static OrderProcessor of(Long customerWhoSellId, Long customerWhoBuyId, Long productId) { + return new OrderProcessor(customerWhoSellId, customerWhoBuyId, productId); + } } From e27a0d3024b7f575815f362fcd7181bc6182a9b3 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:15:28 +0900 Subject: [PATCH 09/17] [not] properties -> yaml #13 --- carrot/src/main/resources/application.properties | 3 --- carrot/src/main/resources/application.yaml | 13 +++++-------- 2 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 carrot/src/main/resources/application.properties diff --git a/carrot/src/main/resources/application.properties b/carrot/src/main/resources/application.properties deleted file mode 100644 index 388b379..0000000 --- a/carrot/src/main/resources/application.properties +++ /dev/null @@ -1,3 +0,0 @@ -spring.application.name=carrot -debug=true - diff --git a/carrot/src/main/resources/application.yaml b/carrot/src/main/resources/application.yaml index 9a25c68..8d74108 100644 --- a/carrot/src/main/resources/application.yaml +++ b/carrot/src/main/resources/application.yaml @@ -1,16 +1,13 @@ spring: - # config: - # activate: - # on-profile: local datasource: - driver-class-name: org.h2.Driver - url: jdbc:h2:tcp://localhost/~/test;NON_KEYWORDS=USER - username: sa - password: + driver-class-name: org.postgresql.Driver + url: jdbc:postgresql://localhost:5432/carrot + username: postgres + password: 12345678 jpa: show-sql: true hibernate: - ddl-auto: create + ddl-auto: update properties: hibernate: format_sql: true From fa6c47c1ba0510c63f48a39bb91ced4487950ecf Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:20:37 +0900 Subject: [PATCH 10/17] =?UTF-8?q?[feat]=20Exception=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20-#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/carrot/error/ErrorMessage.java | 20 +++++++ .../carrot/error/ErrorStatusResponse.java | 11 ++++ .../carrot/exception/BusinessException.java | 13 +++++ .../carrot/exception/NotFoundException.java | 10 ++++ .../exception/NotFoundPlaceException.java | 10 ++++ .../handler/GlobalExceptionHandler.java | 55 +++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 carrot/src/main/java/server/sopt/carrot/error/ErrorMessage.java create mode 100644 carrot/src/main/java/server/sopt/carrot/error/ErrorStatusResponse.java create mode 100644 carrot/src/main/java/server/sopt/carrot/exception/BusinessException.java create mode 100644 carrot/src/main/java/server/sopt/carrot/exception/NotFoundException.java create mode 100644 carrot/src/main/java/server/sopt/carrot/exception/NotFoundPlaceException.java create mode 100644 carrot/src/main/java/server/sopt/carrot/handler/GlobalExceptionHandler.java diff --git a/carrot/src/main/java/server/sopt/carrot/error/ErrorMessage.java b/carrot/src/main/java/server/sopt/carrot/error/ErrorMessage.java new file mode 100644 index 0000000..af0ac6a --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/error/ErrorMessage.java @@ -0,0 +1,20 @@ +package server.sopt.carrot.error; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@AllArgsConstructor +@Getter +public enum ErrorMessage { + // Message + CUSTOMER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "멤버가 없어요"), + PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "상품이 없어요"), + ORDER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "주문내역이 없어요"), + PLACE_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "잘못된 장소입니다"), + + WRONG_REQUEST_FROM_YOU(HttpStatus.BAD_REQUEST.value(), "잘못된 요청입니다."), + NOT_MATCH_PRODUCT_CELLER_AND_PRODUCT(HttpStatus.BAD_REQUEST.value(), "해당 상품의 주인이 아닙니다"); + private final int status; + private final String message; +} diff --git a/carrot/src/main/java/server/sopt/carrot/error/ErrorStatusResponse.java b/carrot/src/main/java/server/sopt/carrot/error/ErrorStatusResponse.java new file mode 100644 index 0000000..3739a0b --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/error/ErrorStatusResponse.java @@ -0,0 +1,11 @@ +package server.sopt.carrot.error; + +public record ErrorStatusResponse(int status, String message) { + public static ErrorStatusResponse of(int status, String message) { + return new ErrorStatusResponse(status, message); + } + + public static ErrorStatusResponse of(ErrorMessage errorMessage) { + return new ErrorStatusResponse(errorMessage.getStatus(), errorMessage.getMessage()); + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/exception/BusinessException.java b/carrot/src/main/java/server/sopt/carrot/exception/BusinessException.java new file mode 100644 index 0000000..10de1c9 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/exception/BusinessException.java @@ -0,0 +1,13 @@ +package server.sopt.carrot.exception; + +import lombok.Getter; +import server.sopt.carrot.error.ErrorMessage; + +@Getter +public class BusinessException extends RuntimeException { + private final ErrorMessage errorMessage; + public BusinessException(ErrorMessage errorMessage) { + super(errorMessage.getMessage()); + this.errorMessage = errorMessage; + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/exception/NotFoundException.java b/carrot/src/main/java/server/sopt/carrot/exception/NotFoundException.java new file mode 100644 index 0000000..074a8bf --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/exception/NotFoundException.java @@ -0,0 +1,10 @@ +package server.sopt.carrot.exception; + + +import server.sopt.carrot.error.ErrorMessage; + +public class NotFoundException extends BusinessException { + public NotFoundException(ErrorMessage errorMessage) { + super(errorMessage); + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/exception/NotFoundPlaceException.java b/carrot/src/main/java/server/sopt/carrot/exception/NotFoundPlaceException.java new file mode 100644 index 0000000..7f14850 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/exception/NotFoundPlaceException.java @@ -0,0 +1,10 @@ +package server.sopt.carrot.exception; + + +import server.sopt.carrot.error.ErrorMessage; + +public class NotFoundPlaceException extends BusinessException { + public NotFoundPlaceException(ErrorMessage errorMessage) { + super(errorMessage); + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/handler/GlobalExceptionHandler.java b/carrot/src/main/java/server/sopt/carrot/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..4bcbb68 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/handler/GlobalExceptionHandler.java @@ -0,0 +1,55 @@ +package server.sopt.carrot.handler; +import org.hibernate.JDBCException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import server.sopt.carrot.error.ErrorMessage; +import server.sopt.carrot.error.ErrorStatusResponse; +import server.sopt.carrot.exception.BusinessException; +import server.sopt.carrot.exception.NotFoundException; +import server.sopt.carrot.exception.NotFoundPlaceException; + +import java.util.Objects; + + +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorStatusResponse.of(HttpStatus.BAD_REQUEST.value(),Objects.requireNonNull(e.getBindingResult().getFieldError().getDefaultMessage()))); + } + + + @ExceptionHandler(DataIntegrityViolationException.class) + protected ResponseEntity blogIdDuplicateException(DataIntegrityViolationException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorStatusResponse.of(HttpStatus.BAD_REQUEST.value(),ErrorMessage.WRONG_REQUEST_FROM_YOU.getMessage())); + } + + @ExceptionHandler(NotFoundPlaceException.class) + protected ResponseEntity handlerNotFoundPlace(NotFoundPlaceException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorStatusResponse.of(ErrorMessage.PLACE_NOT_FOUND)); + } + + @ExceptionHandler(BusinessException.class) + protected ResponseEntity handlerNotFoundInService(BusinessException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorStatusResponse.of(e.getErrorMessage())); + } + @ExceptionHandler(NotFoundException.class) + protected ResponseEntity handleEntityNotFountException(NotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorStatusResponse.of(ErrorMessage.WRONG_REQUEST_FROM_YOU)); + } + + + @ExceptionHandler(JDBCException.class) + protected ResponseEntity sqlException(JDBCException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorStatusResponse.of(e.getErrorCode() , e.getSQLState())); + } + //내 잘못이 아닐꺼야 하는 BAD REQUEST + @ExceptionHandler(Exception.class) + protected ResponseEntity globalException(Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorStatusResponse.of(HttpStatus.BAD_REQUEST.value() , e.getMessage())); + } +} From 8122f49b047e4985fd666a2adb73ed0fd6469cad Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:21:58 +0900 Subject: [PATCH 11/17] =?UTF-8?q?[feat]=20=EC=A2=8B=EC=95=84=EC=9A=94=20dt?= =?UTF-8?q?o=20=EA=B5=AC=ED=98=84=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/sopt/carrot/dto/{ => product}/ProductEdit.java | 4 +--- .../sopt/carrot/dto/product/ProductGoodUpdateDto.java | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) rename carrot/src/main/java/server/sopt/carrot/dto/{ => product}/ProductEdit.java (68%) create mode 100644 carrot/src/main/java/server/sopt/carrot/dto/product/ProductGoodUpdateDto.java diff --git a/carrot/src/main/java/server/sopt/carrot/dto/ProductEdit.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductEdit.java similarity index 68% rename from carrot/src/main/java/server/sopt/carrot/dto/ProductEdit.java rename to carrot/src/main/java/server/sopt/carrot/dto/product/ProductEdit.java index 84e1e0b..65c746b 100644 --- a/carrot/src/main/java/server/sopt/carrot/dto/ProductEdit.java +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductEdit.java @@ -1,8 +1,6 @@ -package server.sopt.carrot.dto; +package server.sopt.carrot.dto.product; -import jakarta.validation.constraints.NotNull; import lombok.*; -import server.sopt.carrot.constant.CellingStatus; public class ProductEdit { @Getter diff --git a/carrot/src/main/java/server/sopt/carrot/dto/product/ProductGoodUpdateDto.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductGoodUpdateDto.java new file mode 100644 index 0000000..54ae4c8 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductGoodUpdateDto.java @@ -0,0 +1,6 @@ +package server.sopt.carrot.dto.product; + +public record ProductGoodUpdateDto( + boolean UporDown +) { +} From 5e85359d14075125f232d290813a0e3740bf2297 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:22:42 +0900 Subject: [PATCH 12/17] =?UTF-8?q?[refactor]=20=EC=A3=BC=EB=AC=B8=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- carrot/src/main/java/server/sopt/carrot/dto/OrderCreate.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/carrot/src/main/java/server/sopt/carrot/dto/OrderCreate.java b/carrot/src/main/java/server/sopt/carrot/dto/OrderCreate.java index dc56971..d953dbc 100644 --- a/carrot/src/main/java/server/sopt/carrot/dto/OrderCreate.java +++ b/carrot/src/main/java/server/sopt/carrot/dto/OrderCreate.java @@ -11,8 +11,6 @@ public class OrderCreate { @NoArgsConstructor @Builder public static class Request { - @NotNull - private Long customerWhoSellId; @NotNull private Long customerWhoBuyId; } From 335393147812abdbb08cfe405febfb05f4a50efa Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Thu, 2 May 2024 23:23:51 +0900 Subject: [PATCH 13/17] =?UTF-8?q?[feat]=20Mapper,=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20-=20#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrot/controller/CustomerController.java | 22 ++++++------ .../sopt/carrot/service/CustomerService.java | 15 ++++---- .../carrot/service/CustomerServiceImpl.java | 34 ++++++++++++------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/carrot/src/main/java/server/sopt/carrot/controller/CustomerController.java b/carrot/src/main/java/server/sopt/carrot/controller/CustomerController.java index 310a150..c1ca526 100644 --- a/carrot/src/main/java/server/sopt/carrot/controller/CustomerController.java +++ b/carrot/src/main/java/server/sopt/carrot/controller/CustomerController.java @@ -2,11 +2,14 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import server.sopt.carrot.dto.CustomerCreate; -import server.sopt.carrot.dto.CustomerFindDto; -import server.sopt.carrot.dto.ProductFindDto; +import server.sopt.carrot.dto.customer.CustomerCreate; +import server.sopt.carrot.dto.customer.CustomerFindDto; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.error.ErrorMessage; +import server.sopt.carrot.exception.BusinessException; +import server.sopt.carrot.mapper.CustomerMapper; import server.sopt.carrot.service.CustomerService; import java.util.List; @@ -16,24 +19,23 @@ public class CustomerController { private final CustomerService customerService; + private final CustomerMapper customerMapper; @PostMapping public CustomerCreate.Response createCustomer( @Valid @RequestBody CustomerCreate.Request req ) { return customerService.createCustomer(req); } - @GetMapping("/{customerId}") public CustomerFindDto getCustomerBasicInfo( @PathVariable Long customerId ) { - return customerService.getCustomerById(customerId); + return customerMapper.toCustomerDto(customerService.getCustomerById(customerId)); } - @GetMapping("/{customerId}/items") - public List getCustomerProducts( - @PathVariable Long customerId + @GetMapping("") + public List getCustomerBasicInfo( ) { - return customerService.getCustomerProducts(customerId); + return customerService.getAllCustomer(); } } diff --git a/carrot/src/main/java/server/sopt/carrot/service/CustomerService.java b/carrot/src/main/java/server/sopt/carrot/service/CustomerService.java index e303207..1890380 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/CustomerService.java +++ b/carrot/src/main/java/server/sopt/carrot/service/CustomerService.java @@ -1,17 +1,16 @@ package server.sopt.carrot.service; -import server.sopt.carrot.dto.CustomerCreate; -import server.sopt.carrot.dto.CustomerFindDto; -import server.sopt.carrot.dto.ProductFindDto; +import server.sopt.carrot.dto.customer.CustomerCreate; +import server.sopt.carrot.dto.customer.CustomerFindDto; +import server.sopt.carrot.dto.product.ProductFindDto; import server.sopt.carrot.entity.Customer; import java.util.List; +import java.util.Optional; public interface CustomerService { - CustomerFindDto getCustomerById(Long customerId); - + List getAllCustomer(); + Customer getCustomerById(Long customerId); CustomerCreate.Response createCustomer(CustomerCreate.Request req); - - List getCustomerProducts(Long customerId); - +// List getCustomerProducts(Long customerId); } diff --git a/carrot/src/main/java/server/sopt/carrot/service/CustomerServiceImpl.java b/carrot/src/main/java/server/sopt/carrot/service/CustomerServiceImpl.java index 9250891..0b88899 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/CustomerServiceImpl.java +++ b/carrot/src/main/java/server/sopt/carrot/service/CustomerServiceImpl.java @@ -3,20 +3,23 @@ import jakarta.persistence.EntityNotFoundException; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import server.sopt.carrot.entity.Customer; -import server.sopt.carrot.dto.CustomerCreate; -import server.sopt.carrot.dto.CustomerFindDto; -import server.sopt.carrot.dto.ProductFindDto; +import server.sopt.carrot.dto.customer.CustomerCreate; +import server.sopt.carrot.dto.customer.CustomerFindDto; +import server.sopt.carrot.dto.product.ProductFindDto; +import server.sopt.carrot.error.ErrorMessage; +import server.sopt.carrot.exception.BusinessException; import server.sopt.carrot.repo.CustomerRepository; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class CustomerServiceImpl implements CustomerService { private final CustomerRepository customerRepository; - private final ProductService productService; @Override @Transactional @@ -29,17 +32,24 @@ public CustomerCreate.Response createCustomer(CustomerCreate.Request req) customerRepository.save(customer); return CustomerCreate.Response.fromEntity(customer); } + @Override - public CustomerFindDto getCustomerById(Long customerId) { - return CustomerFindDto.of(findCutomer(customerId)); - } - private Customer findCutomer(Long id) { - return customerRepository.findById(id) - .orElseThrow(() -> new EntityNotFoundException("NO ID MATCH FOR CUSTOMER")); + public List getAllCustomer() { + return customerRepository.findAll() + .stream().map(CustomerFindDto::of) + .collect(Collectors.toList()); } @Override - public List getCustomerProducts(Long customerId) { - return productService.getAllProductByCustomerId(customerId); + public Customer getCustomerById(Long customerId) { + return findCutomer(customerId); + } + private Customer findCutomer(Long id) { + return customerRepository.findById(id) + .orElseThrow(() -> new BusinessException(ErrorMessage.CUSTOMER_NOT_FOUND)); } +// @Override +// public List getCustomerProducts(Long customerId) { +// return productService.getAllProductByCustomerId(customerId); +// } } From e90ddb85fac9a6b2a7e3224bfd04309b44679294 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Mon, 3 Jun 2024 21:05:39 +0900 Subject: [PATCH 14/17] =?UTF-8?q?[chore]=20#18=20-=20s3=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- carrot/build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/carrot/build.gradle b/carrot/build.gradle index b949825..05f0154 100644 --- a/carrot/build.gradle +++ b/carrot/build.gradle @@ -33,6 +33,8 @@ dependencies { annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.0.Beta1' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' + implementation("software.amazon.awssdk:bom:2.21.0") + implementation("software.amazon.awssdk:s3:2.21.0") } tasks.named('test') { From 60cc86e6b7c8c3bc02ca88f5f82b717c1e50ef1c Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Mon, 3 Jun 2024 21:06:20 +0900 Subject: [PATCH 15/17] =?UTF-8?q?[feat]=20#18-=20=EA=B8=B0=EB=B3=B8=20Conf?= =?UTF-8?q?ig=20=EB=B0=8F=20S3=20service=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/sopt/carrot/config/S3Config.java | 48 +++++++++++ .../sopt/carrot/external/S3Service.java | 80 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 carrot/src/main/java/server/sopt/carrot/config/S3Config.java create mode 100644 carrot/src/main/java/server/sopt/carrot/external/S3Service.java diff --git a/carrot/src/main/java/server/sopt/carrot/config/S3Config.java b/carrot/src/main/java/server/sopt/carrot/config/S3Config.java new file mode 100644 index 0000000..eff1f49 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/config/S3Config.java @@ -0,0 +1,48 @@ +package server.sopt.carrot.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; + +@Configuration +public class S3Config { + + private static final String AWS_ACCESS_KEY_ID = "aws.accessKeyId"; + private static final String AWS_SECRET_ACCESS_KEY = "aws.secretAccessKey"; + + private final String accessKey; + private final String secretKey; + private final String regionString; + + public S3Config(@Value("${aws-property.access-key}") final String accessKey, + @Value("${aws-property.secret-key}") final String secretKey, + @Value("${aws-property.aws-region}") final String regionString) { + this.accessKey = accessKey; + this.secretKey = secretKey; + this.regionString = regionString; + } + + + @Bean + public SystemPropertyCredentialsProvider systemPropertyCredentialsProvider() { + System.setProperty(AWS_ACCESS_KEY_ID, accessKey); + System.setProperty(AWS_SECRET_ACCESS_KEY, secretKey); + return SystemPropertyCredentialsProvider.create(); + } + + @Bean + public Region getRegion() { + return Region.of(regionString); + } + + @Bean + public S3Client getS3Client() { + return S3Client.builder() + .region(getRegion()) + .credentialsProvider(systemPropertyCredentialsProvider()) + .build(); + } +} \ No newline at end of file diff --git a/carrot/src/main/java/server/sopt/carrot/external/S3Service.java b/carrot/src/main/java/server/sopt/carrot/external/S3Service.java new file mode 100644 index 0000000..c25a5c9 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/external/S3Service.java @@ -0,0 +1,80 @@ +package server.sopt.carrot.external; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; +import server.sopt.carrot.config.S3Config; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +@Component +public class S3Service { + + private final String bucketName; + private final S3Config awsConfig; + private static final List IMAGE_EXTENSIONS = Arrays.asList("image/jpeg", "image/png", "image/jpg", "image/webp"); + + + public S3Service(@Value("${aws-property.s3-bucket-name}") final String bucketName, S3Config awsConfig) { + this.bucketName = bucketName; + this.awsConfig = awsConfig; + } + + + public String uploadImage(String directoryPath, MultipartFile image) throws IOException { + final String key = directoryPath + generateImageFileName(); + final S3Client s3Client = awsConfig.getS3Client(); + + validateExtension(image); + validateFileSize(image); + + PutObjectRequest request = PutObjectRequest.builder() + .bucket(bucketName) + .key(key) + .contentType(image.getContentType()) + .contentDisposition("inline") + .build(); + + RequestBody requestBody = RequestBody.fromBytes(image.getBytes()); + s3Client.putObject(request, requestBody); + return key; + } + + public void deleteImage(String key) throws IOException { + final S3Client s3Client = awsConfig.getS3Client(); + s3Client.deleteObject((DeleteObjectRequest.Builder builder) -> + builder.bucket(bucketName) + .key(key) + .build() + ); + } + + + private String generateImageFileName() { + return UUID.randomUUID() + ".jpg"; + } + + + private void validateExtension(MultipartFile image) { + String contentType = image.getContentType(); + if (!IMAGE_EXTENSIONS.contains(contentType)) { + throw new RuntimeException("이미지 확장자는 jpg, png, webp만 가능합니다."); + } + } + + private static final Long MAX_FILE_SIZE = 5 * 1024 * 1024L; + + private void validateFileSize(MultipartFile image) { + if (image.getSize() > MAX_FILE_SIZE) { + throw new RuntimeException("이미지 사이즈는 5MB를 넘을 수 없습니다."); + } + } + +} \ No newline at end of file From 7fb62b2519487d8b9583023ea4c26d0a1654d4d9 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Mon, 3 Jun 2024 21:12:25 +0900 Subject: [PATCH 16/17] =?UTF-8?q?[feat]=20#18-=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrot/controller/ProductController.java | 18 ++++++---- .../carrot/dto/product/ProductCreate.java | 11 ++++--- .../carrot/dto/product/ProductDelete.java | 2 ++ .../sopt/carrot/service/ProductService.java | 11 ++++--- .../carrot/service/ProductServiceImpl.java | 33 ++++++++++++++----- 5 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 carrot/src/main/java/server/sopt/carrot/dto/product/ProductDelete.java diff --git a/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java b/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java index e80f172..40f0dc0 100644 --- a/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java +++ b/carrot/src/main/java/server/sopt/carrot/controller/ProductController.java @@ -6,13 +6,11 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import server.sopt.carrot.constant.Place; -import server.sopt.carrot.dto.product.ProductCreate; -import server.sopt.carrot.dto.product.ProductEdit; -import server.sopt.carrot.dto.product.ProductFindDto; -import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.dto.product.*; import server.sopt.carrot.mapper.ProductMapper; import server.sopt.carrot.service.ProductService; +import java.io.IOException; import java.util.List; @RestController @@ -22,12 +20,20 @@ public class ProductController { private final ProductService productService; private final ProductMapper productMapper; // 새로운 상품을 등록해줘! - @PostMapping("") + @PostMapping public ProductCreate.Response createProduct( @Valid @RequestBody ProductCreate.Request req - ) { + ) throws IOException { return productService.createProductwithCustomerId(req); } + + @DeleteMapping("/{productId}") + public ResponseEntity deleteProduct( + @Valid @RequestHeader Long productId + ) throws IOException { + return productService.deleteProduct(productId); + } + // 특정 상품 조회 @PatchMapping("/{productId}") public ProductFindDto productGoodUporDown( diff --git a/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java index 6a5afb6..5002735 100644 --- a/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductCreate.java @@ -1,12 +1,12 @@ package server.sopt.carrot.dto.product; -import jakarta.persistence.Column; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.*; +import org.springframework.web.multipart.MultipartFile; import server.sopt.carrot.constant.CellingStatus; import server.sopt.carrot.constant.Place; import server.sopt.carrot.entity.Customer; @@ -25,22 +25,23 @@ public static class Request { private Integer price; @NotNull - @Size(min=3,max=50,message= "Name is 3~50") + @Size(min = 3, max = 50, message = "Name is 3~50") private String itemName; @NotNull private Long customerId; -// @NotNull -// private CellingStatus cellingStatus; @NotNull - @Size(min=1,max=512,message= "Description is 3~50") + @Size(min = 1, max = 512, message = "Description is 3~50") private String itemDescription; @NotNull @Enumerated(EnumType.STRING) private Place place; + + private MultipartFile image; + } @Getter @Builder diff --git a/carrot/src/main/java/server/sopt/carrot/dto/product/ProductDelete.java b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductDelete.java new file mode 100644 index 0000000..09c7379 --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/dto/product/ProductDelete.java @@ -0,0 +1,2 @@ +package server.sopt.carrot.dto.product; + diff --git a/carrot/src/main/java/server/sopt/carrot/service/ProductService.java b/carrot/src/main/java/server/sopt/carrot/service/ProductService.java index 2f7a275..28e7389 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/ProductService.java +++ b/carrot/src/main/java/server/sopt/carrot/service/ProductService.java @@ -1,19 +1,18 @@ package server.sopt.carrot.service; +import org.springframework.http.ResponseEntity; import server.sopt.carrot.constant.Place; -import server.sopt.carrot.dto.product.ProductCreate; -import server.sopt.carrot.dto.product.ProductFindDto; -import server.sopt.carrot.dto.product.ProductEdit; -import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.dto.product.*; import server.sopt.carrot.entity.Product; +import java.io.IOException; import java.util.List; public interface ProductService { Product findProductById(Long id); ProductFindDto getProductById(Long id); - ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req); + ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req) throws IOException; List getAllProductByCustomerId(Long customerId); @@ -26,4 +25,6 @@ public interface ProductService { List getProductbyPlace(Place place); ProductFindDto productGoodUporDown(Long productId, ProductGoodUpdateDto productGoodUpdateDto); + + ResponseEntity deleteProduct(Long productId); } diff --git a/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java b/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java index c0b026e..929c33f 100644 --- a/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java +++ b/carrot/src/main/java/server/sopt/carrot/service/ProductServiceImpl.java @@ -4,21 +4,23 @@ import jakarta.transaction.Transactional; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import server.sopt.carrot.constant.CellingStatus; import server.sopt.carrot.constant.Place; -import server.sopt.carrot.dto.product.ProductEdit; -import server.sopt.carrot.dto.product.ProductGoodUpdateDto; +import server.sopt.carrot.dto.product.*; import server.sopt.carrot.entity.Customer; +import server.sopt.carrot.entity.Image; import server.sopt.carrot.entity.Product; -import server.sopt.carrot.dto.product.ProductCreate; -import server.sopt.carrot.dto.product.ProductFindDto; import server.sopt.carrot.error.ErrorMessage; import server.sopt.carrot.exception.BusinessException; import server.sopt.carrot.exception.NotFoundPlaceException; +import server.sopt.carrot.external.S3Service; import server.sopt.carrot.mapper.ProductMapper; +import server.sopt.carrot.repo.ImageRepository; import server.sopt.carrot.repo.ProductRepository; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -29,7 +31,9 @@ public class ProductServiceImpl implements ProductService { private final ProductRepository productRepository; private final CustomerService customerService; private final ProductMapper productMapper; - + private final S3Service s3Service; + private final ImageRepository imageRepository; + private final String BLOG_S3_UPLOAD_FOLER = "/blog"; @Override public Product findProductById(Long id) { return productRepository.findById(id) @@ -43,8 +47,7 @@ public ProductFindDto getProductById(Long id) { } @Transactional @Override - public ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req) - { + public ProductCreate.Response createProductwithCustomerId(ProductCreate.Request req) throws IOException { Customer customer = customerService.getCustomerById(req.getCustomerId()); Product product = Product.builder() .price(req.getPrice()) @@ -56,6 +59,11 @@ public ProductCreate.Response createProductwithCustomerId(ProductCreate.Request .build(); productRepository.save(product); + String imageUrl = s3Service.uploadImage(BLOG_S3_UPLOAD_FOLER, req.getImage()); + Image image = Image.builder() + .imageUrl(imageUrl) + .product(product).build(); + imageRepository.save(image); return ProductCreate.Response.fromEntity(product); } @@ -89,8 +97,6 @@ public ProductFindDto editProduct( @Override @Transactional public ProductFindDto soldProduct(Product product) { -// Product product = productRepository.findById(productId) -// .orElseThrow(() -> new BusinessException(ErrorMessage.PRODUC)T_NOT_FOUND)); product.setCellingStatus(CellingStatus.SOLD); return ProductFindDto.of(product); } @@ -112,4 +118,13 @@ public ProductFindDto productGoodUporDown(Long productId, ProductGoodUpdateDto p product.setGood(product.getGood() + point); return productMapper.toProductFindDto(product); } + + @Override + public ResponseEntity deleteProduct(Long productId) { + Image image= imageRepository.findByProduct_Id(productId).orElseThrow( + () -> new BusinessException(ErrorMessage.PRODUCT_NOT_FOUND) + ); + imageRepository.delete(image); + return ResponseEntity.ok().build(); + } } From 8e2dce2945dde07a3130d198b17b1784ad80e241 Mon Sep 17 00:00:00 2001 From: rlarlgnszx Date: Mon, 3 Jun 2024 21:12:45 +0900 Subject: [PATCH 17/17] =?UTF-8?q?[feat]=20#18=20-=20image=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/server/sopt/carrot/entity/Image.java | 26 +++++++++++++++++++ .../sopt/carrot/repo/ImageRepository.java | 10 +++++++ 2 files changed, 36 insertions(+) create mode 100644 carrot/src/main/java/server/sopt/carrot/entity/Image.java create mode 100644 carrot/src/main/java/server/sopt/carrot/repo/ImageRepository.java diff --git a/carrot/src/main/java/server/sopt/carrot/entity/Image.java b/carrot/src/main/java/server/sopt/carrot/entity/Image.java new file mode 100644 index 0000000..0be333b --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/entity/Image.java @@ -0,0 +1,26 @@ +package server.sopt.carrot.entity; + +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +public class Image { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String imageUrl; + + @OneToOne + Product product; + + @Builder + public Image(String imageUrl, Product product) { + this.imageUrl = imageUrl; + this.product = product; + } +} diff --git a/carrot/src/main/java/server/sopt/carrot/repo/ImageRepository.java b/carrot/src/main/java/server/sopt/carrot/repo/ImageRepository.java new file mode 100644 index 0000000..dc9fc5d --- /dev/null +++ b/carrot/src/main/java/server/sopt/carrot/repo/ImageRepository.java @@ -0,0 +1,10 @@ +package server.sopt.carrot.repo; + +import org.springframework.data.jpa.repository.JpaRepository; +import server.sopt.carrot.entity.Image; + +import java.util.Optional; + +public interface ImageRepository extends JpaRepository { + Optional findByProduct_Id(Long proudctId); +}