LabWork03 multipart
This commit is contained in:
parent
e95c311912
commit
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,21 +25,29 @@ public class Product {
|
|||||||
@Column
|
@Column
|
||||||
private Integer price;
|
private Integer price;
|
||||||
|
|
||||||
|
@Lob
|
||||||
|
private byte[] photo;
|
||||||
|
|
||||||
public Product() {
|
public 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 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; }
|
||||||
|
@ -2,6 +2,7 @@ package ru.ulstu.is.sbapp.HardwareShop.services;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
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.Manufacturer;
|
||||||
import ru.ulstu.is.sbapp.HardwareShop.models.Product;
|
import ru.ulstu.is.sbapp.HardwareShop.models.Product;
|
||||||
|
|
||||||
@ -17,11 +18,11 @@ public class ProductService {
|
|||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Product addProduct(Integer price) {
|
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);
|
final Product product = new Product(price, image);
|
||||||
em.persist(product);
|
em.persist(product);
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,22 @@ 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.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.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
|
||||||
@ -24,15 +33,31 @@ public class JpaProductTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CategoryService categoryService;
|
private CategoryService categoryService;
|
||||||
|
|
||||||
@Test
|
private MultipartFile ConvertFile() throws IOException {
|
||||||
void testProductCreate() {
|
File file = new File("C:\\Users\\aleyc\\IdeaProjects\\IP_Aleikin_PIbd-22\\src\\test\\java\\ru\\ulstu\\is\\sbapp\\cart.png");
|
||||||
productService.deleteAllProducts();
|
String name = "file";
|
||||||
final Product product = productService.addProduct(15000);
|
String originalFileName = file.getName();
|
||||||
log.info(product.toString());
|
String contentType = "image/jpeg";
|
||||||
Assertions.assertNotNull(product.getId());
|
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() 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() {
|
void testProductRead() {
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
final Product product = productService.addProduct(15000);
|
final Product product = productService.addProduct(15000);
|
||||||
@ -86,5 +111,5 @@ public class JpaProductTests {
|
|||||||
Assertions.assertEquals(product.getCategory().getName(), "Электроника");
|
Assertions.assertEquals(product.getCategory().getName(), "Электроника");
|
||||||
productService.deleteAllProducts();
|
productService.deleteAllProducts();
|
||||||
categoryService.deleteAllCategories();
|
categoryService.deleteAllCategories();
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user