@_@ треш
This commit is contained in:
parent
128fd2ff2b
commit
d304afbd9b
@ -13,7 +13,9 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
@ -31,29 +33,13 @@ public class OrderService {
|
|||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//поставщики, у которых есть заказ на конкретный товар или несколько товаров
|
//поставщики, у которых есть заказ на конкретный товар или несколько товаров
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Supplier> suppliers(List<Product> products){
|
public List<Supplier> suppliers(List<Product> products){
|
||||||
List<Supplier> result = new ArrayList<>();
|
return em.createQuery("SELECT distinct o.supplier FROM Orders o join Product p where p in (:products) ", Supplier.class)
|
||||||
List<Orders> orders = findAllOrders();
|
.setParameter("products", products).getResultList();
|
||||||
for(Orders order : orders){
|
|
||||||
int k = 0;
|
|
||||||
for(Product product : products){
|
|
||||||
if(order.getProducts().contains(product)) k++;
|
|
||||||
}
|
|
||||||
if(k == products.size())
|
|
||||||
result.add(order.getSupplier());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Transactional
|
|
||||||
// public List<Supplier> suppliers(List<Product> products){
|
|
||||||
// return em.createQuery("SELECT o.supplier FROM Orders o WHERE o.products = :products ", Supplier.class)
|
|
||||||
// .setParameter("products", products).getResultList();
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Orders addProduct(Long id, Product product) {
|
public Orders addProduct(Long id, Product product) {
|
||||||
if (product == null) {
|
if (product == null) {
|
||||||
|
@ -59,6 +59,6 @@ public class SupplierService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAll(){
|
public void deleteAll(){
|
||||||
em.createQuery("delete from Supplier ").executeUpdate();
|
em.createQuery("delete from Supplier").executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ public class Tests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOrder(){
|
public void testOrder(){
|
||||||
productService.deleteAll();
|
|
||||||
orderService.deleteAll();
|
orderService.deleteAll();
|
||||||
|
productService.deleteAll();
|
||||||
supplierService.deleteAll();
|
supplierService.deleteAll();
|
||||||
|
|
||||||
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
|
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
|
||||||
@ -38,22 +38,25 @@ public class Tests {
|
|||||||
order = orderService.addProduct(order.getId(), product2);
|
order = orderService.addProduct(order.getId(), product2);
|
||||||
final Orders order2 = orderService.findOrder(order.getId());
|
final Orders order2 = orderService.findOrder(order.getId());
|
||||||
Assertions.assertEquals(order, order2);
|
Assertions.assertEquals(order, order2);
|
||||||
productService.deleteAll();
|
|
||||||
orderService.deleteAll();
|
orderService.deleteAll();
|
||||||
|
productService.deleteAll();
|
||||||
supplierService.deleteAll();
|
supplierService.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test(){
|
public void test(){
|
||||||
productService.deleteAll();
|
|
||||||
orderService.deleteAll();
|
orderService.deleteAll();
|
||||||
|
productService.deleteAll();
|
||||||
supplierService.deleteAll();
|
supplierService.deleteAll();
|
||||||
/
|
|
||||||
|
|
||||||
|
|
||||||
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
|
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
|
||||||
final Product product2 = productService.addProduct("Samsung A2", 22000.56);
|
final Product product2 = productService.addProduct("Samsung A2", 22000.56);
|
||||||
final Product product3 = productService.addProduct("Redmond f3", 2000.13);
|
final Product product3 = productService.addProduct("Redmond f3", 2000.13);
|
||||||
final Product product4 = productService.addProduct("Asus red9", 22000.56);
|
final Product product4 = productService.addProduct("Asus red9", 22000.56);
|
||||||
|
|
||||||
|
|
||||||
final Supplier supplier1 = supplierService.addSupplier("SuperSupplier1", 325453);
|
final Supplier supplier1 = supplierService.addSupplier("SuperSupplier1", 325453);
|
||||||
final Supplier supplier2 = supplierService.addSupplier("SuperSupplier2", 545455);
|
final Supplier supplier2 = supplierService.addSupplier("SuperSupplier2", 545455);
|
||||||
final Supplier supplier3 = supplierService.addSupplier("SuperSupplier3", 122122);
|
final Supplier supplier3 = supplierService.addSupplier("SuperSupplier3", 122122);
|
||||||
@ -74,8 +77,8 @@ public class Tests {
|
|||||||
List<Supplier> suppliers = orderService.suppliers(products);
|
List<Supplier> suppliers = orderService.suppliers(products);
|
||||||
Assertions.assertEquals(suppliers.size(), 2);
|
Assertions.assertEquals(suppliers.size(), 2);
|
||||||
|
|
||||||
productService.deleteAll();
|
|
||||||
orderService.deleteAll();
|
orderService.deleteAll();
|
||||||
|
productService.deleteAll();
|
||||||
supplierService.deleteAll();
|
supplierService.deleteAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import com.example.demo.supply.models.Product;
|
|
||||||
|
|
||||||
import com.example.demo.supply.services.ProductService;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
public class TestsProduct {
|
|
||||||
@Autowired
|
|
||||||
private ProductService productService;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testProduct(){
|
|
||||||
productService.deleteAll();
|
|
||||||
final Product product = productService.addProduct("Huawei Band 3", 2000.13);
|
|
||||||
Assertions.assertNotNull(product.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testProductRead(){
|
|
||||||
productService.deleteAll();
|
|
||||||
final Product product = productService.addProduct("Huawei Band 3", 2000.13);
|
|
||||||
final Product findProduct = productService.findProduct(product.getId());
|
|
||||||
Assertions.assertEquals(product, findProduct);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testProductReadNotFound(){
|
|
||||||
productService.deleteAll();
|
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testProductReadAll(){
|
|
||||||
productService.deleteAll();
|
|
||||||
productService.addProduct("Samsung A3", 22000.4);
|
|
||||||
productService.addProduct("Huawei Band 3", 2000.13);
|
|
||||||
final List<Product> products = productService.findAllProducts();
|
|
||||||
Assertions.assertEquals(products.size(), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testProductReadAllEmpty(){
|
|
||||||
productService.deleteAll();
|
|
||||||
final List<Product> products = productService.findAllProducts();
|
|
||||||
Assertions.assertEquals(products.size(), 0);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import com.example.demo.supply.models.Supplier;
|
|
||||||
import com.example.demo.supply.services.OrderService;
|
|
||||||
import com.example.demo.supply.services.ProductService;
|
|
||||||
import com.example.demo.supply.services.SupplierService;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
public class TestsSupplier {
|
|
||||||
@Autowired
|
|
||||||
private SupplierService supplierService;
|
|
||||||
@Autowired
|
|
||||||
private OrderService orderService;
|
|
||||||
@Autowired
|
|
||||||
private ProductService productService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSupplier(){
|
|
||||||
supplierService.deleteAll();
|
|
||||||
final Supplier supplier = supplierService.addSupplier("SuperSup", 359342);
|
|
||||||
Assertions.assertNotNull(supplier.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSupplierRead(){
|
|
||||||
supplierService.deleteAll();
|
|
||||||
final Supplier supplier = supplierService.addSupplier("Huawei", 4357695);
|
|
||||||
final Supplier findSupplier = supplierService.findSupplier(supplier.getId());
|
|
||||||
Assertions.assertEquals(supplier, findSupplier);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSupplierReadNotFound(){
|
|
||||||
supplierService.deleteAll();
|
|
||||||
Assertions.assertThrows(EntityNotFoundException.class, () -> supplierService.findSupplier(-1L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSupplierReadAll(){
|
|
||||||
supplierService.deleteAll();
|
|
||||||
supplierService.addSupplier("Samsung", 3485456);
|
|
||||||
supplierService.addSupplier("Huawei", 45736964);
|
|
||||||
final List<Supplier> suppliers = supplierService.findAllSuppliers();
|
|
||||||
Assertions.assertEquals(suppliers.size(), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSupplierReadAllEmpty(){
|
|
||||||
supplierService.deleteAll();
|
|
||||||
final List<Supplier> suppliers = supplierService.findAllSuppliers();
|
|
||||||
Assertions.assertEquals(suppliers.size(), 0);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user