diff --git a/src/main/java/com/backend/soullive_a/controller/ProductController.java b/src/main/java/com/backend/soullive_a/controller/ProductController.java index 96cc0e1..98cf5e3 100644 --- a/src/main/java/com/backend/soullive_a/controller/ProductController.java +++ b/src/main/java/com/backend/soullive_a/controller/ProductController.java @@ -50,4 +50,16 @@ public BaseResponse getProduct(@PathVariable Long id) { .data(productService.getProduct(id)) .build(); } + + @DeleteMapping("/{id}") + public BaseResponse deleteProduct(@PathVariable Long id) { + + productService.deleteProduct(id); + + return BaseResponse.builder() + .isSuccess(true) + .code(200) + .message("광고 상품 삭제 성공") + .build(); + } } diff --git a/src/main/java/com/backend/soullive_a/entity/Age.java b/src/main/java/com/backend/soullive_a/entity/Age.java index 4bc7531..6530ff5 100644 --- a/src/main/java/com/backend/soullive_a/entity/Age.java +++ b/src/main/java/com/backend/soullive_a/entity/Age.java @@ -3,6 +3,8 @@ import com.backend.soullive_a.constant.AgeType; import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @Table(name = "AGE") @@ -18,6 +20,7 @@ public class Age { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PRODUCT_ID", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) private Product product; @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/backend/soullive_a/entity/BrandImage.java b/src/main/java/com/backend/soullive_a/entity/BrandImage.java index 3ba700b..36c4ef3 100644 --- a/src/main/java/com/backend/soullive_a/entity/BrandImage.java +++ b/src/main/java/com/backend/soullive_a/entity/BrandImage.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @Table(name = "BRAND_IMAGE") @@ -17,6 +19,7 @@ public class BrandImage { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PRODUCT_ID", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) private Product product; @Column(name = "BRAND_IMAGE") diff --git a/src/main/java/com/backend/soullive_a/entity/Gender.java b/src/main/java/com/backend/soullive_a/entity/Gender.java index cfb5b1d..e5f9f9e 100644 --- a/src/main/java/com/backend/soullive_a/entity/Gender.java +++ b/src/main/java/com/backend/soullive_a/entity/Gender.java @@ -3,6 +3,8 @@ import com.backend.soullive_a.constant.GenderType; import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @Table(name = "GENDER") @@ -18,6 +20,7 @@ public class Gender { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PRODUCT_ID", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) private Product product; @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/backend/soullive_a/entity/ProductImage.java b/src/main/java/com/backend/soullive_a/entity/ProductImage.java index 179e7b5..5c70f63 100644 --- a/src/main/java/com/backend/soullive_a/entity/ProductImage.java +++ b/src/main/java/com/backend/soullive_a/entity/ProductImage.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @Table(name = "PRODUCT_IMAGE") @@ -17,6 +19,7 @@ public class ProductImage { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PRODUCT_ID", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) private Product product; @Column(name = "PRODUCT_IMAGE", nullable = false) diff --git a/src/main/java/com/backend/soullive_a/entity/Range.java b/src/main/java/com/backend/soullive_a/entity/Range.java index 8a78252..abb4735 100644 --- a/src/main/java/com/backend/soullive_a/entity/Range.java +++ b/src/main/java/com/backend/soullive_a/entity/Range.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import lombok.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @Table(name = "TARGETRANGE") @@ -17,6 +19,7 @@ public class Range { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PRODUCT_ID", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) private Product product; @Column(name = "RANGE_VALUE", nullable = false) diff --git a/src/main/java/com/backend/soullive_a/entity/model/ProductModel.java b/src/main/java/com/backend/soullive_a/entity/model/ProductModel.java new file mode 100644 index 0000000..48bf341 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/entity/model/ProductModel.java @@ -0,0 +1,43 @@ +package com.backend.soullive_a.entity.model; + +import com.backend.soullive_a.entity.Product; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.time.LocalDateTime; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "PRODUCT_MODEL") +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ProductModel { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "ID") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "PRODUCT_ID", nullable = false) + private Product product; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "MODEL_ID", nullable = false) + private Model model; + + @Column(name = "SEARCH_TIME", nullable = false) + private LocalDateTime searchTime; +} diff --git a/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java b/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java index 3ebc62c..741bd6b 100644 --- a/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java +++ b/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java @@ -1,11 +1,14 @@ package com.backend.soullive_a.exception.base; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @NoArgsConstructor +@JsonInclude(Include.NON_EMPTY) public class BaseResponse { private boolean isSuccess; diff --git a/src/main/java/com/backend/soullive_a/repository/AgeRepository.java b/src/main/java/com/backend/soullive_a/repository/AgeRepository.java index 1ed8b96..ac8fec3 100644 --- a/src/main/java/com/backend/soullive_a/repository/AgeRepository.java +++ b/src/main/java/com/backend/soullive_a/repository/AgeRepository.java @@ -1,6 +1,7 @@ package com.backend.soullive_a.repository; import com.backend.soullive_a.entity.Age; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface AgeRepository extends JpaRepository { List findAllByProductId(Long productId); + Optional findByProductId(Long productId); } \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java b/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java index 1205189..1b03fc9 100644 --- a/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java +++ b/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java @@ -1,6 +1,7 @@ package com.backend.soullive_a.repository; import com.backend.soullive_a.entity.BrandImage; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface BrandImageRepository extends JpaRepository { List findAllByProductId(Long productId); + Optional findByProductId(Long productId); } diff --git a/src/main/java/com/backend/soullive_a/repository/GenderRepository.java b/src/main/java/com/backend/soullive_a/repository/GenderRepository.java index b059aca..227da13 100644 --- a/src/main/java/com/backend/soullive_a/repository/GenderRepository.java +++ b/src/main/java/com/backend/soullive_a/repository/GenderRepository.java @@ -1,6 +1,7 @@ package com.backend.soullive_a.repository; import com.backend.soullive_a.entity.Gender; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface GenderRepository extends JpaRepository { List findAllByProductId(Long productId); + Optional findByProductId(Long productId); } \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java b/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java index a61d09f..35d34ce 100644 --- a/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java +++ b/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java @@ -1,6 +1,7 @@ package com.backend.soullive_a.repository; import com.backend.soullive_a.entity.ProductImage; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface ProductImageRepository extends JpaRepository { List findAllByProductId(Long productId); + Optional findByProductId(Long productId); } \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/ProductModelRepository.java b/src/main/java/com/backend/soullive_a/repository/ProductModelRepository.java new file mode 100644 index 0000000..6f695e5 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/ProductModelRepository.java @@ -0,0 +1,8 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.model.ProductModel; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProductModelRepository extends JpaRepository { + +} diff --git a/src/main/java/com/backend/soullive_a/repository/RangeRepository.java b/src/main/java/com/backend/soullive_a/repository/RangeRepository.java index e286147..775426a 100644 --- a/src/main/java/com/backend/soullive_a/repository/RangeRepository.java +++ b/src/main/java/com/backend/soullive_a/repository/RangeRepository.java @@ -1,6 +1,7 @@ package com.backend.soullive_a.repository; import com.backend.soullive_a.entity.Range; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +10,5 @@ @Repository public interface RangeRepository extends JpaRepository { List findAllByProductId(Long productId); + Optional findByProductId(Long productId); } \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/service/ProductService.java b/src/main/java/com/backend/soullive_a/service/ProductService.java index 49f6616..e76d032 100644 --- a/src/main/java/com/backend/soullive_a/service/ProductService.java +++ b/src/main/java/com/backend/soullive_a/service/ProductService.java @@ -11,4 +11,6 @@ public interface ProductService { ProductResponse getProduct(Long id); List getAllProduct(); + + void deleteProduct(Long id); } diff --git a/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java index 0e25955..fbfa3d7 100644 --- a/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java +++ b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java @@ -128,4 +128,10 @@ public List getAllProduct() { } return productResponses; } + + @Override + @Transactional + public void deleteProduct(Long id) { + productRepository.deleteById(id); + } } \ No newline at end of file diff --git a/src/test/java/com/backend/soullive_a/service/impl/ProductServiceImplTest.java b/src/test/java/com/backend/soullive_a/service/impl/ProductServiceImplTest.java new file mode 100644 index 0000000..18ffc0f --- /dev/null +++ b/src/test/java/com/backend/soullive_a/service/impl/ProductServiceImplTest.java @@ -0,0 +1,108 @@ +//package com.backend.soullive_a.service.impl; +// +// +// +//import com.backend.soullive_a.constant.AgeType; +//import com.backend.soullive_a.constant.GenderType; +//import com.backend.soullive_a.entity.Age; +//import com.backend.soullive_a.entity.BrandImage; +//import com.backend.soullive_a.entity.Gender; +//import com.backend.soullive_a.entity.Product; +//import com.backend.soullive_a.entity.ProductImage; +//import com.backend.soullive_a.entity.Range; +//import com.backend.soullive_a.repository.AgeRepository; +//import com.backend.soullive_a.repository.BrandImageRepository; +//import com.backend.soullive_a.repository.GenderRepository; +//import com.backend.soullive_a.repository.ProductImageRepository; +//import com.backend.soullive_a.repository.ProductRepository; +//import com.backend.soullive_a.repository.RangeRepository; +//import org.junit.jupiter.api.Assertions; +//import org.junit.jupiter.api.Test; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.test.annotation.Rollback; +//import org.springframework.transaction.annotation.Transactional; +// +//@SpringBootTest +//class ProductServiceImplTest { +// +// @Autowired +// ProductRepository productRepository; +// @Autowired +// AgeRepository ageRepository; +// @Autowired +// BrandImageRepository brandImageRepository; +// @Autowired +// GenderRepository genderRepository; +// @Autowired +// ProductImageRepository productImageRepository; +// @Autowired +// RangeRepository rangeRepository; +// +// @Test +// @Transactional +// @Rollback(value = false) +// void deleteProduct() { +// +// /* +// * given +// */ +// // 광고 상품 entity 생성 +// Product product = Product.builder() +// .company("큐시즘") +// .brand("소울라이브-A") +// .product("백엔드 개발") +// .characteristic("characteristic") +// .build(); +// +// productRepository.save(product); +// // 브랜드 이미지 entity 생성 +// BrandImage brandImage = BrandImage.builder() +// .product(product) +// .brandImage("브랜드 이미지 url") +// .build(); +// brandImageRepository.save(brandImage); +// // 상품 이미지 entity 생성 +// ProductImage productImage = ProductImage.builder() +// .product(product) +// .productImage("상품 이미지 url") +// .build(); +// productImageRepository.save(productImage); +// // 성별 entity 생성 +// Gender gender = Gender.builder() +// .product(product) +// .gender(GenderType.MALE) +// .build(); +// genderRepository.save(gender); +// // 나이 entity 생성 +// Age age = Age.builder() +// .product(product) +// .age(AgeType.FIFTY) +// .build(); +// ageRepository.save(age); +// // 범주 entity 생성 +// Range range = Range.builder() +// .product(product) +// .range("범주 예시") +// .build(); +// rangeRepository.save(range); +// +// +// /* +// * when +// */ +// productRepository.deleteById(1L); +// +// +// /* +// * then +// */ +// Assertions.assertTrue(ageRepository.findByProductId(1L).isEmpty()); +// Assertions.assertTrue(brandImageRepository.findByProductId(1L).isEmpty()); +// Assertions.assertTrue(genderRepository.findByProductId(1L).isEmpty()); +// Assertions.assertTrue(productImageRepository.findByProductId(1L).isEmpty()); +// Assertions.assertTrue(rangeRepository.findByProductId(1L).isEmpty()); +// +// } +//} +//