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