-\(оо)/-
This commit is contained in:
parent
6c9581c820
commit
7a1916bff3
@ -19,4 +19,4 @@ public class WebConfiguration {
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.example.demo.supply.Order;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/order")
|
||||
public class OrderController {
|
||||
private OrderService orderService;
|
||||
|
||||
public OrderController(OrderService orderService){
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public OrderDto getOrder(@PathVariable Long id) {
|
||||
return new OrderDto(orderService.findOrder(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<OrderDto> getOrders() {
|
||||
return orderService.findAllOrders().stream().map(OrderDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public OrderDto createOrder(@RequestParam() Long supplierId) {
|
||||
return new OrderDto(orderService.addOrder(supplierId));
|
||||
}
|
||||
|
||||
@PatchMapping("/addProduct/{id}/")
|
||||
public OrderDto addProduct(@PathVariable Long id,
|
||||
@RequestParam() Long productId){
|
||||
return new OrderDto(orderService.addProduct(id, productId));
|
||||
}
|
||||
|
||||
// @PatchMapping("/{id}")
|
||||
// public Orders updateOrder(@PathVariable Long id,
|
||||
// @RequestParam() String name,
|
||||
// @RequestParam() int license) {
|
||||
// return orderService.updateOrder(id, name, license);
|
||||
// }
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public OrderDto deleteOrder(@PathVariable Long id) {
|
||||
return new OrderDto(orderService.deleteOrder(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.example.demo.supply.Order;
|
||||
|
||||
import com.example.demo.supply.Product.Product;
|
||||
import com.example.demo.supply.Supplier.Supplier;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDto {
|
||||
|
||||
private Long id;
|
||||
private Date dateOfOrder;
|
||||
private Supplier supplier;
|
||||
private List<Product> products;
|
||||
|
||||
public OrderDto(Orders order){
|
||||
this.id = order.getId();
|
||||
this.dateOfOrder = order.getDateOfOrder();
|
||||
this.supplier = order.getSupplier();
|
||||
this.products = order.getProducts();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Date getDateOfOrder() {
|
||||
return dateOfOrder;
|
||||
}
|
||||
|
||||
public Supplier getSupplier() {
|
||||
return supplier;
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
package com.example.demo.supply.Order;
|
||||
|
||||
import com.example.demo.supply.Product.Product;
|
||||
import com.example.demo.supply.Product.ProductRepository;
|
||||
import com.example.demo.supply.Product.ProductService;
|
||||
import com.example.demo.supply.Supplier.Supplier;
|
||||
import com.example.demo.supply.Supplier.SupplierService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -14,38 +17,43 @@ import java.util.Optional;
|
||||
public class OrderService {
|
||||
|
||||
private final OrderRepository orderRepository;
|
||||
private final ProductService productService;
|
||||
private final SupplierService supplierService;
|
||||
|
||||
public OrderService(OrderRepository orderRepository){
|
||||
public OrderService(OrderRepository orderRepository,
|
||||
ProductService productService,
|
||||
SupplierService supplierService)
|
||||
{
|
||||
this.orderRepository = orderRepository;
|
||||
this.productService = productService;
|
||||
this.supplierService = supplierService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Orders addOrder(Supplier supplier){
|
||||
public Orders addOrder(Long supplierId){
|
||||
final Orders order = new Orders(new Date(System.currentTimeMillis()));
|
||||
order.setSupplier(supplier);
|
||||
order.setSupplier(supplierService.findSupplier(supplierId));
|
||||
return orderRepository.save(order);
|
||||
}
|
||||
|
||||
//поставщики, у которых есть заказ на конкретный товар или несколько товаров
|
||||
// @Transactional
|
||||
// public List<Supplier> suppliers(List<Product> products){
|
||||
// return em.createQuery("SELECT distinct o.supplier FROM Orders o join Product p where p in (:products) ", Supplier.class)
|
||||
// .setParameter("products", products).getResultList();
|
||||
// }
|
||||
|
||||
@Transactional
|
||||
public List<Supplier> suppliers(List<Product> products){
|
||||
List<Supplier> result = new ArrayList<>();
|
||||
List<Orders> orders = findAllOrders();
|
||||
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;
|
||||
public Orders addProduct(Long id, Long productId) {
|
||||
final Orders currentOrder = findOrder(id);
|
||||
currentOrder.addProduct(productService.findProduct(productId));
|
||||
return orderRepository.save(currentOrder);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Orders addProduct(Long id, Product product) {
|
||||
public Orders deleteProduct(Long id, Long productId) {
|
||||
final Orders currentOrder = findOrder(id);
|
||||
currentOrder.addProduct(product);
|
||||
currentOrder.addProduct(productService.findProduct(productId));
|
||||
return orderRepository.save(currentOrder);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.example.demo.supply.Order;
|
||||
|
||||
import com.example.demo.supply.Product.Product;
|
||||
import com.example.demo.supply.Supplier.Supplier;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.sql.Date;
|
||||
@ -19,6 +20,7 @@ public class Orders {
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date dateOfOrder;
|
||||
|
||||
@JsonIgnore
|
||||
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
|
||||
@JoinColumn(name = "supplier_fk")
|
||||
private Supplier supplier;
|
||||
@ -67,6 +69,13 @@ public class Orders {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteProduct(Product product){
|
||||
if(products.contains(product)){
|
||||
products.remove(product);
|
||||
product.getOrders().remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/product")
|
||||
public class ProductController {
|
||||
|
||||
@ -25,8 +26,8 @@ public class ProductController {
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public ProductDto createProduct(@RequestParam("name") String name,
|
||||
@RequestParam("cost") double cost) {
|
||||
public ProductDto createProduct(@RequestParam() String name,
|
||||
@RequestParam() double cost) {
|
||||
return new ProductDto(productService.addProduct(name, cost));
|
||||
}
|
||||
|
||||
|
@ -10,17 +10,13 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class ProductService {
|
||||
private final ProductRepository productRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public ProductService(ProductRepository productRepository,
|
||||
ValidatorUtil validatorUtil){
|
||||
public ProductService(ProductRepository productRepository){
|
||||
this.productRepository = productRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product addProduct(String name, double cost){
|
||||
final Product product = new Product(name, cost);
|
||||
// validatorUtil.validate(product);
|
||||
return productRepository.save(product);
|
||||
}
|
||||
|
||||
@ -40,7 +36,6 @@ public class ProductService {
|
||||
final Product currentProduct = findProduct(id);
|
||||
currentProduct.setName(name);
|
||||
currentProduct.setCost(cost);
|
||||
// validatorUtil.validate(currentProduct);
|
||||
return productRepository.save(currentProduct);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.example.demo.supply.Supplier;
|
||||
|
||||
import com.example.demo.supply.Order.Orders;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -18,6 +19,7 @@ public class Supplier {
|
||||
@Column(nullable = false)
|
||||
private int license;
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "supplier", cascade = CascadeType.REMOVE)
|
||||
private List<Orders> orders;
|
||||
|
||||
|
@ -15,30 +15,30 @@ public class SupplierController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Supplier getSupplier(@PathVariable Long id) {
|
||||
return supplierService.findSupplier(id);
|
||||
public SupplierDto getSupplier(@PathVariable Long id) {
|
||||
return new SupplierDto(supplierService.findSupplier(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Supplier> getSupplier() {
|
||||
return supplierService.findAllSuppliers();
|
||||
public List<SupplierDto> getSuppliers() {
|
||||
return supplierService.findAllSuppliers().stream().map(SupplierDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Supplier createSupplier(@RequestParam() String name,
|
||||
public SupplierDto createSupplier(@RequestParam() String name,
|
||||
@RequestParam() int license) {
|
||||
return supplierService.addSupplier(name, license);
|
||||
return new SupplierDto(supplierService.addSupplier(name, license));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Supplier updateSupplier(@PathVariable Long id,
|
||||
public SupplierDto updateSupplier(@PathVariable Long id,
|
||||
@RequestParam() String name,
|
||||
@RequestParam() int license) {
|
||||
return supplierService.updateSupplier(id, name, license);
|
||||
return new SupplierDto(supplierService.updateSupplier(id, name, license));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Supplier deleteSupplier(@PathVariable Long id) {
|
||||
return supplierService.deleteSupplier(id);
|
||||
public SupplierDto deleteSupplier(@PathVariable Long id) {
|
||||
return new SupplierDto(supplierService.deleteSupplier(id));
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ public class SupplierDto {
|
||||
private List<Orders> orders;
|
||||
|
||||
public SupplierDto(Supplier supplier){
|
||||
this.id = supplier.getId();
|
||||
this.name = supplier.getName();
|
||||
this.license = supplier.getLicense();
|
||||
this.orders = supplier.getOrders();
|
||||
|
@ -16,5 +16,4 @@ import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class Tests {
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
import { useState } from 'react';
|
||||
import Catalog from '../general/Catalog'
|
||||
import Order from '../models/Order';
|
||||
|
||||
export default function CatalogOrders(props) {
|
||||
const url = 'order/'
|
||||
|
||||
const transformer = (data) => new Order(data)
|
||||
|
||||
const catalogOrderHeaders = [
|
||||
{ name: 'date', label: 'Дата заказа' },
|
||||
{ name: 'supplier', label: 'Поставщик' },
|
||||
{ name: 'products', label: 'Продукт(ы)' }
|
||||
];
|
||||
|
||||
const [data, setData] = useState(new Order());
|
||||
|
||||
const add = () => setData(new Order());
|
||||
const edit = (data) => setData(new Order(data))
|
||||
|
||||
|
||||
function handleFormChange(event) {
|
||||
setData({ ...data, [event.target.id]: event.target.value })
|
||||
}
|
||||
|
||||
return (
|
||||
<Catalog headers={catalogOrderHeaders}
|
||||
getAllUrl={url}
|
||||
url={url}
|
||||
transformer={transformer}
|
||||
data={data}
|
||||
add={add}
|
||||
edit={edit}>
|
||||
<div className="mb-3">
|
||||
<label htmlFor="data" className="form-label" value={`Дата ${data.date}`}>Дата ${data.date}</label>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label htmlFor="cost" className="form-label">Цена</label>
|
||||
<input type="number" id="cost" className="form-control" required
|
||||
value={data.cost} onChange={handleFormChange}/>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label htmlFor="groupId" className="form-label">Группа</label>
|
||||
<select id="groupId" className="form-select" required
|
||||
value={data.groupId} onChange={handleFormChange}>
|
||||
<option disabled value="">Укажите группу</option>
|
||||
{
|
||||
groups.map(group =>
|
||||
<option key={group.id} value={group.id}>{group.name}</option>
|
||||
)
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</Catalog>
|
||||
);
|
||||
}
|
@ -15,10 +15,7 @@ export default function CatalogProducts(props) {
|
||||
const [data, setData] = useState(new Product());
|
||||
|
||||
const add = () => setData(new Product());
|
||||
const edit = (data) => {
|
||||
console.log(data)
|
||||
setData(new Product(data))
|
||||
}
|
||||
const edit = (data) => setData(new Product(data))
|
||||
|
||||
|
||||
function handleFormChange(event) {
|
||||
|
@ -1,69 +1,50 @@
|
||||
import axios from 'axios';
|
||||
|
||||
function toJSON(data) {
|
||||
const jsonObj = {};
|
||||
function getFullUrl(url, data) {
|
||||
let currentUrl = new URL(url)
|
||||
//извлекаем поля
|
||||
const fields = Object.getOwnPropertyNames(data);
|
||||
//проходимся по каждому полю
|
||||
for (const field of fields) {
|
||||
if (data[field] === undefined) {
|
||||
continue;
|
||||
}
|
||||
jsonObj[field] = data[field];
|
||||
currentUrl.searchParams.append(field, data[field])
|
||||
}
|
||||
return jsonObj;
|
||||
|
||||
return currentUrl;
|
||||
}
|
||||
|
||||
export default class DataService {
|
||||
static dataUrlPrefix = 'http://localhost:8080/';
|
||||
static mainUrl = 'http://localhost:8080/';
|
||||
|
||||
static async readAll(url, transformer) {
|
||||
const response = await axios.get(this.dataUrlPrefix + url);
|
||||
const response = await axios.get(this.mainUrl + url);
|
||||
return response.data.map(item => transformer(item));
|
||||
}
|
||||
|
||||
static async read(url, transformer) {
|
||||
const response = await axios.get(this.dataUrlPrefix + url);
|
||||
const response = await axios.get(this.mainUrl + url);
|
||||
return transformer(response.data);
|
||||
}
|
||||
|
||||
static async create(url, data) {
|
||||
console.log(toJSON(data))
|
||||
// const response = await axios.post(this.dataUrlPrefix + url, toJSON(data));
|
||||
|
||||
await axios.post('http://localhost:8080/product/', {
|
||||
name: 'product3',
|
||||
cost: 123
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
await fetch(getFullUrl(this.mainUrl + url, data), {
|
||||
method: 'POST',
|
||||
}).catch(e => console.log(e))
|
||||
return true;
|
||||
}
|
||||
|
||||
static async update(url, data) {
|
||||
// console.log(toJSON(data))
|
||||
// const response = await axios.put(url);
|
||||
|
||||
await axios.put('http://localhost:8080/product/1', {
|
||||
name: 'product2',
|
||||
cost: 342
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
|
||||
data.id = undefined
|
||||
await fetch(getFullUrl(this.mainUrl + url, data), {
|
||||
method: 'PATCH',
|
||||
}).catch(e => console.log(e))
|
||||
return true;
|
||||
}
|
||||
|
||||
static async delete(url) {
|
||||
const response = await axios.delete(this.dataUrlPrefix + url);
|
||||
const response = await axios.delete(this.mainUrl + url);
|
||||
return response.data.id;
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ function Catalog(props) {
|
||||
|
||||
const saveItem = () => {
|
||||
if (!isEdit) {
|
||||
props
|
||||
DataService.create(props.url, props.data).then(() => loadItems());
|
||||
} else {
|
||||
DataService.update(props.url + props.data.id, props.data).then(() => loadItems());
|
||||
@ -76,10 +75,7 @@ function Catalog(props) {
|
||||
}
|
||||
}
|
||||
|
||||
const handleTableClick = (tableSelectedItems) =>{
|
||||
console.log(tableSelectedItems)
|
||||
selectedItems = tableSelectedItems;
|
||||
}
|
||||
const handleTableClick = (tableSelectedItems) => selectedItems = tableSelectedItems;
|
||||
|
||||
const handleTableDblClick = (tableSelectedItem) => editItem(tableSelectedItem);
|
||||
|
||||
|
46
front/src/models/DataService2.js
Normal file
46
front/src/models/DataService2.js
Normal file
@ -0,0 +1,46 @@
|
||||
import axios from 'axios';
|
||||
|
||||
function toJSON(data) {
|
||||
const jsonObj = {};
|
||||
const fields = Object.getOwnPropertyNames(data);
|
||||
for (const field of fields) {
|
||||
if (data[field] === undefined) {
|
||||
continue;
|
||||
}
|
||||
jsonObj[field] = data[field];
|
||||
}
|
||||
return jsonObj;
|
||||
}
|
||||
|
||||
export default class DataService {
|
||||
static dataUrlPrefix = 'http://localhost:8080/';
|
||||
|
||||
static async readAll(url, transformer) {
|
||||
const response = await axios.get(this.dataUrlPrefix + url);
|
||||
return response.data.map(item => transformer(item));
|
||||
}
|
||||
|
||||
static async read(url, transformer) {
|
||||
const response = await axios.get(this.dataUrlPrefix + url);
|
||||
return transformer(response.data);
|
||||
}
|
||||
|
||||
static async create(url, data) {
|
||||
console.log(toJSON(data))
|
||||
const response = await axios.post(this.dataUrlPrefix + url, {
|
||||
name: 'prod4',
|
||||
cost: 123
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
static async update(url, data) {
|
||||
const response = await axios.put(this.dataUrlPrefix + url, toJSON(data));
|
||||
return true;
|
||||
}
|
||||
|
||||
static async delete(url) {
|
||||
const response = await axios.delete(this.dataUrlPrefix + url);
|
||||
return response.data.id;
|
||||
}
|
||||
}
|
@ -1,7 +1,15 @@
|
||||
import Product from "./Product";
|
||||
|
||||
export default class Order {
|
||||
constructor(data) {
|
||||
this.id = data?.id;
|
||||
this.date = data?.date || '';
|
||||
this.supplier = data?.supplier || '';
|
||||
this.supplierId = data?.supplierId || '';
|
||||
this.products = data?.products.map(product => new Product(product)) || '';
|
||||
this.countProducts = '';
|
||||
if(this.countProducts !== ''){
|
||||
this.countProducts = this.products.length
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user