Сделал image

This commit is contained in:
Katerina881 2023-04-03 16:31:44 +04:00
parent e95c311912
commit b5775f0abb
6 changed files with 116 additions and 19 deletions

View File

@ -13,6 +13,8 @@ repositories {
} }
dependencies { 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-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210' implementation 'com.h2database:h2:2.1.210'

View File

@ -10,9 +10,6 @@ public class Category {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@OneToMany(mappedBy = "category")
private List<Product> productList;
@Column @Column
private String name; private String name;

View File

@ -10,7 +10,8 @@ public class Manufacturer {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@ManyToMany(mappedBy = "manufacturerList") @ManyToMany(fetch = FetchType.EAGER,
mappedBy = "manufacturerList")
private List<Product> productList; private List<Product> productList;
@Column @Column

View File

@ -1,5 +1,8 @@
package ru.ulstu.is.sbapp.HardwareShop.models; package ru.ulstu.is.sbapp.HardwareShop.models;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.*; import javax.persistence.*;
@ -11,17 +14,22 @@ public class Product {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "category_id") @JoinColumn(name = "category_id")
private Category category; private Category category;
@ManyToMany @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "products_manufacturers", @JoinTable(name = "products_manufacturers",
joinColumns = @JoinColumn(name = "product_fk"), joinColumns = @JoinColumn(name = "product_fk"),
inverseJoinColumns = @JoinColumn(name = "manufacturer_fk")) inverseJoinColumns = @JoinColumn(name = "manufacturer_fk"))
private List<Manufacturer> manufacturerList; private List<Manufacturer> manufacturerList;
@Lob
@Column(name = "photo", columnDefinition="BLOB")
private byte[] photo;
@Column @Column
private Integer price; private Integer price;
@ -29,15 +37,20 @@ public class Product {
} }
public Product(Integer price) public Product(Integer price, byte[] photo)
{ {
this.price = price; this.price = price;
this.photo = photo;
} }
public Long getId() { return id; } public Long getId() { return id; }
public Integer getPrice() { return price; } 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 void setPrice(Integer price) { this.price = price; }
public List<Manufacturer> getManufacturerList() { return manufacturerList; } public List<Manufacturer> getManufacturerList() { return manufacturerList; }

View File

@ -1,14 +1,26 @@
package ru.ulstu.is.sbapp.HardwareShop.services; 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.stereotype.Service;
import org.springframework.util.StringUtils; 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.Manufacturer;
import ru.ulstu.is.sbapp.HardwareShop.models.Product; import ru.ulstu.is.sbapp.HardwareShop.models.Product;
import javax.imageio.ImageIO;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import org.springframework.transaction.annotation.Transactional; 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; import java.util.List;
@Service @Service
@ -16,12 +28,25 @@ public class ProductService {
@PersistenceContext @PersistenceContext
private EntityManager em; 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 @Transactional
public Product addProduct(Integer price) { public Product addProduct(Integer price, String path) {
if (price == 0) { if (price == 0) {
throw new IllegalArgumentException("Product price equal 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); em.persist(product);
return product; return product;
} }
@ -63,6 +88,16 @@ public class ProductService {
em.createQuery("delete from Product").executeUpdate(); 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 @Transactional
public void addManufacturersToProduct(Long id, Manufacturer manufacturer) { public void addManufacturersToProduct(Long id, Manufacturer manufacturer) {
final Product product = findProduct(id); final Product product = findProduct(id);

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp; package ru.ulstu.is.sbapp;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -7,11 +8,14 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.HardwareShop.models.Category; 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.services.CategoryService;
import ru.ulstu.is.sbapp.HardwareShop.models.Product; import ru.ulstu.is.sbapp.HardwareShop.models.Product;
import ru.ulstu.is.sbapp.HardwareShop.services.ProductService; import ru.ulstu.is.sbapp.HardwareShop.services.ProductService;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import java.io.InputStream;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
@ -24,18 +28,32 @@ public class JpaProductTests {
@Autowired @Autowired
private CategoryService categoryService; 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 @Test
void testProductCreate() { void testProductCreate() {
productService.deleteAllProducts(); productService.deleteAllProducts();
final Product product = productService.addProduct(15000); final Product product = productService.addProduct(15000, path);
log.info(product.toString()); log.info(product.toString());
Assertions.assertNotNull(product.getId()); 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 @Test
void testProductRead() { void testProductRead() {
productService.deleteAllProducts(); productService.deleteAllProducts();
final Product product = productService.addProduct(15000); final Product product = productService.addProduct(15000, path);
log.info(product.toString()); log.info(product.toString());
final Product findProduct = productService.findProduct(product.getId()); final Product findProduct = productService.findProduct(product.getId());
log.info(findProduct.toString()); log.info(findProduct.toString());
@ -51,8 +69,8 @@ public class JpaProductTests {
@Test @Test
void testProductReadAll() { void testProductReadAll() {
productService.deleteAllProducts(); productService.deleteAllProducts();
productService.addProduct(15000); productService.addProduct(15000, path);
productService.addProduct(20000); productService.addProduct(20000, path);
final List<Product> productList = productService.findAllProducts(); final List<Product> productList = productService.findAllProducts();
log.info(productList.toString()); log.info(productList.toString());
Assertions.assertEquals(productList.size(), 2); Assertions.assertEquals(productList.size(), 2);
@ -69,22 +87,53 @@ public class JpaProductTests {
@Test @Test
void testProductUpdate() { void testProductUpdate() {
productService.deleteAllProducts(); productService.deleteAllProducts();
final Product product = productService.addProduct(15000); Product product = productService.addProduct(15000, path);
log.info(product.toString()); log.info(product.toString());
productService.updateProduct(product.getId(), 20000); productService.updateProduct(product.getId(), 20000);
product = productService.findProduct(product.getId());
log.info(product.toString()); log.info(product.toString());
Assertions.assertEquals(product.getPrice(), 20000); Assertions.assertEquals(product.getPrice(), 20000);
} }
@Test /*@Test
void testProductAddCategory() { void testProductAddCategory() {
productService.deleteAllProducts(); Product product = productService.addProduct(15000);
final Product product = productService.addProduct(15000); Category category = categoryService.addCategory("Электроника");
final Category category = categoryService.addCategory("Электроника"); productService.addCategoryToProduct(product.getId(), category);
product.addCategory(category); product = productService.findProduct(product.getId());
category = categoryService.findCategory(category.getId());
log.info(product.toString() + product.getCategory().getName()); log.info(product.toString() + product.getCategory().getName());
log.info(category.getProductList().toString());
Assertions.assertEquals(product.getCategory().getName(), "Электроника"); Assertions.assertEquals(product.getCategory().getName(), "Электроника");
productService.deleteAllProducts(); productService.deleteAllProducts();
categoryService.deleteAllCategories(); 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();
} }
} }