урааааа, сдана 4
This commit is contained in:
parent
4f93068cce
commit
a36c6e9130
@ -12,7 +12,8 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class _Order {
|
||||
@Table(name = "tab_order")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@ -36,12 +37,12 @@ public class _Order {
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public _Order(Date dateOfOrder) {
|
||||
public Order(Date dateOfOrder) {
|
||||
this.dateOfOrder = dateOfOrder;
|
||||
products = new ArrayList<>();
|
||||
}
|
||||
|
||||
public _Order() {
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public Date getDateOfOrder() {
|
||||
@ -81,7 +82,7 @@ public class _Order {
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
_Order order = (_Order) o;
|
||||
Order order = (Order) o;
|
||||
if(!Objects.equals(id, order.id)) return false;
|
||||
|
||||
if(!Objects.equals(dateOfOrder.toString(), order.dateOfOrder.toString())) return false;
|
@ -34,15 +34,10 @@ public class OrderController {
|
||||
return orderService.findAllOrderProducts(id).stream().map(ProductDto::new).toList();
|
||||
}
|
||||
|
||||
// @PostMapping("/someSuppliers/")
|
||||
// public List<SupplierDto> getSomeSuppliers(@RequestBody() List<Product> products) {
|
||||
// return orderService.suppliers(products).stream().map(SupplierDto::new).toList();
|
||||
// }
|
||||
|
||||
// @PostMapping("/someSuppliers/")
|
||||
// public List<ProductDto> getSomeSuppliers(@RequestBody() List<ProductDto> products) {
|
||||
// return products;
|
||||
// }
|
||||
@GetMapping("/someSuppliers/{id}")
|
||||
public List<SupplierDto> getSomeSuppliers(@PathVariable Long id) {
|
||||
return orderService.suppliers(id).stream().map(SupplierDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Long createOrder(@RequestParam() Long supplierId) {
|
||||
|
@ -14,7 +14,7 @@ public class OrderDto {
|
||||
private Supplier supplier;
|
||||
private List<Product> products;
|
||||
|
||||
public OrderDto(_Order order){
|
||||
public OrderDto(Order order){
|
||||
this.id = order.getId();
|
||||
this.dateOfOrder = order.getDateOfOrder();
|
||||
this.supplier = order.getSupplier();
|
||||
|
@ -7,8 +7,9 @@ import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderRepository extends JpaRepository<_Order, Long> {
|
||||
@Query("SELECT distinct o.supplier FROM _Order o join Product p where p in (?1)")
|
||||
List<Supplier> getSomeSuppliers(List<Product> products);
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
@Query("SELECT distinct o.supplier FROM Order o join Product p where p = ?1")
|
||||
// @Query("SELECT distinct o.supplier FROM Order o where ?1 member of o.products")
|
||||
List<Supplier> getSomeSuppliers(Product product);
|
||||
|
||||
}
|
||||
|
@ -28,58 +28,52 @@ public class OrderService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public _Order addOrder(Long supplierId){
|
||||
final _Order order = new _Order(new Date());
|
||||
public Order addOrder(Long supplierId){
|
||||
final Order order = new Order(new Date());
|
||||
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){
|
||||
return orderRepository.getSomeSuppliers(products);
|
||||
public List<Supplier> suppliers(Long productId){
|
||||
final Product product = productService.findProduct(productId);
|
||||
return orderRepository.getSomeSuppliers(product);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public _Order addProduct(Long id, Long productId) {
|
||||
final _Order currentOrder = findOrder(id);
|
||||
public Order addProduct(Long id, Long productId) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
currentOrder.addProduct(productService.findProduct(productId));
|
||||
return orderRepository.save(currentOrder);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public _Order removeProduct(Long id, Long productId) {
|
||||
final _Order currentOrder = findOrder(id);
|
||||
public Order removeProduct(Long id, Long productId) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
currentOrder.addProduct(productService.findProduct(productId));
|
||||
return orderRepository.save(currentOrder);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public _Order findOrder(Long id) {
|
||||
final Optional<_Order> order = orderRepository.findById(id);
|
||||
public Order findOrder(Long id) {
|
||||
final Optional<Order> order = orderRepository.findById(id);
|
||||
return order.orElseThrow(() -> new OrderNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<_Order> findAllOrders() {
|
||||
public List<Order> findAllOrders() {
|
||||
return orderRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findAllOrderProducts(Long orderId) {
|
||||
final Optional<_Order> order = orderRepository.findById(orderId);
|
||||
final Optional<Order> order = orderRepository.findById(orderId);
|
||||
return order.orElseThrow(() -> new OrderNotFoundException(orderId)).getProducts();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public _Order deleteOrder(Long id) {
|
||||
final _Order currentOrder = findOrder(id);
|
||||
public Order deleteOrder(Long id) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
orderRepository.delete(currentOrder);
|
||||
return currentOrder;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.example.demo.supply.Product;
|
||||
|
||||
import com.example.demo.supply.Order._Order;
|
||||
import com.example.demo.supply.Order.Order;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@ -22,7 +22,7 @@ public class Product {
|
||||
private double cost;
|
||||
@JsonIgnore
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "products")
|
||||
private List<_Order> orders;
|
||||
private List<Order> orders;
|
||||
|
||||
public Product(){}
|
||||
|
||||
@ -48,11 +48,11 @@ public class Product {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<_Order> orders) {
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.example.demo.supply.Product;
|
||||
|
||||
import com.example.demo.supply.Order._Order;
|
||||
import com.example.demo.supply.Order.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -8,7 +8,7 @@ public class ProductDto {
|
||||
private long id;
|
||||
private String name;
|
||||
private double cost;
|
||||
private List<_Order> orders;
|
||||
private List<Order> orders;
|
||||
|
||||
public ProductDto(Product product) {
|
||||
this.id = product.getId();
|
||||
@ -26,7 +26,7 @@ public class ProductDto {
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.example.demo.supply.Supplier;
|
||||
|
||||
import com.example.demo.supply.Order._Order;
|
||||
import com.example.demo.supply.Product.Product;
|
||||
import com.example.demo.supply.Order.Order;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@ -22,7 +21,7 @@ public class Supplier {
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "supplier", cascade = CascadeType.REMOVE)
|
||||
private List<_Order> orders;
|
||||
private List<Order> orders;
|
||||
|
||||
public Supplier(){}
|
||||
|
||||
@ -49,11 +48,11 @@ public class Supplier {
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(List<_Order> orders) {
|
||||
public void setOrders(List<Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.example.demo.supply.Supplier;
|
||||
|
||||
import com.example.demo.supply.Order._Order;
|
||||
import com.example.demo.supply.Order.Order;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -8,7 +8,7 @@ public class SupplierDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
private int license;
|
||||
private List<_Order> orders;
|
||||
private List<Order> orders;
|
||||
|
||||
public SupplierDto(Supplier supplier){
|
||||
this.id = supplier.getId();
|
||||
@ -24,7 +24,7 @@ public class SupplierDto {
|
||||
public int getLicense() {
|
||||
return license;
|
||||
}
|
||||
public List<_Order> getOrders() {
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import CatalogProducts from "./Pages/CatalogProducts";
|
||||
import CatalogSuppliers from "./Pages/CatalogSuppliers";
|
||||
import OrderPage from "./Pages/OrdersPage";
|
||||
import CreateOrderPage from "./Pages/CreateOrderPage";
|
||||
import GetSomeSuppliers from "./Pages/GetSomeSuppliers";
|
||||
import Header from "./general/Header";
|
||||
|
||||
function App() {
|
||||
@ -16,6 +17,7 @@ function App() {
|
||||
<Route path="/suppliers" Component={CatalogSuppliers} />
|
||||
<Route path="/orders" Component={OrderPage} />
|
||||
<Route path="/createOrder" Component={CreateOrderPage} />
|
||||
<Route path="/dop" Component={GetSomeSuppliers} />
|
||||
</Routes>
|
||||
|
||||
</BrowserRouter>
|
||||
|
@ -6,7 +6,6 @@ function getFullUrl(url, data) {
|
||||
const fields = Object.getOwnPropertyNames(data);
|
||||
//проходимся по каждому полю
|
||||
for (const field of fields) {
|
||||
|
||||
if (field === undefined) continue
|
||||
if (field === 'id') continue
|
||||
if (field === 'date') continue
|
||||
@ -51,26 +50,12 @@ export default class DataService {
|
||||
return true;
|
||||
}
|
||||
|
||||
static async getSomeSuppliers(){
|
||||
const arr =[
|
||||
{
|
||||
"id":104,
|
||||
"name":"prod3",
|
||||
"cost":3333
|
||||
},
|
||||
{
|
||||
"id":153,
|
||||
"name":"prod3",
|
||||
"cost":5555
|
||||
}
|
||||
]
|
||||
|
||||
//const resArr = JSON.stringify(arr)
|
||||
//console.log(resArr)
|
||||
const response = await axios.post(this.mainUrl + `order/someSuppliers/`, {
|
||||
body: {arr}
|
||||
});
|
||||
console.log(response)
|
||||
static async getSomeSuppliers(url){
|
||||
const response = await fetch(this.mainUrl + url, {
|
||||
method: 'GET',
|
||||
}).catch(e => console.log(e))
|
||||
const res = response.json()
|
||||
return res
|
||||
}
|
||||
|
||||
static async update(url, data) {
|
||||
|
@ -4,7 +4,6 @@ import Order from '../models/Order';
|
||||
import Supplier from '../models/Supplier';
|
||||
import Product from '../models/Product';
|
||||
import DataService from '../DataService';
|
||||
import Modal from '../general/Modal';
|
||||
|
||||
export default function CatalogOrders(props) {
|
||||
const url = 'order/'
|
||||
|
97
front/src/Pages/GetSomeSuppliers.jsx
Normal file
97
front/src/Pages/GetSomeSuppliers.jsx
Normal file
@ -0,0 +1,97 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import Modal from "../general/Modal";
|
||||
import Table from "../general/Table";
|
||||
import DataService from "../DataService";
|
||||
import Product from "../models/Product";
|
||||
import Supplier from "../models/Supplier";
|
||||
|
||||
export default function GetSomeSuppliers(props) {
|
||||
const url = 'order/someSuppliers/'
|
||||
const productUrl = 'product/'
|
||||
|
||||
const headers = [
|
||||
{ name: 'name', label: 'Поставщик' },
|
||||
{ name: 'license', label: 'Лицензия' }
|
||||
];
|
||||
|
||||
const transformer = (data) => new Supplier(data)
|
||||
const transformerProduct = (data) => new Product(data)
|
||||
|
||||
const [suppliers, setSuppliers] = useState([])
|
||||
const [products, setProducts] = useState([])
|
||||
const [product, setProduct] = useState(new Product())
|
||||
|
||||
const [modalHeader, setModalHeader] = useState('')
|
||||
const [modalConfirm, setModalConfirm] = useState('')
|
||||
const [modalVisible, setModalVisible] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
loadItems()
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const loadItems = () => {
|
||||
DataService.readAll(productUrl, transformerProduct).then(data =>{
|
||||
console.log(data)
|
||||
setProducts(data)
|
||||
})
|
||||
}
|
||||
|
||||
const chooseProduct = () => {
|
||||
setModalHeader('Добавление')
|
||||
setModalConfirm('Добавить')
|
||||
setModalVisible(true)
|
||||
}
|
||||
|
||||
const save = () => {
|
||||
console.log(product)
|
||||
DataService.getSomeSuppliers(`${url}${product.id}`)
|
||||
.then(data => setSuppliers(data))
|
||||
}
|
||||
|
||||
const handleChooseProduct = (event) => {
|
||||
// console.log(event.target.id)
|
||||
// console.log(event.target.value)
|
||||
setProduct({ ...product, [event.target.id]: event.target.value })
|
||||
}
|
||||
|
||||
const handleTableClick = (tableSelectedItems) => console.log("")
|
||||
const handleTableDblClick = (tableSelectedItem) => console.log("")
|
||||
|
||||
const hideModal = () => {
|
||||
setModalVisible(false)
|
||||
}
|
||||
const modalDone = () => save()
|
||||
|
||||
return (
|
||||
<>
|
||||
<button type="button" className="btn btn-success" onClick={chooseProduct}>Выбрать продукт</button>
|
||||
|
||||
<Table
|
||||
headers={headers}
|
||||
items={suppliers}
|
||||
selectable={false}
|
||||
onClick={handleTableClick}
|
||||
onDblClick={handleTableDblClick}/>
|
||||
<Modal
|
||||
header={modalHeader}
|
||||
confirm={modalConfirm}
|
||||
visible={modalVisible}
|
||||
onHide={hideModal}
|
||||
onDone={modalDone}>
|
||||
<div className="mb-3">
|
||||
<p className="h4" htmlFor="id">Продукт</p>
|
||||
<select id="id" className="form-select" required
|
||||
value={product.id} onChange={handleChooseProduct}>
|
||||
<option disabled value="">Укажите продукт</option>
|
||||
{
|
||||
products.map(product =>
|
||||
<option key={product.id} value={product.id}>{product.name}</option>
|
||||
)
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
@ -12,7 +12,6 @@ function Catalog(props) {
|
||||
const [modalVisible, setModalVisible] = useState(false)
|
||||
const [addProdVisible, setAddProdVisible] = useState(false)
|
||||
const [isEdit, setEdit] = useState(false)
|
||||
const [isAddProd, setIsAddProd] = useState(false)
|
||||
|
||||
|
||||
let selectedItems = [];
|
||||
@ -24,7 +23,10 @@ function Catalog(props) {
|
||||
|
||||
const loadItems = () => {
|
||||
DataService.readAll(props.url, props.transformer)
|
||||
.then(data => setItems(data))
|
||||
.then(data =>{
|
||||
console.log(data)
|
||||
setItems(data)
|
||||
} )
|
||||
}
|
||||
|
||||
const saveItem = () => {
|
||||
|
@ -27,6 +27,11 @@ export default function Header() {
|
||||
Заказы
|
||||
</Link>
|
||||
</li>
|
||||
<li className="nav-item">
|
||||
<Link className="nav-link" to="/dop">
|
||||
Доп задание
|
||||
</Link>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -43,7 +43,7 @@ export default function Table(props) {
|
||||
return data
|
||||
else{
|
||||
let prod = ''
|
||||
data.map(product => prod += ` <${product.name}> `)
|
||||
data.map(product => prod += ` ${product.name},`)
|
||||
return prod
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user