diff --git a/frontend/spa-vue/src/pages/categories.vue b/frontend/spa-vue/src/pages/categories.vue index a3e2c3a..4b39e5d 100644 --- a/frontend/spa-vue/src/pages/categories.vue +++ b/frontend/spa-vue/src/pages/categories.vue @@ -19,9 +19,6 @@ - - - @@ -98,9 +95,6 @@ export default { const addModal = document.getElementById('editModal'); addModal.addEventListener('shown.bs.modal', function () { }) - const ModelForProducts = document.getElementById('ModelForProducts'); - addModal.addEventListener('shown.bs.modal', function () { - }) }, data() { @@ -169,22 +163,6 @@ export default { console.log(error); }); }, - getProductsFromCategory(category_id){ - axios.get(this.URL + `group/${category_id}/products`) - .then(response => { - this.products = response.data; - console.log(response.data); - }) - .catch(error => { - console.log(error); - }); - }, - OpenModelForProducts() { - document.getElementById("ModelForProducts").style.display = "block"; - }, - closeModelForProducts() { - document.getElementById("ModelForProducts").style.display = "none"; - }, } } diff --git a/frontend/spa-vue/src/pages/products.vue b/frontend/spa-vue/src/pages/products.vue index f5a5a95..a0e8993 100644 --- a/frontend/spa-vue/src/pages/products.vue +++ b/frontend/spa-vue/src/pages/products.vue @@ -2,6 +2,7 @@

Product Table

+ @@ -25,12 +26,54 @@ +
+ +
- + + + diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java index 39781ab..138fba2 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java @@ -3,6 +3,7 @@ package ru.ulstu.is.sbapp.HardwareShop.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Product; import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService; import ru.ulstu.is.sbapp.HardwareShop.services.ProductService; @@ -34,6 +35,16 @@ public class ProductController { .toList(); } + @GetMapping("/{id}/manufacturers") + public List getProductManufacturers(@PathVariable Long id){ + return productService.getProductManufacturers(id).stream().map(ManufacturerDTO::new).toList(); + } + + @PostMapping("/by-manufacturers") + public List getProductsByManufacturers(@RequestBody List manufacturers) { + return productService.getProductsByManufacturers(manufacturers); + } + @PostMapping public ProductDTO createProduct(@RequestBody @Valid ProductDTO productDTO) throws IOException{ return new ProductDTO(productService.addProduct(productDTO)); @@ -44,9 +55,14 @@ public class ProductController { return new ProductDTO(productService.updateProduct(id, productDTO)); } - @PostMapping("/{id}/{manufacturer}") - public void addManufacturer(@PathVariable Long id, @PathVariable Long manufacturer_id) { - productService.addManufacturersToProduct(id, manufacturer_id); + @PostMapping("/{id}/Manufacturer/{manufacturerId}") + public void addManufacturer(@PathVariable Long id, @PathVariable Long manufacturerId) { + productService.addManufacturersToProduct(id, manufacturerId); + } + + @DeleteMapping("/{id}/Manufacturer/{manufacturerId}") + public void removeManufacturer(@PathVariable Long id, @PathVariable Long manufacturerId) { + productService.removeManufacturersToProduct(id, manufacturerId); } @DeleteMapping("/{id}") diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Manufacturer.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Manufacturer.java index 99f3fca..e685637 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Manufacturer.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Manufacturer.java @@ -38,6 +38,8 @@ public class Manufacturer { public Long getId() { return id;} + public void setId(Long id) { this.id = id; } + public String getName() { return name; } public String getAddress() { return address; } diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Product.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Product.java index e6685f2..7ebd735 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Product.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Product.java @@ -58,7 +58,7 @@ public class Product { public String getName() { return name; } - public void setName() { this.name = name; } + public void setName(String name) { this.name = name; } public void setPrice(Integer price) { this.price = price; } diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/ProductRepository.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/ProductRepository.java index 5081de0..de382da 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/ProductRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/ProductRepository.java @@ -2,12 +2,14 @@ package ru.ulstu.is.sbapp.HardwareShop.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Product; import java.util.List; public interface ProductRepository extends JpaRepository { - @Query("Select manufacturerList from Product where id =:id") - List getManufacturers(Long id); + @Query("SELECT p FROM Product p WHERE p.manufacturerList IN :manufacturers") + List findByManufacturers(@Param("manufacturers") List manufacturers); } + diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java index 4058c5a..f01b578 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java @@ -73,11 +73,18 @@ public class ProductService { return productRepository.findAll(); } + @Transactional(readOnly = true) + public List getProductsByManufacturers(List manufacturers) { + return productRepository.findByManufacturers(manufacturers); + } + @Transactional public Product updateProduct(Long id, ProductDTO productDTO) { final Product currentProduct = findProduct(id); + currentProduct.setName(productDTO.getName()); currentProduct.setPrice(productDTO.getPrice()); - /*currentProduct.setPhoto(productDTO.getPhoto().getBytes(StandardCharsets.UTF_8));*/ + currentProduct.setPhoto(productDTO.getPhoto().getBytes(StandardCharsets.UTF_8)); + currentProduct.setCategory(categoryService.findCategory(productDTO.getCategory_id())); validatorUtil.validate(currentProduct); return productRepository.save(currentProduct); } @@ -121,8 +128,7 @@ public class ProductService { } @Transactional - public List getProductManufacturers(Long id){ - List manufacturers = productRepository.getManufacturers(id); - return manufacturers; + public List getProductManufacturers(Long id){ + return productRepository.findById(id).get().getManufacturerList(); } }