diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java new file mode 100644 index 0000000..9385e97 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/ProductController.java @@ -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; + } + } +} 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..564c657 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 @@ -25,21 +25,29 @@ public class Product { @Column private Integer price; + @Lob + private byte[] photo; + public 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 setPrice(Integer price) { this.price = price; } + public void setPhoto(byte[] photo) { this.photo = photo; } + public List getManufacturerList() { return manufacturerList; } public Category getCategory() { return category; } 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..18963e6 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 @@ -2,6 +2,7 @@ package ru.ulstu.is.sbapp.HardwareShop.services; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; import ru.ulstu.is.sbapp.HardwareShop.models.Manufacturer; import ru.ulstu.is.sbapp.HardwareShop.models.Product; @@ -17,11 +18,11 @@ public class ProductService { private EntityManager em; @Transactional - public Product addProduct(Integer price) { + public Product addProduct(Integer price, byte[] image) { if (price == 0) { throw new IllegalArgumentException("Product price equal 0"); } - final Product product = new Product(price); + final Product product = new Product(price, image); em.persist(product); return product; } diff --git a/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java b/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java index ae2d976..a7e5ee8 100644 --- a/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/JpaProductTests.java @@ -5,13 +5,22 @@ 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.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.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 @@ -24,15 +33,31 @@ public class JpaProductTests { @Autowired private CategoryService categoryService; - @Test - void testProductCreate() { - productService.deleteAllProducts(); - final Product product = productService.addProduct(15000); - log.info(product.toString()); - Assertions.assertNotNull(product.getId()); + 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() 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, strImage.getBytes()); + log.info(product.toString()); + Assertions.assertNotNull(product.getId()); + Assertions.assertNotNull(product.getPhoto()); + } + + /*@Test void testProductRead() { productService.deleteAllProducts(); final Product product = productService.addProduct(15000); @@ -86,5 +111,5 @@ public class JpaProductTests { Assertions.assertEquals(product.getCategory().getName(), "Электроника"); productService.deleteAllProducts(); categoryService.deleteAllCategories(); - } + }*/ }