Skip to content

Commit

Permalink
Merge pull request #48 from Uni-Made/feature/#25
Browse files Browse the repository at this point in the history
✨ [FEATURE] 판매자의 상품 등록, 수정, 삭제 구현 (#25)
  • Loading branch information
jjin70 authored Jul 20, 2024
2 parents 407016f + 42cc281 commit 79b687d
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/umc/unimade/domain/accounts/entity/Seller.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package umc.unimade.domain.accounts.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import umc.unimade.domain.favorite.entity.FavoriteSeller;
import umc.unimade.domain.products.entity.ProductRegister;
import umc.unimade.domain.products.entity.Products;
import umc.unimade.domain.qna.entity.Answers;
import umc.unimade.global.common.BaseEntity;
Expand Down Expand Up @@ -52,6 +54,10 @@ public class Seller extends BaseEntity {
@OneToMany(mappedBy = "seller", fetch = FetchType.LAZY)
private List<Products> products = new ArrayList<>();

@OneToMany(mappedBy = "seller", fetch = FetchType.LAZY)
@JsonIgnore
private List<ProductRegister> productRegister = new ArrayList<>();

@OneToMany(mappedBy = "seller", fetch = FetchType.LAZY)
private List<Answers> answers = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;
import umc.unimade.domain.products.dto.ProductRequest.UpdateProductDto;
import umc.unimade.domain.products.dto.ProductResponse;
import umc.unimade.domain.products.dto.ProductRequest.CreateProductDto;
import umc.unimade.domain.products.entity.ProductRegister;
import umc.unimade.domain.products.entity.Products;
import umc.unimade.domain.products.entity.ViewType;
import umc.unimade.domain.products.service.ProductsCommandService;
import umc.unimade.domain.products.service.ProductsQueryService;
import umc.unimade.global.common.ApiResponse;
import umc.unimade.global.common.BaseEntity;
import umc.unimade.global.common.ErrorCode;
import umc.unimade.global.common.exception.ProductsExceptionHandler;
import umc.unimade.global.common.exception.UserExceptionHandler;

import java.util.List;

@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductsController {
public class ProductsController extends BaseEntity {
private final ProductsQueryService productsQueryService;
private final ProductsCommandService productsCommandService;

Expand Down Expand Up @@ -54,4 +62,33 @@ public ResponseEntity<ApiResponse<Void>> toggleFavoriteProduct(@PathVariable Lon
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.onFailure(ErrorCode.BUYER_NOT_FOUND.getCode(), ErrorCode.BUYER_NOT_FOUND.getMessage()));
}
}

// 상품 등록
@Tag(name = "Products")
@Operation(summary = "상품 등록", description = "productRegister로 저장")
@PostMapping(value = "/create", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<ApiResponse<ProductRegister>> createProduct(@RequestPart("createProductDto") CreateProductDto request,
@RequestPart(name = "image", required = false) List<MultipartFile> images) {
ApiResponse<ProductRegister> createdProduct = productsCommandService.createProduct(request, images);
return new ResponseEntity<>(createdProduct, HttpStatus.CREATED);
}

// 상품 수정
@Tag(name = "Products")
@Operation(summary = "상품 수정", description = "product 수정")
@PutMapping("/{productId}")
public ResponseEntity<ApiResponse<Products>> updateProduct(@PathVariable Long productId,
@RequestBody UpdateProductDto request) {
ApiResponse<Products> updatedProduct = productsCommandService.updateProduct(productId, request);
return ResponseEntity.ok(updatedProduct);
}

// 상품 삭제
@Tag(name = "Products")
@Operation(summary = "상품 삭제")
@DeleteMapping("/{productId}")
public ApiResponse<Object> deleteProduct(@PathVariable Long productId) {
productsCommandService.deleteProduct(productId);
return ApiResponse.noContent();
}
}
21 changes: 21 additions & 0 deletions src/main/java/umc/unimade/domain/products/dto/OptionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//package umc.unimade.domain.products.dto;
//
//import lombok.*;
////import umc.unimade.domain.products.entity.Options;
//import umc.unimade.domain.products.entity.ProductRegister;
//
//@Getter
//@NoArgsConstructor
//@AllArgsConstructor
//public class OptionRequest {
// private String name;
// private String value;
//
// public Options toEntity(ProductRegister productRegister) {
// return Options.builder()
// .name(name)
// .value(value)
// .productRegister(productRegister)
// .build();
// }
//}
73 changes: 73 additions & 0 deletions src/main/java/umc/unimade/domain/products/dto/ProductRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package umc.unimade.domain.products.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import umc.unimade.domain.accounts.entity.Seller;
import umc.unimade.domain.products.entity.*;
import umc.unimade.global.registerStatus.RegisterStatus;

import java.time.LocalDate;
import java.util.List;

public class ProductRequest {

@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class CreateProductDto { // TODO - 셀러 추가
private String name;
private String content;
private Long price;
private LocalDate deadline;
private ProductStatus status;
private String university;
private PickupOption pickupOption;
private String bankName;
private String accountNumber;
private String accountName;
private RegisterStatus registerStatus;
private Long sellerId;
private Long categoryId;
// private List<OptionRequest> options;

public ProductRegister toEntity(Category category, Seller seller) {
ProductRegister product = ProductRegister.builder()
.name(name)
.content(content)
.price(price)
.deadline(deadline)
.status(status)
.university(university)
.pickupOption(pickupOption)
.bankName(bankName)
.accountNumber(accountNumber)
.accountName(accountName)
.registerStatus(registerStatus)
.seller(seller)
.category(category)
.build();

return product;
}
}

// 수정 dto
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class UpdateProductDto {
private String name;
private String content;
private Long price;
private LocalDate deadline;
private ProductStatus status;
private String university;
private PickupOption pickupOption;
private String bankName;
private String accountNumber;
private String accountName;
private Long categoryId;
// private List<OptionRequest> options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import umc.unimade.domain.accounts.entity.Seller;
import umc.unimade.global.registerStatus.RegisterStatus;

import java.time.LocalDate;
Expand Down Expand Up @@ -60,18 +61,33 @@ public class ProductRegister {
@Column(name = "reason")
private String reason; // 거부 or 보류 사유

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "seller_id")
private Seller seller;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;

@OneToMany(mappedBy = "productRegister", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<ProductsImage> productImages = new ArrayList<>();

// @OneToMany(mappedBy = "productRegister", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
// private List<Options> options = new ArrayList<>();

@PostPersist
private void setRegisterStatus() {
registerStatus = RegisterStatus.PENDING;
}

// public void setOptions(List<Options> options) {
// this.options = options;
// }

public void setProductImages(List<ProductsImage> productImages) {
this.productImages = productImages;
}

public void changeStatus(RegisterStatus registerStatus) {
this.registerStatus = registerStatus;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package umc.unimade.domain.products.entity;

public enum ProductStatus {
SOLDOUT
SELLING, SOLDOUT
}
18 changes: 18 additions & 0 deletions src/main/java/umc/unimade/domain/products/entity/Products.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package umc.unimade.domain.products.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import umc.unimade.domain.accounts.entity.Seller;
import umc.unimade.domain.favorite.entity.FavoriteProduct;
import umc.unimade.domain.orders.entity.Orders;
import umc.unimade.domain.orders.entity.PurchaseForm;
import umc.unimade.domain.products.dto.ProductRequest;
import umc.unimade.domain.qna.entity.Questions;
import umc.unimade.domain.review.entity.Review;
import umc.unimade.global.common.BaseEntity;
Expand Down Expand Up @@ -60,10 +62,12 @@ public class Products extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "seller_id")
@JsonIgnore
private Seller seller;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
@JsonIgnore
private Category category;

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Expand All @@ -86,4 +90,18 @@ public class Products extends BaseEntity {

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<PurchaseForm> PurchaseForms = new ArrayList<>();

public void updateProduct(ProductRequest.UpdateProductDto request, Category category) {
this.name = request.getName();
this.content = request.getContent();
this.price = request.getPrice();
this.deadline = request.getDeadline();
this.status = request.getStatus();
this.university = request.getUniversity();
this.pickupOption = request.getPickupOption();
this.bankName = request.getBankName();
this.accountNumber = request.getAccountNumber();
this.accountName = request.getAccountName();
this.category = category;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package umc.unimade.domain.products.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import umc.unimade.global.common.BaseEntity;
Expand All @@ -26,6 +27,7 @@ public class ProductsImage extends BaseEntity {

@ManyToOne
@JoinColumn(name = "product_register_id")
@JsonIgnore
private ProductRegister productRegister;

public void setProduct(Products product) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package umc.unimade.domain.products.exception;

import umc.unimade.global.common.BaseErrorCode;
import umc.unimade.global.common.exception.CustomException;

public class ProductExceptionHandler extends CustomException {
public ProductExceptionHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package umc.unimade.domain.products.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import umc.unimade.domain.products.entity.Category;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//package umc.unimade.domain.products.repository;
//
//import org.springframework.data.jpa.repository.JpaRepository;
//import org.springframework.stereotype.Repository;
//import umc.unimade.domain.products.entity.Options;
//
//@Repository
//public interface OptionsRepository extends JpaRepository<Options, Long> {
//}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

public interface ProductRegisterRepository extends JpaRepository<ProductRegister, Long> {
Page<ProductRegister> findByRegisterStatus(RegisterStatus status, Pageable pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package umc.unimade.domain.products.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import umc.unimade.domain.products.entity.ProductsImage;

@Repository
public interface ProductsImageRepository extends JpaRepository<ProductsImage, Long> {
}
Loading

0 comments on commit 79b687d

Please sign in to comment.