From 5394a904ce0d0fe59c56f0b14cba557efd9af127 Mon Sep 17 00:00:00 2001 From: wintiger98 Date: Fri, 3 May 2024 08:51:08 +0000 Subject: [PATCH 01/10] =?UTF-8?q?=E2=9C=A8Feat=20:=20Category=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80=20=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/application/CategoryService.java | 38 +++++++++++++++++++ .../product/dao/CategoryRepository.java | 8 ++++ .../domain/product/domain/Category.java | 23 +++++++++++ .../domain/product/domain/Product.java | 15 ++++---- 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java create mode 100644 spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dao/CategoryRepository.java create mode 100644 spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Category.java diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java new file mode 100644 index 0000000..5d4196a --- /dev/null +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java @@ -0,0 +1,38 @@ +package com.ssafy.springbootapi.domain.product.application; + +import com.ssafy.springbootapi.domain.product.dao.CategoryRepository; +import com.ssafy.springbootapi.domain.product.domain.Category; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class CategoryService { + private final CategoryRepository categoryRepository; + + /** + * 카테고리 전체 조회 + * @return List(Category) + */ + public List getAllCategories() { + return categoryRepository.findAll(); + } + + /** + * id로 카테고리 조회 + * @param id : category id + * @return Category + */ + public Category getCategoryById(Integer id) { + return categoryRepository.getReferenceById(id); + } + + public Boolean createCategory(String name) { + Category category = new Category(); + category.setName(name); + categoryRepository.save(category); + return true; + } +} diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dao/CategoryRepository.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dao/CategoryRepository.java new file mode 100644 index 0000000..8897fa1 --- /dev/null +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dao/CategoryRepository.java @@ -0,0 +1,8 @@ +package com.ssafy.springbootapi.domain.product.dao; + +import com.ssafy.springbootapi.domain.product.domain.Category; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CategoryRepository extends JpaRepository { + +} diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Category.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Category.java new file mode 100644 index 0000000..61f39b7 --- /dev/null +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Category.java @@ -0,0 +1,23 @@ +package com.ssafy.springbootapi.domain.product.domain; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "category") +@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) +public class Category { + @Id + @GeneratedValue + @Column(name = "id", unique = true) + private Integer id; + + @Column(name = "name") + private String name; +} diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java index 38020aa..bcf87c2 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java @@ -41,25 +41,24 @@ public class Product { private String name; @Column(name = "price", nullable = false) - private int price; + private Integer price; @Column(name = "description", nullable = false) private String description; - @Column(nullable = false) - private int category; + @OneToOne + @JoinColumn(name="category_id", nullable = false) + private Category category; @Column(nullable = false) - private int stock; + private Integer stock; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) private User user; -// @Column(name = "user_id") -// private int userId; + // test code를 위해 추가 - public void updateInfo(int category, int stock, String imageUrl) { - this.category = category; + public void updateInfo(int stock, String imageUrl) { this.stock = stock; this.imageUrl = imageUrl; } From 8f148b58f3265fa703fd80958c974e9719e5834d Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:28:48 +0000 Subject: [PATCH 02/10] =?UTF-8?q?:sparkles:=20Feat=20:=20Product=20?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20category=20->=20category=5Fid(fk)=20?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8?= =?UTF-8?q?=ED=95=9C=20Product=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=20-=20=EA=B0=80=EB=B3=80=EC=A0=81=EC=9D=B8=20category=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=EC=84=B1=20=EB=95=8C=EB=AC=B8=EC=97=90=20enu?= =?UTF-8?q?m=20=ED=83=80=EC=9E=85=EC=9C=BC=EB=A1=9C=20=EA=B4=80=EB=A6=AC?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=ED=85=8C=EC=9D=B4=EB=B8=94=20=EA=B4=80=EB=A6=AC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/application/ProductService.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java index f04e705..93ca008 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java @@ -4,28 +4,30 @@ import java.util.Optional; import java.util.stream.Collectors; +import com.ssafy.springbootapi.domain.product.dao.CategoryRepository; import com.ssafy.springbootapi.domain.product.dao.ProductRepository; +import com.ssafy.springbootapi.domain.product.domain.Category; import com.ssafy.springbootapi.domain.product.domain.Product; import com.ssafy.springbootapi.domain.product.domain.ProductMapper; import com.ssafy.springbootapi.domain.product.dto.ProductInput; import com.ssafy.springbootapi.domain.product.dto.ProductListOutput; import com.ssafy.springbootapi.domain.product.dto.ProductOutput; import com.ssafy.springbootapi.domain.product.dto.ProductUpdate; +import com.ssafy.springbootapi.domain.product.exception.NotFoundCategoryException; import com.ssafy.springbootapi.domain.product.exception.NotFoundProductException; import com.ssafy.springbootapi.domain.user.dao.UserRepository; import com.ssafy.springbootapi.domain.user.domain.User; import com.ssafy.springbootapi.domain.user.exception.UserNotFoundException; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; -import org.springframework.web.server.ResponseStatusException; @Service @RequiredArgsConstructor public class ProductService { private final ProductRepository productRepository; private final UserRepository userRepository; + private final CategoryService categoryService; private final ProductMapper productMapper; @@ -55,6 +57,7 @@ public ProductOutput getProductById(Long id) { * @param productInput : Product 생성 데이터 * @return ProductOutput(DTO) */ + @Transactional public ProductOutput insertProduct(ProductInput productInput) { // user_id로 User 객체 찾기 Optional userOptional = userRepository.findById(productInput.getUser_id()); @@ -62,11 +65,14 @@ public ProductOutput insertProduct(ProductInput productInput) { new UserNotFoundException("User not found with id: " + productInput.getUser_id()) ); + Category category = findCategoryByIdOrThrow(productInput.getCategory_id()); + // ProductInput에서 Product 엔티티로 변환 Product product = productMapper.toEntity(productInput); // User 객체를 Product 엔티티에 설정 product.setUser(user); + product.setCategory(category); // Product 엔티티를 DB에 저장하고, 저장된 엔티티를 ProductOutput DTO로 변환하여 반환 return productMapper.toProductOutput(productRepository.save(product)); @@ -78,6 +84,7 @@ public ProductOutput insertProduct(ProductInput productInput) { * @param id : 삭제할 product id * @return ProductOutput(DTO) */ + @Transactional public ProductOutput removeProduct(Long id) { Product toRemove = findProductByIdOrThrow(id); productRepository.delete(toRemove); @@ -93,6 +100,10 @@ public ProductOutput removeProduct(Long id) { @Transactional public ProductOutput updateProduct(Long id, ProductUpdate productUpdate) { Product productToUpdate = findProductByIdOrThrow(id); + if(productUpdate.getCategory_id() != null) { + Category category = findCategoryByIdOrThrow(productUpdate.getCategory_id()); + productToUpdate.setCategory(category); + } productMapper.updateProduct(productUpdate, productToUpdate); return productMapper.toProductOutput(productRepository.save(productToUpdate)); } @@ -106,4 +117,13 @@ private Product findProductByIdOrThrow(Long id) { return productRepository.findById(id) .orElseThrow(() -> new NotFoundProductException("Product not found with id: " + id)); } + + /** + * id로 카테고리 찾기. 없을 경우 Exception 발생 + * @param categoryId + * @return + */ + private Category findCategoryByIdOrThrow(Integer categoryId) { + return categoryService.getCategoryById(categoryId); + } } From 7982abf0f8b1189ad0b0058cab9daa76bdc958a0 Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:30:19 +0000 Subject: [PATCH 03/10] =?UTF-8?q?:sparkles:=20Feat=20:=20category=20?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20Product=20DTO=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ssafy/springbootapi/domain/product/dto/ProductBase.java | 2 +- .../ssafy/springbootapi/domain/product/dto/ProductInput.java | 5 +++-- .../springbootapi/domain/product/dto/ProductOutput.java | 3 --- .../springbootapi/domain/product/dto/ProductUpdate.java | 5 ++--- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductBase.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductBase.java index 961ce63..c336471 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductBase.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductBase.java @@ -10,5 +10,5 @@ public class ProductBase { private String imageUrl; private String name; - private int price; + private Integer price; } diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductInput.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductInput.java index 986d395..5c72375 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductInput.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductInput.java @@ -11,8 +11,9 @@ @SuperBuilder public class ProductInput extends ProductBase { private String description; - private int category; - private int stock; + @NotBlank(message = "category id is required") + private Integer category_id; + private Integer stock; @NotBlank(message = "user id is required") private Long user_id; } \ No newline at end of file diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductOutput.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductOutput.java index ba3dc8b..4462612 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductOutput.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductOutput.java @@ -1,7 +1,6 @@ package com.ssafy.springbootapi.domain.product.dto; import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @@ -12,7 +11,5 @@ @SuperBuilder public class ProductOutput extends ProductListOutput{ private String description; - private int category; private int stock; -// private int user_id; } diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductUpdate.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductUpdate.java index 473a6b1..555734b 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductUpdate.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/dto/ProductUpdate.java @@ -3,7 +3,6 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; import lombok.experimental.SuperBuilder; @AllArgsConstructor @@ -12,6 +11,6 @@ @SuperBuilder public class ProductUpdate extends ProductBase { private String description; - private int category; - private int stock; + private Integer category_id; + private Integer stock; } From 35af35cdc4c977849c01e5827a91cbcb47ac5c59 Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:30:42 +0000 Subject: [PATCH 04/10] =?UTF-8?q?:sparkles:=20Feat=20:=20category=20?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20Product=20Test=20Code=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/ProductServiceTest.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/spring-boot-api/src/test/java/com/ssafy/springbootapi/domain/product/application/ProductServiceTest.java b/spring-boot-api/src/test/java/com/ssafy/springbootapi/domain/product/application/ProductServiceTest.java index e21ab2a..0480de2 100644 --- a/spring-boot-api/src/test/java/com/ssafy/springbootapi/domain/product/application/ProductServiceTest.java +++ b/spring-boot-api/src/test/java/com/ssafy/springbootapi/domain/product/application/ProductServiceTest.java @@ -1,6 +1,7 @@ package com.ssafy.springbootapi.domain.product.application; import com.ssafy.springbootapi.domain.product.dao.ProductRepository; +import com.ssafy.springbootapi.domain.product.domain.Category; import com.ssafy.springbootapi.domain.product.domain.Product; import com.ssafy.springbootapi.domain.product.domain.ProductMapper; import com.ssafy.springbootapi.domain.product.dto.ProductInput; @@ -32,6 +33,9 @@ class ProductServiceTest { @Mock private ProductRepository productRepository; + @Mock + private CategoryService categoryService; + @Mock private ProductMapper productMapper; @@ -93,16 +97,26 @@ class ProductServiceTest { void 상품삽입성공테스트() { // given Long userId = 1L; // 테스트용 user_id 값 + Integer categoryId = 1; // 테스트용 카테고리 id값 + ProductInput productInput = new ProductInput(); productInput.setUser_id(userId); // ProductInput에 user_id 설정 + productInput.setCategory_id(categoryId); + Product product = new Product(); User user = new User(); // 새 User 객체 생성 user.setId(userId); // User 객체에 id 설정 + + Category category = new Category(); // 새 카테고리 생성 + category.setId(categoryId); + ProductOutput productOutput = new ProductOutput(); when(userRepository.findById(userId)).thenReturn(java.util.Optional.of(user)); // userRepository.findById 호출 시 user 반환 + when(categoryService.getCategoryById(categoryId)).thenReturn(category); when(productMapper.toEntity(productInput)).thenReturn(product); product.setUser(user); // Product 객체에 User 설정 + product.setCategory(category); when(productRepository.save(product)).thenReturn(product); when(productMapper.toProductOutput(product)).thenReturn(productOutput); @@ -163,19 +177,17 @@ class ProductServiceTest { @Test void 상품수정성공테스트() { // given - int newCategory = 2; int newStock = 20; String newImageUrl = "newImageUrl"; ProductUpdate productUpdate = new ProductUpdate(); productUpdate.builder() - .category(newCategory) .stock(newStock) .imageUrl(newImageUrl).build(); Product product = new Product(); - product.updateInfo(newCategory, newStock, newImageUrl); + product.updateInfo(newStock, newImageUrl); ProductOutput productOutput = new ProductOutput(); @@ -190,7 +202,6 @@ class ProductServiceTest { // then assertThat(result).isEqualTo(productOutput); - assertThat(product.getCategory()).isEqualTo(newCategory); assertThat(product.getStock()).isEqualTo(newStock); assertThat(product.getImageUrl()).isEqualTo(newImageUrl); } From 7dfedb55b37f40839f1c8f66d1774c771991ef22 Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:31:25 +0000 Subject: [PATCH 05/10] =?UTF-8?q?:sparkles:=20Feat=20:=20Category=20Servic?= =?UTF-8?q?e=EB=82=B4=20CRUD=20=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C=20+?= =?UTF-8?q?=20Transactional=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/application/CategoryService.java | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java index 5d4196a..e8dc4d5 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/CategoryService.java @@ -2,6 +2,8 @@ import com.ssafy.springbootapi.domain.product.dao.CategoryRepository; import com.ssafy.springbootapi.domain.product.domain.Category; +import com.ssafy.springbootapi.domain.product.exception.NotFoundCategoryException; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -26,13 +28,58 @@ public List getAllCategories() { * @return Category */ public Category getCategoryById(Integer id) { - return categoryRepository.getReferenceById(id); + + return categoryRepository.findById(id).orElseThrow(() + -> new NotFoundCategoryException("Category not found with id : " + id)); } - public Boolean createCategory(String name) { - Category category = new Category(); + /** + * 카테고리 생성 + * @param name : 카테고리 이름 + * @return 생성된 카테고리 + */ + @Transactional + public Category createCategory(String name) { + try { + Category category = new Category(); + category.setName(name); + return categoryRepository.save(category); + } catch(Exception E) { + return null; + } + } + + /** + * 카테고리 이름 수정 + * @param id : 수정할 카테고리 아이디 + * @param name : 수정할 카테고리 이름 + * @return 수정된 카테고리 + */ + @Transactional + public Category updateCategory(Integer id, String name) { + Category category = findCategoryByIdOrThrow(id); category.setName(name); - categoryRepository.save(category); - return true; + return category; + } + + /** + * 카테고리 삭제 + * @param id : 삭제할 카테고리 아이디 + * @return 성공 여부 + */ + @Transactional + public Boolean deleteCategory(Integer id) { + Category category = findCategoryByIdOrThrow(id); + try { + categoryRepository.delete(category); + return true; + } catch(Exception e) { + return false; + } + } + + private Category findCategoryByIdOrThrow(Integer id) { + return categoryRepository.findById(id) + .orElseThrow(() -> new NotFoundCategoryException("Category Not Found")); } } From 3a61147dd273e08f9ed9eaf95352a50cb3e2261e Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:31:42 +0000 Subject: [PATCH 06/10] =?UTF-8?q?:sparkles:=20Feat=20:=20Category=20Contro?= =?UTF-8?q?ller=20CRUD=20=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/api/CategoryController.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/api/CategoryController.java diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/api/CategoryController.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/api/CategoryController.java new file mode 100644 index 0000000..01e9195 --- /dev/null +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/api/CategoryController.java @@ -0,0 +1,47 @@ +package com.ssafy.springbootapi.domain.product.api; + +import com.ssafy.springbootapi.domain.product.application.CategoryService; +import com.ssafy.springbootapi.domain.product.domain.Category; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Tag(name = "Category", description = "Category 관련 API 입니다.") +@RestController +@RequiredArgsConstructor +//@CrossOrigin // spring security 디펜던시 삭제로 인한 주석처리 +@RequestMapping("/api/v1/categories") +public class CategoryController { + private final CategoryService categoryService; + + @Operation(summary = "모든 카테고리 조회") + @GetMapping + public ResponseEntity> getAllCategories() { + List categories = categoryService.getAllCategories(); + return ResponseEntity.status(HttpStatus.OK).body(categories); + } + + @Operation(summary = "카테고리 생성") + @PostMapping + public ResponseEntity createCategory(@RequestBody String name) { + Category category = categoryService.createCategory(name); + return ResponseEntity.status(HttpStatus.CREATED).body(category); + } + + @Operation(summary = "카테고리 수정") + @PutMapping("/{id}") + public ResponseEntity updateCategory(@PathVariable Integer id, @RequestBody String name) { + return ResponseEntity.status(HttpStatus.OK).body(categoryService.updateCategory(id, name)); + } + + @Operation(summary = "카테고리 삭제") + @DeleteMapping("/{id}") + public ResponseEntity deleteCategory(@PathVariable Integer id) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(categoryService.deleteCategory(id)); + } +} From b15532565f55335a95e4c67be34146defb70b9e3 Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:31:58 +0000 Subject: [PATCH 07/10] =?UTF-8?q?:sparkles:=20Feat=20:=20Category=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=BB=A4=EC=8A=A4=ED=85=80=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/exception/NotFoundCategoryException.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/exception/NotFoundCategoryException.java diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/exception/NotFoundCategoryException.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/exception/NotFoundCategoryException.java new file mode 100644 index 0000000..ff9b675 --- /dev/null +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/exception/NotFoundCategoryException.java @@ -0,0 +1,7 @@ +package com.ssafy.springbootapi.domain.product.exception; + +public class NotFoundCategoryException extends RuntimeException{ + public NotFoundCategoryException(String message) { + super(message); + } +} From 4c5ed76daced2370444772b2eea72467a838db46 Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:32:24 +0000 Subject: [PATCH 08/10] =?UTF-8?q?:sparkles:=20Feat=20:=20Category=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=BB=A4=EC=8A=A4=ED=85=80=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20=EC=A0=84=EC=97=AD=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootapi/global/error/GlobalExceptionHandler.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/global/error/GlobalExceptionHandler.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/global/error/GlobalExceptionHandler.java index 26f8a35..12f9ce4 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/global/error/GlobalExceptionHandler.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/global/error/GlobalExceptionHandler.java @@ -1,5 +1,6 @@ package com.ssafy.springbootapi.global.error; + import com.ssafy.springbootapi.domain.product.exception.NotFoundCategoryException; import com.ssafy.springbootapi.domain.product.exception.NotFoundProductException; import com.ssafy.springbootapi.domain.user.exception.UserNotFoundException; import org.springframework.http.HttpStatus; @@ -15,6 +16,11 @@ public ResponseEntity handleNotFoundProductException(NotFoundProductException return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); } + @ExceptionHandler(NotFoundCategoryException.class) + public ResponseEntity handleNotFoundCategoryException(NotFoundCategoryException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); + } + @ExceptionHandler(UserNotFoundException.class) public ResponseEntity handleUserNotFoundException(UserNotFoundException ex) { return ResponseEntity From f06048693a768bdd15bb091a8b5678d6cdcbd6be Mon Sep 17 00:00:00 2001 From: ??? Date: Mon, 6 May 2024 12:34:12 +0000 Subject: [PATCH 09/10] =?UTF-8?q?:ambulance:=20Fix=20:=20Unused=20import?= =?UTF-8?q?=20=EA=B5=AC=EB=AC=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/product/application/ProductService.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java index 93ca008..582aab9 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/application/ProductService.java @@ -4,7 +4,6 @@ import java.util.Optional; import java.util.stream.Collectors; -import com.ssafy.springbootapi.domain.product.dao.CategoryRepository; import com.ssafy.springbootapi.domain.product.dao.ProductRepository; import com.ssafy.springbootapi.domain.product.domain.Category; import com.ssafy.springbootapi.domain.product.domain.Product; @@ -13,7 +12,6 @@ import com.ssafy.springbootapi.domain.product.dto.ProductListOutput; import com.ssafy.springbootapi.domain.product.dto.ProductOutput; import com.ssafy.springbootapi.domain.product.dto.ProductUpdate; -import com.ssafy.springbootapi.domain.product.exception.NotFoundCategoryException; import com.ssafy.springbootapi.domain.product.exception.NotFoundProductException; import com.ssafy.springbootapi.domain.user.dao.UserRepository; import com.ssafy.springbootapi.domain.user.domain.User; @@ -120,8 +118,8 @@ private Product findProductByIdOrThrow(Long id) { /** * id로 카테고리 찾기. 없을 경우 Exception 발생 - * @param categoryId - * @return + * @param categoryId : 찾을 category 아이디 + * @return 카테고리 */ private Category findCategoryByIdOrThrow(Integer categoryId) { return categoryService.getCategoryById(categoryId); From 40dfce77952697c916d4ccfeafdea2fe3f20daec Mon Sep 17 00:00:00 2001 From: ??? Date: Wed, 8 May 2024 11:51:38 +0000 Subject: [PATCH 10/10] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8FFix=20:=20OneToOne?= =?UTF-8?q?=20->=20ManyToOne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ssafy/springbootapi/domain/product/domain/Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java index bcf87c2..9d00673 100644 --- a/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java +++ b/spring-boot-api/src/main/java/com/ssafy/springbootapi/domain/product/domain/Product.java @@ -46,7 +46,7 @@ public class Product { @Column(name = "description", nullable = false) private String description; - @OneToOne + @ManyToOne @JoinColumn(name="category_id", nullable = false) private Category category;