From b5775f0abb7e6410913eaac41b60bbef5e912058 Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Mon, 3 Apr 2023 16:31:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20image?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../sbapp/HardwareShop/models/Category.java | 3 - .../HardwareShop/models/Manufacturer.java | 3 +- .../is/sbapp/HardwareShop/models/Product.java | 19 ++++- .../HardwareShop/services/ProductService.java | 39 ++++++++++- .../ru/ulstu/is/sbapp/JpaProductTests.java | 69 ++++++++++++++++--- 6 files changed, 116 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index 6833345..3e90001 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,8 @@ repositories { } dependencies { + // https://mvnrepository.com/artifact/commons-io/commons-io + implementation group: 'commons-io', name: 'commons-io', version: '2.6' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Category.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Category.java index 1ce20ed..ec7a0ef 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Category.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Category.java @@ -10,9 +10,6 @@ public class Category { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @OneToMany(mappedBy = "category") - private List productList; - @Column private String name; 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 55ca96a..b9fb540 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 @@ -10,7 +10,8 @@ public class Manufacturer { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @ManyToMany(mappedBy = "manufacturerList") + @ManyToMany(fetch = FetchType.EAGER, + mappedBy = "manufacturerList") private List productList; @Column 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 b4b8927..0a74224 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 @@ -1,5 +1,8 @@ package ru.ulstu.is.sbapp.HardwareShop.models; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; + import java.util.List; import java.util.Objects; import javax.persistence.*; @@ -11,17 +14,22 @@ public class Product { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne + @LazyCollection(LazyCollectionOption.FALSE) @JoinColumn(name = "category_id") private Category category; - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "products_manufacturers", joinColumns = @JoinColumn(name = "product_fk"), inverseJoinColumns = @JoinColumn(name = "manufacturer_fk")) private List manufacturerList; + @Lob + @Column(name = "photo", columnDefinition="BLOB") + private byte[] photo; + @Column private Integer price; @@ -29,15 +37,20 @@ public class Product { } - public Product(Integer price) + public Product(Integer price, byte[] photo) { this.price = price; + this.photo = photo; } public Long getId() { return id; } public Integer getPrice() { return price; } + public byte[] getPhoto() { return photo; } + + public void setPhoto(byte[] photo) { this.photo = photo; } + public void setPrice(Integer price) { this.price = price; } public List getManufacturerList() { return manufacturerList; } 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 97146e0..384df4b 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 @@ -1,14 +1,26 @@ package ru.ulstu.is.sbapp.HardwareShop.services; +import org.apache.commons.io.IOCase; +import org.apache.commons.io.IOUtils; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import ru.ulstu.is.sbapp.HardwareShop.models.Category; import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Product; +import javax.imageio.ImageIO; import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; import javax.persistence.PersistenceContext; +import javax.persistence.Temporal; + import org.springframework.transaction.annotation.Transactional; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.List; @Service @@ -16,12 +28,25 @@ public class ProductService { @PersistenceContext private EntityManager em; + private byte[] PhotoToByte(String path) { + try { + BufferedImage bImage = ImageIO.read(new File(path)); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ImageIO.write(bImage, "png", byteArrayOutputStream ); + byte [] data = byteArrayOutputStream.toByteArray(); + return data; + } + catch (Exception e) { + return null; + } + } + @Transactional - public Product addProduct(Integer price) { + public Product addProduct(Integer price, String path) { if (price == 0) { throw new IllegalArgumentException("Product price equal 0"); } - final Product product = new Product(price); + final Product product = new Product(price, PhotoToByte(path)); em.persist(product); return product; } @@ -63,6 +88,16 @@ public class ProductService { em.createQuery("delete from Product").executeUpdate(); } + @Transactional + public void addCategoryToProduct(Long id, Category category) { + final Product product = findProduct(id); + if (product == null) { + throw new IllegalArgumentException("Product with id " + id + " not found"); + } + product.addCategory(category); + em.merge(product); + } + @Transactional public void addManufacturersToProduct(Long id, Manufacturer manufacturer) { final Product product = findProduct(id); diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java b/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java index ae2d976..bb7f653 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java @@ -1,5 +1,6 @@ package ru.ulstu.is.sbapp; +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -7,11 +8,14 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import ru.ulstu.is.sbapp.HardwareShop.models.Category; +import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; +import ru.ulstu.is.sbapp.HardwareShop.services.ManufacturerService; import ru.ulstu.is.sbapp.HardwareShop.services.CategoryService; import ru.ulstu.is.sbapp.HardwareShop.models.Product; import ru.ulstu.is.sbapp.HardwareShop.services.ProductService; import javax.persistence.EntityNotFoundException; +import java.io.InputStream; import java.util.List; @SpringBootTest @@ -24,18 +28,32 @@ public class JpaProductTests { @Autowired private CategoryService categoryService; + @Autowired + private ManufacturerService manufacturerService; + + String path = "C:\\Users\\user\\Desktop\\IP_Aleikin_PIbd-22(ver 2)\\IP_Aleikin_PIbd-22\\src\\test\\java\\ru\\ulstu\\is\\sbapp\\photo.png"; + @Test void testProductCreate() { productService.deleteAllProducts(); - final Product product = productService.addProduct(15000); + final Product product = productService.addProduct(15000, path); log.info(product.toString()); Assertions.assertNotNull(product.getId()); } + @Test + void testProductHasPhoto() { + productService.deleteAllProducts(); + Product product = productService.addProduct(15000, path); + log.info(product.toString()); + product = productService.findProduct(product.getId()); + Assertions.assertNotEquals(product.getPhoto().length, 0); + } + @Test void testProductRead() { productService.deleteAllProducts(); - final Product product = productService.addProduct(15000); + final Product product = productService.addProduct(15000, path); log.info(product.toString()); final Product findProduct = productService.findProduct(product.getId()); log.info(findProduct.toString()); @@ -51,8 +69,8 @@ public class JpaProductTests { @Test void testProductReadAll() { productService.deleteAllProducts(); - productService.addProduct(15000); - productService.addProduct(20000); + productService.addProduct(15000, path); + productService.addProduct(20000, path); final List productList = productService.findAllProducts(); log.info(productList.toString()); Assertions.assertEquals(productList.size(), 2); @@ -69,22 +87,53 @@ public class JpaProductTests { @Test void testProductUpdate() { productService.deleteAllProducts(); - final Product product = productService.addProduct(15000); + Product product = productService.addProduct(15000, path); log.info(product.toString()); productService.updateProduct(product.getId(), 20000); + product = productService.findProduct(product.getId()); log.info(product.toString()); Assertions.assertEquals(product.getPrice(), 20000); } - @Test + /*@Test void testProductAddCategory() { - productService.deleteAllProducts(); - final Product product = productService.addProduct(15000); - final Category category = categoryService.addCategory("Электроника"); - product.addCategory(category); + Product product = productService.addProduct(15000); + Category category = categoryService.addCategory("Электроника"); + productService.addCategoryToProduct(product.getId(), category); + product = productService.findProduct(product.getId()); + category = categoryService.findCategory(category.getId()); log.info(product.toString() + product.getCategory().getName()); + log.info(category.getProductList().toString()); Assertions.assertEquals(product.getCategory().getName(), "Электроника"); productService.deleteAllProducts(); categoryService.deleteAllCategories(); + }*/ + + @Test + void testProductAddManufacturer() { + productService.deleteAllProducts(); + Product product = productService.addProduct(15000, path); + Manufacturer manufacturer = manufacturerService.addManufacturer("Sony", "ул.Камышинская 107"); + productService.addManufacturersToProduct(product.getId(), manufacturer); + product = productService.findProduct(product.getId()); + log.info(product.toString() + product.getManufacturerList()); + manufacturer = manufacturerService.findManufacturer(manufacturer.getId()); + log.info(manufacturer.getProductList().toString()); + Assertions.assertEquals(product.getManufacturerList().size(), 1); + manufacturerService.deleteAllManufacturers(); + } + + @Test + void testProductRemoveManufacturer() { + productService.deleteAllProducts(); + Product product = productService.addProduct(15000, path); + final Manufacturer manufacturer = manufacturerService.addManufacturer("Sony", "ул.Камышинская 107"); + productService.addManufacturersToProduct(product.getId(), manufacturer); + product = productService.findProduct(product.getId()); + log.info(product.toString() + product.getManufacturerList()); + productService.removeManufacturersToProduct(product.getId(), manufacturer); + product = productService.findProduct(product.getId()); + Assertions.assertEquals(product.getManufacturerList().size(), 0); + manufacturerService.deleteAllManufacturers(); } }