Сделал image
This commit is contained in:
parent
e95c311912
commit
b5775f0abb
@ -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'
|
||||
|
@ -10,9 +10,6 @@ public class Category {
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@OneToMany(mappedBy = "category")
|
||||
private List<Product> productList;
|
||||
|
||||
@Column
|
||||
private String name;
|
||||
|
||||
|
@ -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<Product> productList;
|
||||
|
||||
@Column
|
||||
|
@ -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<Manufacturer> 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<Manufacturer> getManufacturerList() { return manufacturerList; }
|
||||
|
@ -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);
|
||||
|
@ -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<Product> 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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user