Compare commits

...

3 Commits

Author SHA1 Message Date
98d5474e3c LabWork03 multipart 2023-05-01 21:52:32 +04:00
56bc353670 Merge remote-tracking branch 'origin/LabWork03' into LabWork03
# Conflicts:
#	src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/Product.java
#	src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/ProductService.java
#	src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java
2023-05-01 21:50:27 +04:00
4b358e7975 LabWork03 multipart 2023-05-01 21:48:17 +04:00
5 changed files with 83 additions and 109 deletions

View File

@ -0,0 +1,37 @@
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.Product;
import ru.ulstu.is.sbapp.HardwareShop.services.ProductService;
import java.util.Base64;
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping("/test")
public String test() {
return "Test";
}
@PostMapping("/uploadImage")
public Product createProduct(@RequestParam Integer price, @RequestParam MultipartFile file) {
try {
byte[] byteImage = file.getBytes();
return productService.addProduct(price, byteImage);
}
catch (Exception e) {
return null;
}
}
}

View File

@ -1,8 +1,5 @@
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.*;
@ -14,25 +11,23 @@ public class Product {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;
@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany
@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;
@Lob
private byte[] photo;
public Product() {
}
@ -49,10 +44,10 @@ public class Product {
public byte[] getPhoto() { return photo; }
public void setPhoto(byte[] photo) { this.photo = photo; }
public void setPrice(Integer price) { this.price = price; }
public void setPhoto(byte[] photo) { this.photo = photo; }
public List<Manufacturer> getManufacturerList() { return manufacturerList; }
public Category getCategory() { return category; }

View File

@ -1,26 +1,15 @@
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 org.springframework.web.multipart.MultipartFile;
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
@ -28,25 +17,12 @@ 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, String path) {
public Product addProduct(Integer price, byte[] image) {
if (price == 0) {
throw new IllegalArgumentException("Product price equal 0");
}
final Product product = new Product(price, PhotoToByte(path));
final Product product = new Product(price, image);
em.persist(product);
return product;
}
@ -88,16 +64,6 @@ 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);

View File

@ -1,21 +1,26 @@
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;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
@SpringBootTest
@ -28,32 +33,34 @@ 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";
private MultipartFile ConvertFile() throws IOException {
File file = new File("C:\\Users\\aleyc\\IdeaProjects\\IP_Aleikin_PIbd-22\\src\\test\\java\\ru\\ulstu\\is\\sbapp\\cart.png");
String name = "file";
String originalFileName = file.getName();
String contentType = "image/jpeg";
byte[] content = new byte[(int) file.length()];
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(content);
MultipartFile multipartFile = new MockMultipartFile(name, originalFileName, contentType, content);
return multipartFile;
}
@Test
void testProductCreate() {
void testProductCreate() throws Exception {
MultipartFile localFile = ConvertFile();
String strImage;
strImage = ("data:" + localFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(localFile.getBytes()));
productService.deleteAllProducts();
final Product product = productService.addProduct(15000, path);
final Product product = productService.addProduct(15000, strImage.getBytes());
log.info(product.toString());
Assertions.assertNotNull(product.getId());
Assertions.assertNotNull(product.getPhoto());
}
@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() {
productService.deleteAllProducts();
final Product product = productService.addProduct(15000, path);
final Product product = productService.addProduct(15000);
log.info(product.toString());
final Product findProduct = productService.findProduct(product.getId());
log.info(findProduct.toString());
@ -69,8 +76,8 @@ public class JpaProductTests {
@Test
void testProductReadAll() {
productService.deleteAllProducts();
productService.addProduct(15000, path);
productService.addProduct(20000, path);
productService.addProduct(15000);
productService.addProduct(20000);
final List<Product> productList = productService.findAllProducts();
log.info(productList.toString());
Assertions.assertEquals(productList.size(), 2);
@ -87,53 +94,22 @@ public class JpaProductTests {
@Test
void testProductUpdate() {
productService.deleteAllProducts();
Product product = productService.addProduct(15000, path);
final Product product = productService.addProduct(15000);
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() {
Product product = productService.addProduct(15000);
Category category = categoryService.addCategory("Электроника");
productService.addCategoryToProduct(product.getId(), category);
product = productService.findProduct(product.getId());
category = categoryService.findCategory(category.getId());
productService.deleteAllProducts();
final Product product = productService.addProduct(15000);
final Category category = categoryService.addCategory("Электроника");
product.addCategory(category);
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();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB