diff --git a/src/main/java/com/example/demo/master/MasterController.java b/src/main/java/com/example/demo/master/MasterController.java index 792188a..ff78f88 100644 --- a/src/main/java/com/example/demo/master/MasterController.java +++ b/src/main/java/com/example/demo/master/MasterController.java @@ -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 GetMasters(){ + return masterService.findAllMasters().stream().map(MasterDto::new).toList(); + } } diff --git a/src/main/java/com/example/demo/order/Order.java b/src/main/java/com/example/demo/order/Order.java index 4ae93cd..6ba91ef 100644 --- a/src/main/java/com/example/demo/order/Order.java +++ b/src/main/java/com/example/demo/order/Order.java @@ -25,8 +25,10 @@ public class Order { @JoinTable(name = "order_products", joinColumns = {@JoinColumn(name = "order_id")}, inverseJoinColumns = {@JoinColumn(name = "product_id")}) private List products; - public Order() { + private OrderStatus status; + public Order() { + status = OrderStatus.Open; } public Long getId() { @@ -62,6 +64,11 @@ public class Order { public List getProducts() { return products; } + public OrderStatus getStatus() {return status;} + + public void setStatus(OrderStatus status) { + this.status = status; + } public void setProducts(List products) { this.products = products; diff --git a/src/main/java/com/example/demo/order/OrderController.java b/src/main/java/com/example/demo/order/OrderController.java index fceb4e3..a193878 100644 --- a/src/main/java/com/example/demo/order/OrderController.java +++ b/src/main/java/com/example/demo/order/OrderController.java @@ -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 findOrders(@PathVariable("masterId") Long masterId) { + return orderService.findMastersOrders(masterId).stream().map(OrderDto::new).toList(); + } } diff --git a/src/main/java/com/example/demo/order/OrderDto.java b/src/main/java/com/example/demo/order/OrderDto.java index 689cd88..c3cbae0 100644 --- a/src/main/java/com/example/demo/order/OrderDto.java +++ b/src/main/java/com/example/demo/order/OrderDto.java @@ -14,10 +14,20 @@ public class OrderDto { private final Master master; private final List 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 getProducts() { return products; } + + public OrderStatus getStatus() { + return status; + } + public int getCost() { + return cost; + } } diff --git a/src/main/java/com/example/demo/order/OrderMvcController.java b/src/main/java/com/example/demo/order/OrderMvcController.java index db14635..1ebbb3a 100644 --- a/src/main/java/com/example/demo/order/OrderMvcController.java +++ b/src/main/java/com/example/demo/order/OrderMvcController.java @@ -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()); + } + return "MastersOrders"; + } } diff --git a/src/main/java/com/example/demo/order/OrderRepository.java b/src/main/java/com/example/demo/order/OrderRepository.java index 96f5f96..08a317e 100644 --- a/src/main/java/com/example/demo/order/OrderRepository.java +++ b/src/main/java/com/example/demo/order/OrderRepository.java @@ -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 { - Optional findByMaster(Master master); + Optional findByMasterAndStatus(Master master, OrderStatus status); + + List findByMaster(Master master); } diff --git a/src/main/java/com/example/demo/order/OrderService.java b/src/main/java/com/example/demo/order/OrderService.java index dab795b..7d663d7 100644 --- a/src/main/java/com/example/demo/order/OrderService.java +++ b/src/main/java/com/example/demo/order/OrderService.java @@ -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.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(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 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 findMastersOrders(Long masterId) { + return orderRepository.findByMaster(masterService.findMaster(masterId)); + } } diff --git a/src/main/java/com/example/demo/order/OrderStatus.java b/src/main/java/com/example/demo/order/OrderStatus.java new file mode 100644 index 0000000..10fa4da --- /dev/null +++ b/src/main/java/com/example/demo/order/OrderStatus.java @@ -0,0 +1,6 @@ +package com.example.demo.order; + +public enum OrderStatus { + Open, + Closed +} diff --git a/src/main/java/com/example/demo/product/ProductController.java b/src/main/java/com/example/demo/product/ProductController.java index 11204f6..ab975a8 100644 --- a/src/main/java/com/example/demo/product/ProductController.java +++ b/src/main/java/com/example/demo/product/ProductController.java @@ -55,4 +55,5 @@ public class ProductController { public ProductDto deleteProduct(@PathVariable Long id) { return new ProductDto(productService.deleteProduct(id)); } + } diff --git a/src/main/java/com/example/demo/product/ProductService.java b/src/main/java/com/example/demo/product/ProductService.java index bead501..df9c41b 100644 --- a/src/main/java/com/example/demo/product/ProductService.java +++ b/src/main/java/com/example/demo/product/ProductService.java @@ -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 findProducts(Long masterId) { - List products = productRepository.findByMaster(masterRepository.findById(masterId).orElseThrow(() -> new MasterNotFoundException(masterId))); + List products = productRepository.findByMaster(masterService.findMaster(masterId)); return products.stream().filter((item) -> item.getAvailable() != Status.Bought).toList(); } diff --git a/src/main/resources/templates/Header.html b/src/main/resources/templates/Header.html index d96014c..faf006d 100644 --- a/src/main/resources/templates/Header.html +++ b/src/main/resources/templates/Header.html @@ -21,8 +21,9 @@
Shop List diff --git a/src/main/resources/templates/MastersOrders.html b/src/main/resources/templates/MastersOrders.html new file mode 100644 index 0000000..9816fcc --- /dev/null +++ b/src/main/resources/templates/MastersOrders.html @@ -0,0 +1,51 @@ + + + + + Login + + + + + + +
+
+

Masters

+ +
+
+
+ + +
+
+
+ + + + + + + + + + + + + + +
#Cost
+
+ + \ No newline at end of file