Skip to content

Commit

Permalink
create new category when adding product
Browse files Browse the repository at this point in the history
  • Loading branch information
casyazmon committed Jul 22, 2023
1 parent 29f9ca0 commit 89169c8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.kasina.automobileapi.controller;

import com.kasina.automobileapi.dto.ProductDto;

import com.kasina.automobileapi.model.Category;
import com.kasina.automobileapi.model.Product;
import com.kasina.automobileapi.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/kasina/automobileapi/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Builder
@Entity
@Table(name = "products")
@NoArgsConstructor

@AllArgsConstructor
public class Product {
@Id
Expand All @@ -35,13 +35,18 @@ public class Product {
@JoinTable(name = "product_category",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "category_id"))
private Set<Category> categories = new HashSet<>();
private Set<Category> categories;

public Product() {
this.categories = new HashSet<>();
}

public void addCategory(Category category) {
this.categories.add(category);
categories.add(category);
category.getProducts().add(this);
}


public void removeCategory(Category category) {
categories.remove(category);
category.getProducts().remove(this);
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/kasina/automobileapi/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,29 @@ public class ProductService {
private final CategoryService categoryService;

public Product createProduct(ProductDto productDto) {
// First, save the product to get the product ID

Set<Category> categories = new HashSet<>();
User currentUser = userService.getCurrentUser();
var product = Product.builder()
Product product = new Product();
product = Product.builder()
.name(productDto.getName())
.description(productDto.getDescription())
.shortDescription(productDto.getShortDescription())
.price(productDto.getPrice())
.image(productDto.getImage())
.user(currentUser)
.build();
Product savedProduct = productRepository.save(product);

// Then, associate the product with the specified categories
for (String categoryName : productDto.getCategories()) {
/*categoryService.getCategoryByName(categoryName).ifPresent(category -> {
savedProduct.addCategory(category);
productRepository.save(savedProduct);
});*/

Category category = categoryService.getDefaultCategory(categoryName);
savedProduct.addCategory(category);
productRepository.save(savedProduct);
System.out.println("******##*#**# CATEGORY NAME: " + category.getName() +"**********");
categories.add(category);
}
product.setCategories(categories);

return savedProduct;
return productRepository.save(product);
}

public Product updateProduct(ProductDto productDto, Long id){
Expand Down

0 comments on commit 89169c8

Please sign in to comment.