source: src/main/java/com/example/fooddeliverysystem/service/impl/OrderServiceImpl.java@ 8d11f8c

Last change on this file since 8d11f8c was 8d11f8c, checked in by jovanmanchev <jovanmanchev3003@…>, 20 months ago

code added, trial 2

  • Property mode set to 100644
File size: 5.5 KB
Line 
1package com.example.fooddeliverysystem.service.impl;
2
3import com.example.fooddeliverysystem.exceptions.FoodItemNotFoundException;
4import com.example.fooddeliverysystem.exceptions.OrderNotFoundException;
5import com.example.fooddeliverysystem.exceptions.SalePlaceNotFoundException;
6import com.example.fooddeliverysystem.model.*;
7import com.example.fooddeliverysystem.repository.*;
8import com.example.fooddeliverysystem.service.OrderService;
9import com.example.fooddeliverysystem.service.PriceService;
10import com.example.fooddeliverysystem.service.SalePlaceService;
11import com.example.fooddeliverysystem.service.UserService;
12import org.springframework.stereotype.Service;
13
14import java.sql.Timestamp;
15import java.time.LocalDate;
16import java.time.LocalDateTime;
17import java.util.ArrayList;
18import java.util.List;
19
20@Service
21public class OrderServiceImpl implements OrderService {
22
23 private final SalePlaceService salePlaceService;
24 private final OrderRepository orderRepository;
25 private final OrderHasFoodRepository orderHasFoodRepository;
26 private final UserRepository userRepository;
27 private final ConsumerRepository consumerRepository;
28
29 private final FoodItemRepository foodItemRepository;
30 private final DeliverRepository deliverRepository;
31 private final PriceService priceService;
32 public OrderServiceImpl(SalePlaceService salePlaceRepository, OrderRepository orderRepository, OrderHasFoodRepository orderHasFoodRepository, UserRepository userRepository,
33 ConsumerRepository consumerRepository, FoodItemRepository foodItemRepository, PriceService priceService, DeliverRepository deliverRepository) {
34 this.salePlaceService = salePlaceRepository;
35 this.orderRepository = orderRepository;
36 this.orderHasFoodRepository = orderHasFoodRepository;
37 this.userRepository = userRepository;
38 this.consumerRepository = consumerRepository;
39 this.foodItemRepository = foodItemRepository;
40 this.priceService = priceService;
41 this.deliverRepository = deliverRepository;
42 }
43
44 @Override
45 public void placeOrder(String typeOfPayment, Long salePlaceId, List<Long> foodIds, List<Integer> foodPrices, List<Integer> foodQuantities, String username) throws SalePlaceNotFoundException {
46 SalePlace salePlace = this.salePlaceService.findSalePlaceServiceById(salePlaceId);
47 User user = this.userRepository.findByUsername(username).get();
48 Consumer consumer = this.consumerRepository.findById(user.getUser_id()).get();
49
50 Order order = new Order(typeOfPayment,"kreirana", Timestamp.valueOf(LocalDateTime.now()), salePlace ,consumer);
51 List<OrderHasFood> orderHasFoodList = new ArrayList<>();
52 order = orderRepository.save(order);
53 for(int i = 0; i < foodIds.size(); i++){
54
55 if(foodQuantities.get(i) != null) {
56 OrderHasFood orderHasFood = new OrderHasFood(new OrderHasFoodKey(foodIds.get(i), order.getOrderId()), foodQuantities.get(i));
57 orderHasFoodList.add(orderHasFood);
58 }
59 }
60 this.orderHasFoodRepository.saveAll(orderHasFoodList);
61
62
63
64 }
65
66 @Override
67 public Order changeOrderStatus(Long orderId, String status) {
68 Order order = this.orderRepository.findById(orderId).get();
69 order.setOrderStatus(status);
70
71 return this.orderRepository.save(order);
72 }
73
74 @Override
75 public Integer calculateCostOfOrder(Long orderId) throws FoodItemNotFoundException {
76 Integer totalCost = 0;
77 List<OrderHasFood> orderHasFoodList = this.orderHasFoodRepository.findAllByOrderHasFoodKeyOrderId(orderId);
78 for(OrderHasFood orderHasFood: orderHasFoodList){
79 Long foodItemId = orderHasFood.getOrderHasFoodKey().getFoodItemId();
80 FoodItem foodItem = this.foodItemRepository.findById(foodItemId).get();
81 Integer quantity = orderHasFood.getQuantity();
82 Integer price = this.priceService.findCurrentPriceForFoodItem(foodItem).getCost();
83 totalCost += quantity * price;
84
85 }
86
87 return totalCost;
88 }
89
90 @Override
91 public List<Order> findAllOrdersReadyToBeDelivered() {
92 return this.orderRepository.findAllByOrderStatus("spremna");
93 }
94
95 @Override
96 public List<Order> findAllOrdersForDeliver(String username) {
97 User user = this.userRepository.findByUsername(username).get();
98 Deliver deliver = this.deliverRepository.findById(user.getUser_id()).get();
99 return this.orderRepository.findAllByOrderStatusAndDeliver("prevzemena", deliver);
100 }
101
102 @Override
103 public Order updateOrderDeliver(String username, Long orderId) {
104 User user = this.userRepository.findByUsername(username).get();
105 Deliver deliver = this.deliverRepository.findById(user.getUser_id()).get();
106 Order order = this.orderRepository.findById(orderId).get();
107 order.setDeliver(deliver);
108 return this.orderRepository.save(order);
109 }
110
111 @Override
112 public Order findOrderById(Long orderId) throws OrderNotFoundException {
113 return this.orderRepository.findById(orderId).orElseThrow(() -> new OrderNotFoundException("order cannot be found"));
114 }
115
116 @Override
117 public List<Order> findAllOrdersForCustomer(String username) {
118 User user = this.userRepository.findByUsername(username).get();
119 Consumer customer = this.consumerRepository.findById(user.getUser_id()).get();
120 return this.orderRepository.findAllByConsumer(customer);
121 }
122
123 @Override
124 public void saveOrder(Order order) {
125 this.orderRepository.save(order);
126 }
127}
Note: See TracBrowser for help on using the repository browser.