Compare commits
3 Commits
b5775f0abb
...
98d5474e3c
Author | SHA1 | Date | |
---|---|---|---|
98d5474e3c | |||
56bc353670 | |||
4b358e7975 |
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,5 @@
|
|||||||
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.*;
|
||||||
@ -14,25 +11,23 @@ public class Product {
|
|||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@LazyCollection(LazyCollectionOption.FALSE)
|
|
||||||
@JoinColumn(name = "category_id")
|
@JoinColumn(name = "category_id")
|
||||||
private Category category;
|
private Category category;
|
||||||
|
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany
|
||||||
@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;
|
||||||
|
|
||||||
|
@Lob
|
||||||
|
private byte[] photo;
|
||||||
|
|
||||||
public Product() {
|
public Product() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -49,10 +44,10 @@ public class Product {
|
|||||||
|
|
||||||
public byte[] getPhoto() { return photo; }
|
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 void setPhoto(byte[] photo) { this.photo = photo; }
|
||||||
|
|
||||||
public List<Manufacturer> getManufacturerList() { return manufacturerList; }
|
public List<Manufacturer> getManufacturerList() { return manufacturerList; }
|
||||||
|
|
||||||
public Category getCategory() { return category; }
|
public Category getCategory() { return category; }
|
||||||
|
@ -1,26 +1,15 @@
|
|||||||
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 org.springframework.web.multipart.MultipartFile;
|
||||||
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
|
||||||
@ -28,25 +17,12 @@ 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, String path) {
|
public Product addProduct(Integer price, byte[] image) {
|
||||||
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, PhotoToByte(path));
|
final Product product = new Product(price, image);
|
||||||
em.persist(product);
|
em.persist(product);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
@ -88,16 +64,6 @@ 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);
|
||||||
|
@ -1,21 +1,26 @@
|
|||||||
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;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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.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.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;
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -28,32 +33,34 @@ public class JpaProductTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CategoryService categoryService;
|
private CategoryService categoryService;
|
||||||
|
|
||||||
@Autowired
|
private MultipartFile ConvertFile() throws IOException {
|
||||||
private ManufacturerService manufacturerService;
|
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 path = "C:\\Users\\user\\Desktop\\IP_Aleikin_PIbd-22(ver 2)\\IP_Aleikin_PIbd-22\\src\\test\\java\\ru\\ulstu\\is\\sbapp\\photo.png";
|
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
|
@Test
|
||||||
void testProductCreate() {
|
void testProductCreate() throws Exception {
|
||||||
|
MultipartFile localFile = ConvertFile();
|
||||||
|
String strImage;
|
||||||
|
strImage = ("data:" + localFile.getContentType() + ";base64," + Base64.getEncoder().encodeToString(localFile.getBytes()));
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
final Product product = productService.addProduct(15000, path);
|
final Product product = productService.addProduct(15000, strImage.getBytes());
|
||||||
log.info(product.toString());
|
log.info(product.toString());
|
||||||
Assertions.assertNotNull(product.getId());
|
Assertions.assertNotNull(product.getId());
|
||||||
|
Assertions.assertNotNull(product.getPhoto());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
/*@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() {
|
void testProductRead() {
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
final Product product = productService.addProduct(15000, path);
|
final Product product = productService.addProduct(15000);
|
||||||
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());
|
||||||
@ -69,8 +76,8 @@ public class JpaProductTests {
|
|||||||
@Test
|
@Test
|
||||||
void testProductReadAll() {
|
void testProductReadAll() {
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
productService.addProduct(15000, path);
|
productService.addProduct(15000);
|
||||||
productService.addProduct(20000, path);
|
productService.addProduct(20000);
|
||||||
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);
|
||||||
@ -87,53 +94,22 @@ public class JpaProductTests {
|
|||||||
@Test
|
@Test
|
||||||
void testProductUpdate() {
|
void testProductUpdate() {
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
Product product = productService.addProduct(15000, path);
|
final Product product = productService.addProduct(15000);
|
||||||
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() {
|
||||||
Product product = productService.addProduct(15000);
|
productService.deleteAllProducts();
|
||||||
Category category = categoryService.addCategory("Электроника");
|
final Product product = productService.addProduct(15000);
|
||||||
productService.addCategoryToProduct(product.getId(), category);
|
final Category category = categoryService.addCategory("Электроника");
|
||||||
product = productService.findProduct(product.getId());
|
product.addCategory(category);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
BIN
src/test/java/ru/ulstu/is/sbapp/cart.png
Normal file
BIN
src/test/java/ru/ulstu/is/sbapp/cart.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
Loading…
Reference in New Issue
Block a user