Lab 5 ExtraTask
This commit is contained in:
parent
9b8134b185
commit
c98e598675
@ -2,6 +2,7 @@ package com.example.demo.master;
|
||||
|
||||
import com.example.demo.WebConfiguration;
|
||||
import com.example.demo.order.OrderService;
|
||||
import com.example.demo.product.ProductDto;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
@ -62,10 +63,13 @@ public class MasterController {
|
||||
return new MasterDto(masterService.deleteMaster(masterService.getCurrentMasterId()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/log_out")
|
||||
public void logOut() {
|
||||
masterService.setCurrentMasterId(0L);
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<MasterDto> GetMasters(){
|
||||
return masterService.findAllMasters().stream().map(MasterDto::new).toList();
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,10 @@ public class Order {
|
||||
@JoinTable(name = "order_products", joinColumns = {@JoinColumn(name = "order_id")}, inverseJoinColumns = {@JoinColumn(name = "product_id")})
|
||||
private List<Product> products;
|
||||
|
||||
public Order() {
|
||||
private OrderStatus status;
|
||||
|
||||
public Order() {
|
||||
status = OrderStatus.Open;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -62,6 +64,11 @@ public class Order {
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
public OrderStatus getStatus() {return status;}
|
||||
|
||||
public void setStatus(OrderStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void setProducts(List<Product> products) {
|
||||
this.products = products;
|
||||
|
@ -2,13 +2,8 @@ package com.example.demo.order;
|
||||
|
||||
import com.example.demo.WebConfiguration;
|
||||
import com.example.demo.master.MasterService;
|
||||
import com.example.demo.product.Product;
|
||||
import com.example.demo.product.ProductDto;
|
||||
import com.example.demo.product.ProductService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@ -51,4 +46,9 @@ public class OrderController {
|
||||
public void deleteProduct(@PathVariable("product") Long productId) {
|
||||
orderService.deleteProduct(masterService.getCurrentMasterId(), productId);
|
||||
}
|
||||
|
||||
@GetMapping("/findOrders/{masterId}")
|
||||
public List<OrderDto> findOrders(@PathVariable("masterId") Long masterId) {
|
||||
return orderService.findMastersOrders(masterId).stream().map(OrderDto::new).toList();
|
||||
}
|
||||
}
|
||||
|
@ -14,10 +14,20 @@ public class OrderDto {
|
||||
private final Master master;
|
||||
private final List<Product> products;
|
||||
|
||||
private final OrderStatus status;
|
||||
|
||||
private final int cost;
|
||||
|
||||
public OrderDto(Order order) {
|
||||
id = order.getId();
|
||||
master = order.getMaster();
|
||||
products = order.getProducts();
|
||||
status = order.getStatus();
|
||||
int cost1 = 0;
|
||||
for (var item : products) {
|
||||
cost1 += item.getCost();
|
||||
}
|
||||
cost = cost1;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
@ -31,4 +41,11 @@ public class OrderDto {
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
public int getCost() {
|
||||
return cost;
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Controller
|
||||
@ -46,4 +48,19 @@ public class OrderMvcController {
|
||||
orderService.buyProducts(masterService.getCurrentMasterId());
|
||||
return "redirect:/product";
|
||||
}
|
||||
|
||||
@GetMapping("/masters_order")
|
||||
public String MastersOrders(Model model, @RequestParam(value = "master_id", defaultValue = "-1") String masterId) {
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("masters", masterService.findAllMasters());
|
||||
|
||||
if (!Objects.equals(masterId, "-1")) {
|
||||
model.addAttribute("orders", orderService.findMastersOrders(Long.valueOf(masterId)).
|
||||
stream().map(OrderDto::new).toList());
|
||||
}
|
||||
else {
|
||||
model.addAttribute("orders", new ArrayList<OrderDto>());
|
||||
}
|
||||
return "MastersOrders";
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,11 @@ package com.example.demo.order;
|
||||
import com.example.demo.master.Master;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
Optional<Order> findByMaster(Master master);
|
||||
Optional<Order> findByMasterAndStatus(Master master, OrderStatus status);
|
||||
|
||||
List<Order> findByMaster(Master master);
|
||||
}
|
||||
|
@ -1,90 +1,72 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.master.MasterNotFoundException;
|
||||
import com.example.demo.master.MasterRepository;
|
||||
import com.example.demo.product.Product;
|
||||
import com.example.demo.product.ProductNotFoundException;
|
||||
import com.example.demo.product.ProductRepository;
|
||||
import com.example.demo.product.Status;
|
||||
import com.example.demo.master.MasterService;
|
||||
import com.example.demo.product.*;
|
||||
import com.example.demo.util.validation.ValidatorUtil;
|
||||
import jakarta.persistence.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
private final OrderRepository orderRepository;
|
||||
private final ProductRepository productRepository;
|
||||
private final MasterRepository masterRepository;
|
||||
private final ProductService productService;
|
||||
private final MasterService masterService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public OrderService(OrderRepository orderRepository, ProductRepository productRepository,
|
||||
MasterRepository masterRepository, ValidatorUtil validatorUtil) {
|
||||
public OrderService(OrderRepository orderRepository, ProductService productService,
|
||||
MasterService masterService, ValidatorUtil validatorUtil) {
|
||||
this.orderRepository = orderRepository;
|
||||
this.productRepository = productRepository;
|
||||
this.masterRepository = masterRepository;
|
||||
this.productService = productService;
|
||||
this.masterService = masterService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order addOrder(Long masterId) {
|
||||
final Order order = new Order();
|
||||
order.setMaster(masterRepository.findById(masterId).orElseThrow(() -> new MasterNotFoundException(masterId)));
|
||||
order.setMaster(masterService.findMaster(masterId));
|
||||
validatorUtil.validate(order);
|
||||
return orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addProduct(Long masterId, Long productId) {
|
||||
final Order order = orderRepository.findByMaster(masterRepository.findById(masterId).orElseThrow(
|
||||
() -> new MasterNotFoundException(masterId))).orElseThrow(() -> new OrderNotFoundException(masterId));
|
||||
final Product product = productRepository.findById(productId).orElseThrow(() ->
|
||||
new ProductNotFoundException(productId));
|
||||
order.addProduct(product);
|
||||
final Order order = findOrder(masterId);
|
||||
final Product product = productService.findProduct(productId);
|
||||
order.addProduct(product); var products = new ArrayList<Product>();
|
||||
product.setOrder(order);
|
||||
product.setAvailable(Status.Ordered);
|
||||
orderRepository.save(order);
|
||||
productRepository.save(product);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void buyProducts(Long masterId) {
|
||||
final Order order = orderRepository.findByMaster(masterRepository.findById(masterId).orElseThrow(
|
||||
() -> new MasterNotFoundException(masterId))).orElseThrow(() -> new OrderNotFoundException(masterId));
|
||||
var products = new ArrayList<Product>(order.getProducts());
|
||||
for (var item: products) {
|
||||
final Order order = findOrder(masterId);
|
||||
for (var item: order.getProducts()) {
|
||||
item.setAvailable(Status.Bought);
|
||||
productRepository.save(item);
|
||||
order.removeProduct(item);
|
||||
}
|
||||
order.setStatus(OrderStatus.Closed);
|
||||
orderRepository.save(order);
|
||||
addOrder(masterId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteProduct(Long masterId, Long productId) {
|
||||
final Order order = orderRepository.findByMaster(masterRepository.findById(masterId).orElseThrow(
|
||||
() -> new MasterNotFoundException(masterId))).orElseThrow(() -> new OrderNotFoundException(masterId));
|
||||
final Product product = productRepository.findById(productId).orElseThrow(() ->
|
||||
new ProductNotFoundException(productId));
|
||||
final Order order = findOrder(masterId);
|
||||
final Product product = productService.findProduct(productId);
|
||||
order.removeProduct(product);
|
||||
product.removeOrder(order);
|
||||
product.setAvailable(Status.Availible);
|
||||
orderRepository.save(order);
|
||||
productRepository.save(product);
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
public Order findOrder(Long masterId) {
|
||||
final Order order = orderRepository.findByMaster(masterRepository.findById(masterId).
|
||||
orElseThrow(() -> new MasterNotFoundException(masterId))).orElseThrow(
|
||||
return orderRepository.findByMasterAndStatus(masterService.findMaster(masterId), OrderStatus.Open).orElseThrow(
|
||||
() -> new OrderNotFoundException(masterId));
|
||||
return order;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@ -95,8 +77,9 @@ public class OrderService {
|
||||
@Transactional
|
||||
public Order deleteOrder(Long id) {
|
||||
final Order order = findOrder(id);
|
||||
orderRepository.delete(order);
|
||||
return order;
|
||||
order.setStatus(OrderStatus.Closed);
|
||||
orderRepository.save(order);
|
||||
return addOrder(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -104,13 +87,9 @@ public class OrderService {
|
||||
orderRepository.deleteAll();
|
||||
}
|
||||
|
||||
// EXTRA TASK FROM LAB 3
|
||||
// @Transactional
|
||||
// public List<Order> findOrdersWithProduct(Long masterId, Product product) {
|
||||
// return em.createQuery("SELECT o FROM Order o WHERE (o.master._id = :masterId) and (:product member OF o.products)",
|
||||
// Order.class)
|
||||
// .setParameter("product", product)
|
||||
// .setParameter("masterId", masterId)
|
||||
// .getResultList();
|
||||
// }
|
||||
|
||||
@Transactional
|
||||
public List<Order> findMastersOrders(Long masterId) {
|
||||
return orderRepository.findByMaster(masterService.findMaster(masterId));
|
||||
}
|
||||
}
|
||||
|
6
src/main/java/com/example/demo/order/OrderStatus.java
Normal file
6
src/main/java/com/example/demo/order/OrderStatus.java
Normal file
@ -0,0 +1,6 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
public enum OrderStatus {
|
||||
Open,
|
||||
Closed
|
||||
}
|
@ -55,4 +55,5 @@ public class ProductController {
|
||||
public ProductDto deleteProduct(@PathVariable Long id) {
|
||||
return new ProductDto(productService.deleteProduct(id));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,9 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.master.MasterRepository;
|
||||
import com.example.demo.master.MasterDto;
|
||||
import com.example.demo.master.MasterNotFoundException;
|
||||
import com.example.demo.master.*;
|
||||
import com.example.demo.util.validation.ValidatorUtil;
|
||||
import jakarta.persistence.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@ -17,19 +12,19 @@ import java.util.Optional;
|
||||
public class ProductService {
|
||||
private final ProductRepository productRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
private final MasterRepository masterRepository;
|
||||
private final MasterService masterService;
|
||||
|
||||
public ProductService(ProductRepository productRepository, MasterRepository masterRepository, ValidatorUtil validatorUtil) {
|
||||
public ProductService(ProductRepository productRepository, MasterService masterService, ValidatorUtil validatorUtil) {
|
||||
this.productRepository = productRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
this.masterRepository = masterRepository;
|
||||
this.masterService = masterService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product addProduct(String name, Integer cost, Long masterId) {
|
||||
final Product product = new Product(name, cost);
|
||||
validatorUtil.validate(product);
|
||||
product.setMaster(masterRepository.findById(masterId).orElseThrow(() -> new MasterNotFoundException(masterId)));
|
||||
product.setMaster(masterService.findMaster(masterId));
|
||||
return productRepository.save(product);
|
||||
}
|
||||
|
||||
@ -41,7 +36,7 @@ public class ProductService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findProducts(Long masterId) {
|
||||
List<Product> products = productRepository.findByMaster(masterRepository.findById(masterId).orElseThrow(() -> new MasterNotFoundException(masterId)));
|
||||
List<Product> products = productRepository.findByMaster(masterService.findMaster(masterId));
|
||||
return products.stream().filter((item) -> item.getAvailable() != Status.Bought).toList();
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,9 @@
|
||||
<svg class="bi me-2" width="40" height="32" role="img" aria-label="Bootstrap"><use xlink:href="#bootstrap"></use></svg>
|
||||
</a>
|
||||
<ul class="nav col-12 col-md-auto mb-2 justify-content-center mb-md-0">
|
||||
<li th:if="${user.firstName != null}"><a href="/product/my_products" class="nav-link px-2 link-dark">My Products</a></li>
|
||||
<li><a href="/product" class="nav-link px-2 link-dark">Products</a></li>
|
||||
<li th:if="${user.firstName != null}"><a href="/product/my_products" class="nav-link px-2 link-dark">My Products</a></li>
|
||||
<li><a href="/product" class="nav-link px-2 link-dark">Products</a></li>
|
||||
<li th:if="${user.firstName != null}"><a href="/order/masters_order" class="nav-link px-2 link-dark">Orders</a></li>
|
||||
</ul>
|
||||
<div th:if="${user.firstName != null}" class="col-md-3 text-end">
|
||||
<a href="/order" class="btn btn-outline-primary me-2">Shop List</a>
|
||||
|
51
src/main/resources/templates/MastersOrders.html
Normal file
51
src/main/resources/templates/MastersOrders.html
Normal file
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<div>
|
||||
<h1 class="text-center">Masters</h1>
|
||||
|
||||
<div class="container">
|
||||
<div class="row gy-5 p-2">
|
||||
<form th:action="@{/order/masters_order}" method="get">
|
||||
<select th:each="master : ${masters}" th:name="master_id" class="form-select" aria-label="Default select example">
|
||||
<option th:id="master_id" th:value="${master.id}" >
|
||||
<span th:text="${master.firstName + ' ' + master.lastName }"></span>
|
||||
</option>
|
||||
</select>
|
||||
<button class="btn btn-primary w-100">Check</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<table class="table table-dark">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Cost</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="order : ${orders}">
|
||||
<th scope="row" th:text="${order.id}"></th>
|
||||
<td th:text="${order.cost}"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user