1 | package com.example.salonbella.service.order;
|
---|
2 |
|
---|
3 | import com.example.salonbella.controller.cart.ShoppingCart;
|
---|
4 | import com.example.salonbella.entity.CartDetailEntity;
|
---|
5 | import com.example.salonbella.entity.OrderEntity;
|
---|
6 | import com.example.salonbella.entity.UserEntity;
|
---|
7 | import com.example.salonbella.repository.OrderRepository;
|
---|
8 | import com.example.salonbella.repository.ProductRepository;
|
---|
9 | import com.example.salonbella.repository.UserRepository;
|
---|
10 | import com.twilio.Twilio;
|
---|
11 | import com.twilio.rest.api.v2010.account.Message;
|
---|
12 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
13 | import org.springframework.security.core.Authentication;
|
---|
14 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
15 | import org.springframework.stereotype.Service;
|
---|
16 |
|
---|
17 | import javax.transaction.Transactional;
|
---|
18 | import java.time.LocalDate;
|
---|
19 | import java.util.ArrayList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Optional;
|
---|
23 |
|
---|
24 | @Service
|
---|
25 | public class OrderService {
|
---|
26 |
|
---|
27 | private final UserRepository userRepository;
|
---|
28 | private final ProductRepository productRepository;
|
---|
29 | private final OrderRepository orderRepository;
|
---|
30 | private static final String ACCOUNT_SID = "AC773ecbd8592a5cad8169cd06c3ce6e13";
|
---|
31 | private static final String AUTH_TOKEN = "51247e92a82c11f5a929c4e3ae6c2953";
|
---|
32 |
|
---|
33 | @Autowired
|
---|
34 | public OrderService(UserRepository userRepository, ProductRepository productRepository, OrderRepository orderRepository) {
|
---|
35 | this.userRepository = userRepository;
|
---|
36 | this.productRepository = productRepository;
|
---|
37 | this.orderRepository = orderRepository;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void saveOrder(ShoppingCart shoppingCart) {
|
---|
41 | Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
---|
42 | String username = auth.getName();
|
---|
43 | UserEntity user = userRepository.findByUsername(username);
|
---|
44 | OrderEntity orderEntity = new OrderEntity();
|
---|
45 | orderEntity.setCartDetails(shoppingCart.getCartDetails());
|
---|
46 | orderEntity.setUser(user);
|
---|
47 | orderEntity.setLocalDate(LocalDate.now());
|
---|
48 | orderEntity.setTotal(calculateTotalPrice(shoppingCart));
|
---|
49 | orderEntity.setStatus("PENDING");
|
---|
50 | orderRepository.save(orderEntity);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void changeQuantities(Map<String, String> allParams, ShoppingCart shoppingCart) {
|
---|
54 | for (String id : allParams.keySet()) {
|
---|
55 | for (CartDetailEntity c : shoppingCart.getCartDetails()) {
|
---|
56 | if (c.getId() == Long.parseLong(id)) {
|
---|
57 | c.setQuantity(Integer.parseInt(allParams.get(id)));
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | public double calculateTotalPrice(ShoppingCart shoppingCart) {
|
---|
66 | double total = 0.0;
|
---|
67 | for (CartDetailEntity c : shoppingCart.getCartDetails()) {
|
---|
68 | total += (c.getQuantity() * productRepository.findById(c.getId()).get().getPrice());
|
---|
69 | }
|
---|
70 | return total;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void clearCart(ShoppingCart shoppingCart) {
|
---|
74 | for (int i = 0; i < shoppingCart.getCartDetails().size(); i++) {
|
---|
75 | shoppingCart.getCartDetails().remove(i);
|
---|
76 | i--;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public List<OrderEntity> getUserOrders() {
|
---|
81 | Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
---|
82 | String username = auth.getName();
|
---|
83 | return makeOrderDetails(username);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public List<OrderEntity> makeOrderDetails(String username) {
|
---|
87 | UserEntity user = userRepository.findByUsername(username);
|
---|
88 | List<OrderEntity> orderEntityList = orderRepository.findAllByUser(user);
|
---|
89 | for (OrderEntity order : orderEntityList) {
|
---|
90 | for (CartDetailEntity cartDetailEntity : order.getCartDetails()) {
|
---|
91 | String name = productRepository.findById(cartDetailEntity.getId()).get().getName();
|
---|
92 | int quantity = cartDetailEntity.getQuantity();
|
---|
93 | order.getOrderDetails().add(new OrderDetail(name, quantity));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return orderEntityList;
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Transactional
|
---|
100 | public void cancelOrder(Long id) {
|
---|
101 | Optional<OrderEntity> orderEntity = orderRepository.findById(id);
|
---|
102 | if (orderEntity.isPresent()) {
|
---|
103 | if (getUser().getId().equals(orderEntity.get().getUser().getId())) {
|
---|
104 | orderRepository.delete(orderEntity.get());
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Transactional
|
---|
110 | public void cancelOrderAdmin(Long id) {
|
---|
111 | Optional<OrderEntity> orderEntity = orderRepository.findById(id);
|
---|
112 | orderEntity.ifPresent(orderRepository::delete);
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Transactional
|
---|
116 | public void changeOrderStatus(Long id) {
|
---|
117 | Optional<OrderEntity> orderEntity = orderRepository.findById(id);
|
---|
118 | orderEntity.ifPresent(entity -> entity.setStatus("AWAITING PICKUP"));
|
---|
119 | // Twilio.init(ACCOUNT_SID,AUTH_TOKEN);
|
---|
120 | // Message message = Message.creator(
|
---|
121 | // new com.twilio.type.PhoneNumber(getUser().getNumber().replaceFirst("0","+389")),
|
---|
122 | // new com.twilio.type.PhoneNumber("+1 815 567 9673"),
|
---|
123 | // "YOUR ORDER IS HERE")
|
---|
124 | // .create();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public UserEntity getUser() {
|
---|
128 | Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
---|
129 | String username = auth.getName();
|
---|
130 | UserEntity user = userRepository.findByUsername(username);
|
---|
131 | return user;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public List<OrderResponse> getAllOrders() {
|
---|
135 | List<OrderResponse> orderResponses = new ArrayList<>();
|
---|
136 | List<OrderEntity> orderEntities = orderRepository.findAll();
|
---|
137 | for (OrderEntity order : orderEntities) {
|
---|
138 | for (CartDetailEntity cartDetailEntity : order.getCartDetails()) {
|
---|
139 | String name = productRepository.findById(cartDetailEntity.getId()).get().getName();
|
---|
140 | int quantity = cartDetailEntity.getQuantity();
|
---|
141 | order.getOrderDetails().add(new OrderDetail(name, quantity));
|
---|
142 | }
|
---|
143 | OrderResponse orderResponse = new OrderResponse();
|
---|
144 | orderResponse.setId(order.getId());
|
---|
145 | orderResponse.setOrderDetails(order.getOrderDetails());
|
---|
146 | orderResponse.setLocalDate(order.getLocalDate());
|
---|
147 | orderResponse.setStatus(order.getStatus());
|
---|
148 | orderResponse.setTotal(order.getTotal());
|
---|
149 | orderResponse.setName(order.getUser().getName());
|
---|
150 | orderResponse.setSurname(order.getUser().getSurname());
|
---|
151 | orderResponses.add(orderResponse);
|
---|
152 | }
|
---|
153 | return orderResponses;
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|
157 |
|
---|