работает, невероятное

This commit is contained in:
Ino 2023-05-10 14:49:58 +04:00
parent cf46765fb9
commit 12ac235025
12 changed files with 183 additions and 167 deletions

View File

@ -0,0 +1,52 @@
package com.example.demo.supply.Order;
import com.example.demo.supply.Product.Product;
import com.example.demo.supply.Supplier.Supplier;
import java.util.Date;
import java.util.List;
public class OrderDtoForCreate {
private Long id;
private Date dateOfOrder;
private long supplierId;
private List<Long> productsId;
public OrderDtoForCreate(){
}
public OrderDtoForCreate(Order order){
this.id = order.getId();
this.dateOfOrder = order.getDateOfOrder();
this.supplierId = order.getSupplier().getId();
this.productsId = order.getProducts().stream().map(Product::getId).toList();
}
public long getId() {
return id;
}
public Date getDateOfOrder() {
return dateOfOrder;
}
public void setDateOfOrder(Date dateOfOrder) {
this.dateOfOrder = dateOfOrder;
}
public long getSupplierId() {
return supplierId;
}
public void setSupplierId(long supplierId) {
this.supplierId = supplierId;
}
public List<Long> getProductsId() {
return productsId;
}
public void setProductsId(List<Long> productsId) {
this.productsId = productsId;
}
}

View File

@ -10,7 +10,6 @@ import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
@Controller @Controller
@RequestMapping("/order") @RequestMapping("/order")
@ -33,40 +32,48 @@ public class OrderMvcController {
orderService.findAllOrders().stream() orderService.findAllOrders().stream()
.map(OrderDto::new) .map(OrderDto::new)
.toList()); .toList());
// model.addAttribute("products",
// pro.findAllOrders().stream()
// .map(OrderDto::new)
// .toList());
return "order"; return "order";
} }
@GetMapping("/{id}") @GetMapping("/dop")
public String getOrder(@PathVariable Long id, public String getSuppliers(Model model,
Model model) { @RequestParam(required = false) Long id) {
model.addAttribute("orderId", id); if(id == null || id <= 0) {
model.addAttribute("orderDto", new OrderDto(orderService.findOrder(id))); model.addAttribute("productDto", new ProductDto());
return "product-edit"; model.addAttribute("suppliers", null);
model.addAttribute("selectedProduct", null);
}
else{
ProductDto product = new ProductDto(productService.findProduct(id));
model.addAttribute("productDto", product);
model.addAttribute("selectedProduct", null);
model.addAttribute("suppliers", orderService.suppliers(id).stream().map(SupplierDto::new).toList());
}
model.addAttribute("products", productService.findAllProducts().stream().map(ProductDto::new).toList());
// model.addAttribute("orderDto", new OrderDto(orderService.findOrder(id)));
return "order-dop";
} }
@PostMapping("/") @GetMapping("/add")
public String createOrder(@ModelAttribute @Valid OrderDto orderDto, public String addOrder(Model model) {
BindingResult bindingResult, model.addAttribute("orderDto", new OrderDtoForCreate());
Model model) { model.addAttribute("selectedSupplier", null);
model.addAttribute("suppliers", supplierService.findAllSuppliers().stream().map(SupplierDto::new).toList());
model.addAttribute("products", productService.findAllProducts().stream().map(ProductDto::new).toList());
return "order-add";
}
@PostMapping("/create")
public String saveOrder(Model model,
@ModelAttribute("orderDto") @Valid OrderDtoForCreate order,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors()); model.addAttribute("errors", bindingResult.getAllErrors());
return "order-add"; return "order-add";
} }
Order newOrder = orderService.addOrder(supplierService.findSupplier(order.getSupplierId()).getId());
List<SupplierDto> suppliers =supplierService.findAllSuppliers().stream() for(Long obj: order.getProductsId())
.map(SupplierDto::new).toList(); orderService.addProduct(newOrder.getId(), obj);
orderService.addOrder(orderDto.getSupplier().getId());
return "redirect:/order"; return "redirect:/order";
} }
//
// @PostMapping("/delete/{id}")
// public String deleteProduct(@PathVariable Long id) {
// orderService.deleteProduct(id);
// return "redirect:/product";
// }
} }

View File

@ -8,6 +8,6 @@ import org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;
public interface OrderRepository extends JpaRepository<Order, Long> { public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("SELECT distinct o.supplier FROM Order o join Product p where p = ?1") @Query("SELECT o.supplier FROM Order o where ?1 member of o.products")
List<Supplier> getSomeSuppliers(Product product); List<Supplier> getSomeSuppliers(Product product);
} }

View File

@ -34,6 +34,8 @@
th:classappend="${#strings.equals(activeLink, '/supplier')} ? 'active' : ''">Поставщики</a> th:classappend="${#strings.equals(activeLink, '/supplier')} ? 'active' : ''">Поставщики</a>
<a class="nav-link" href="/order" <a class="nav-link" href="/order"
th:classappend="${#strings.equals(activeLink, '/order')} ? 'active' : ''">Заказы</a> th:classappend="${#strings.equals(activeLink, '/order')} ? 'active' : ''">Заказы</a>
<a class="nav-link" href="/order/dop"
th:classappend="${#strings.equals(activeLink, '/order')} ? 'active' : ''">Доп задание</a>
<a class="nav-link" href="/swagger-ui/index.html" target="_blank">Документация REST API</a> <a class="nav-link" href="/swagger-ui/index.html" target="_blank">Документация REST API</a>
<a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a> <a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a>
</ul> </ul>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>

View File

@ -1,135 +1,37 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>
<body> <body>
<div layout:fragment="content" class="mw-100"> <div layout:fragment="content">
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3"> <form action="#" th:action="@{/order/create}" th:object="${orderDto}" method="post">
<div class="d-flex justify-content-center">
<div style="margin-left: 2vw;">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Создать</span>
</button>
</div>
<div style="margin-left: 2vw;">
<a class="btn btn-outline-dark text-center button-fixed" th:href="@{/order}">
Отмена
</a>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary btn-success">Создать</button>
<a class="btn btn-primary btn-danger" th:href="@{/order}">
Назад
</a>
</div> </div>
<div class="mb-3">
<label for="supplier" class="form-label">Поставщик</label>
<select id="supplier" class="form-select" th:field="*{supplierId}" th:name="${selectedSupplier}">
<option th:each="value: ${suppliers}" th:selected="${selectedSupplier != null and selectedSupplier == value.id}" th:value="${value.id}" th:text=" ${value.name}"></option>
</select>
</div>
<p class="d-flex justify-content-between">
<label>Продукты:</label>
<div th:each="product : ${products}">
<input type="checkbox" name="genres" th:text="${product.name} + '(Цена: ' + ${product.cost} + ') '"
th:value="${product.id} "
th:field="*{productsId}"
/>
</div>
</p>
</form> </form>
<div class="d-flex container-fluid ">
<div style="width: 45vw; margin-right: 2vw" class="container-fluid">
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" >
<input name="wpName" type="hidden" th:value="${name}" />
<input name="PW" type="hidden" th:value="W" />
<div class="mb-3">
<label for="idW" class="form-label">Покупатель</label>
<select class="form-select" id = "idW" th:name="idPW">
<option th:each="value: ${buyers}"
th:value="${value.id}"
th:text="${value.buyerFirstName} + ' ' + ${value.buyerSecondName}">
</option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Добавить продукт</span>
</button>
</div>
</form>
<table class="table" >
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Название</th>
<th scope="col">Цена</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="product, iterator: ${carBuyers}">
<td th:text="${iterator.index} + 1"></td>
<td th:text="${product.name}"></td>
<td th:text="${product.cost}"></td>
<td>
<div>
<a type="button" class="btn btn-outline-dark text-center button-fixed"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${product.id}').click()|">
<i class="fa fa-trash"></i>
</a>
</div>
<form th:action="@{'/car/' + ${id} + '?PW=W&wpName='+ ${name} +'&idPW=' + ${product.id} + '&delete=true'}" method="post">
<button th:id="'remove-' + ${product.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
<div style="width: 45vw; margin-left: 2vw" class="container-fluid">
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3">
<input name="wpName" type="hidden" th:value="${name}" />
<input type="hidden" th:value="P" name="PW"/>
<div class="mb-3">
<label for="idP" class="form-label">Магазин</label>
<select class="form-select" id = "idP" th:name="idPW">
<option th:each="store, iterator: ${stores}"
th:value="${store.id}"
th:text="${store.storeName}">
</option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Добавить</span>
</button>
</div>
</form>
<table class="table" id="tbl-items">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Название</th>
<th scope="col">Цена</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="carStore, iterator: ${carStores}">
<td th:text="${iterator.index} + 1"></td>
<td th:text="${carStore.storeName}"></td>
<td>
<div>
<a type="button" class="btn btn-outline-dark text-center button-fixed"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${carStore.id}').click()|">
<i class="fa fa-trash"></i>
</a>
</div>
<form th:action="@{'/car/' + ${id} + '?PW=P&wpName='+ ${name} + '&idPW=' + ${carStore.id} + '&delete=true'}" method="post">
<button class = "btn btn-outline-dark text-center button-fixed" th:id="'remove-' + ${carStore.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<form action="#" th:action="@{/order/dop}" th:object="${productDto}" method="get">
<div class="mb-3">
<label for="author" class="form-label">Продукт</label>
<select id="author" class="form-select" th:field="*{id}" th:name="${selectedProduct}">
<option th:each="value: ${products}" th:selected="${selectedProduct != null and selectedProduct == value.id}" th:value="${value.id}" th:text="${value.name}"></option>
</select>
</div>
<button type="submit" class="btn btn-success button-fixed">Сформировать</button>
</form>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">ID</th>
<th scope="col">Название</th>
<th scope="col">Лицензия</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="supplier, iterator: ${suppliers}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${supplier.id}"/>
<td th:text="${supplier.name}"/>
<td th:text="${supplier.license}"/>
<td style="width: 10%">
<div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-warning button-fixed button-sm"
th:href="@{/supplier/edit/{id}(id=${supplier.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
</a>
<button type="button" class="btn btn-danger button-fixed button-sm"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${supplier.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/supplier/delete/{id}(id=${supplier.id})}" method="post">
<button th:id="'remove-' + ${supplier.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>
@ -8,7 +8,7 @@
<div layout:fragment="content"> <div layout:fragment="content">
<div> <div>
<a class="btn btn-success button-fixed" <a class="btn btn-success button-fixed"
th:href="@{/order/}"> th:href="@{/order/add}">
<i class="fa-solid fa-plus"></i> Создать заказ <i class="fa-solid fa-plus"></i> Создать заказ
</a> </a>
</div> </div>
@ -24,21 +24,13 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="product, iterator: ${orders}"> <tr th:each="order, iterator: ${orders}">
<th scope="row" th:text="${iterator.index} + 1"/> <th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${order.id}"/> <td th:text="${order.id}"/>
<td th:text="${order.dateOfOrder}" /> <td th:text="${order.dateOfOrder}" />
<td th:text="${order.supplier}" /> <td th:text="${order.supplier.name}" />
<td> <td>
<li th:each="product : ${order.products}" th:text="${product.name}"></li> <li th:each="product : ${order.products}" th:text="${product.name}"></li>
<!-- <select class="form-select" size="3" aria-label="size 3 select example">-->
<!-- <option selected>Open this select menu</option>-->
<!-- <option value="1">One</option>-->
<!-- <option value="2">Two</option>-->
<!-- <option value="3">Three</option>-->
<!-- </select>-->
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" <html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
</head> </head>