Index: src/main/java/com/example/salonbella/controller/cart/CartController.java
===================================================================
--- src/main/java/com/example/salonbella/controller/cart/CartController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/controller/cart/CartController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,41 @@
+package com.example.salonbella.controller.cart;
+
+import com.example.salonbella.service.ShoppingCartService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@Controller
+public class CartController {
+
+    private final ShoppingCartService shoppingCartService;
+
+    @Autowired
+    public CartController(ShoppingCartService shoppingCartService) {
+        this.shoppingCartService = shoppingCartService;
+    }
+
+    @PostMapping("/add-to-cart")
+    public String addProductToCart(@RequestParam(name = "id") String id) {
+        if(!shoppingCartService.existInCart(Long.parseLong(id))) {
+            shoppingCartService.addProduct(Long.parseLong(id));
+        }
+        return "redirect:/order";
+    }
+
+    @GetMapping("/my-cart")
+    public String getCartDetails(Model model) {
+        model.addAttribute("products", shoppingCartService.getShoppingCartDetails());
+        return "user-cart";
+    }
+
+    @GetMapping("/delete-product-from-cart")
+    public String deleteProductFromCart(@RequestParam(name = "id") String id) {
+        shoppingCartService.deleteProductFromCart(Long.parseLong(id));
+        return "redirect:/my-cart";
+    }
+
+}
Index: src/main/java/com/example/salonbella/controller/cart/ShoppingCart.java
===================================================================
--- src/main/java/com/example/salonbella/controller/cart/ShoppingCart.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/controller/cart/ShoppingCart.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,26 @@
+package com.example.salonbella.controller.cart;
+
+import com.example.salonbella.entity.CartDetailEntity;
+import org.springframework.context.annotation.Scope;
+import org.springframework.context.annotation.ScopedProxyMode;
+import org.springframework.stereotype.Component;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
+public class ShoppingCart implements Serializable {
+    private List<CartDetailEntity> cartDetailEntities = new ArrayList<>();
+    public List<CartDetailEntity> getCartDetails() {
+        return cartDetailEntities;
+    }
+
+    @Override
+    public String toString() {
+        return "ShoppingCart{" +
+                "cartDetails=" + cartDetailEntities +
+                '}';
+    }
+}
Index: src/main/java/com/example/salonbella/controller/order/OrderController.java
===================================================================
--- src/main/java/com/example/salonbella/controller/order/OrderController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/controller/order/OrderController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,75 @@
+package com.example.salonbella.controller.order;
+
+import com.example.salonbella.service.ProductService;
+import com.example.salonbella.service.ShoppingCartService;
+import com.example.salonbella.service.order.OrderService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.util.Map;
+
+@Controller
+public class OrderController {
+
+    private final OrderService orderService;
+    private final ShoppingCartService shoppingCartService;
+    private final ProductService productService;
+
+    @Autowired
+    public OrderController(OrderService orderService, ShoppingCartService shoppingCartService, ProductService productService) {
+        this.orderService = orderService;
+        this.shoppingCartService = shoppingCartService;
+        this.productService = productService;
+    }
+
+
+
+    @PostMapping("/order")
+    public String order(@RequestParam Map<String, String> allParams) {
+        orderService.changeQuantities(allParams, shoppingCartService.getShoppingCart());
+        orderService.saveOrder(shoppingCartService.getShoppingCart());
+        orderService.clearCart(shoppingCartService.getShoppingCart());
+        return "redirect:/userDashboard";
+    }
+
+    @GetMapping("/my-orders")
+    public String getMyOrders(Model model) {
+        model.addAttribute("orders", orderService.getUserOrders());
+        return "user-my-orders";
+    }
+
+    @PostMapping("/cancel-order")
+    public String cancelOrder(@RequestParam(name = "id") String id) {
+        orderService.cancelOrder(Long.parseLong(id));
+        return "redirect:/my-orders";
+    }
+
+    @GetMapping("/admin-get-orders")
+    public String getOrders(Model model) {
+        model.addAttribute("orders", orderService.getAllOrders());
+        return "admin-orders";
+    }
+
+    @PostMapping("/admin-change-status")
+    public String changeOrderStatus(@RequestParam(name = "id") String id) {
+        orderService.changeOrderStatus(Long.parseLong(id));
+        return "redirect:/admin-get-orders";
+    }
+
+    @PostMapping("/admin-cancel-order")
+    public String cancelOrderAdmin(@RequestParam(name = "id") String id) {
+        orderService.cancelOrderAdmin(Long.parseLong(id));
+        return "redirect:/admin-get-orders";
+    }
+
+    @GetMapping("/order")
+    public String getOrderPage(Model model) {
+        model.addAttribute("products", productService.getProducts());
+        return "user-order";
+    }
+}
+
Index: src/main/java/com/example/salonbella/controller/product/ProductController.java
===================================================================
--- src/main/java/com/example/salonbella/controller/product/ProductController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/controller/product/ProductController.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,57 @@
+package com.example.salonbella.controller.product;
+
+import com.example.salonbella.service.ProductService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+@Controller
+public class ProductController {
+
+    private final ProductService productService;
+
+
+    @Autowired
+    public ProductController(ProductService productService) {
+        this.productService = productService;
+
+    }
+
+
+    @GetMapping("/admin-add-product")
+    public String getAddProductPage() {
+        return "admin-product";
+    }
+
+    @PostMapping("/admin-add-product")
+    public String addProduct(@RequestParam(name = "name") String name, @RequestParam(name = "category") String category,
+                             @RequestParam(name = "price") String price,
+                             @RequestParam(name = "description") String description,
+                             @RequestParam(name = "image") MultipartFile multipartFile) throws IOException {
+        productService.addProduct(name, category, price, description, multipartFile);
+        return "redirect:/adminDashboard";
+    }
+
+
+
+    @GetMapping("/admin-remove-product")
+    public String getRemoveProductPage(Model model) {
+        model.addAttribute("products", productService.getProducts());
+        return "admin-remove-product";
+    }
+
+    @PostMapping("/admin-remove-product")
+    public String removeProduct(@RequestParam(name = "id") String id) {
+        productService.removeProduct(Long.parseLong(id));
+        return "redirect:/admin-remove-product";
+    }
+
+
+}
+
Index: src/main/java/com/example/salonbella/entity/CartDetailEntity.java
===================================================================
--- src/main/java/com/example/salonbella/entity/CartDetailEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/entity/CartDetailEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,51 @@
+package com.example.salonbella.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+public class CartDetailEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    private long product_id;
+    private int quantity;
+
+    public CartDetailEntity(long product_id, int quantity) {
+        this.product_id = product_id;
+        this.quantity = quantity;
+    }
+
+    public CartDetailEntity() {
+
+    }
+
+    public long getId() {
+        return product_id;
+    }
+
+    public void setId(long product_id) {
+        this.product_id = product_id;
+    }
+
+    public int getQuantity() {
+        return quantity;
+    }
+
+    public void setQuantity(int quantity) {
+        this.quantity = quantity;
+    }
+
+    @Override
+    public String toString() {
+        return "CartDetail{" +
+                "product_id=" + product_id +
+                ", quantity=" + quantity +
+                '}';
+    }
+}
+
Index: src/main/java/com/example/salonbella/entity/OrderEntity.java
===================================================================
--- src/main/java/com/example/salonbella/entity/OrderEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/entity/OrderEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,93 @@
+package com.example.salonbella.entity;
+
+import com.example.salonbella.service.order.OrderDetail;
+
+import javax.persistence.*;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Table(name = "orders")
+public class OrderEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @OneToMany(cascade = CascadeType.ALL)
+    private List<CartDetailEntity> cartDetailEntities = new ArrayList<>();
+
+    @Column(name = "status", nullable = false, length = 30)
+    private String status;
+
+    @Column(name = "date", nullable = false, length = 30)
+    private LocalDate localDate;
+
+    @Column(name = "total_price", nullable = false)
+    private double total;
+
+    @ManyToOne
+    @JoinColumn(name = "user_id", nullable = false)
+    private UserEntity user;
+
+    @Transient
+    private List<OrderDetail> orderDetails = new ArrayList<>();
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public List<CartDetailEntity> getCartDetails() {
+        return cartDetailEntities;
+    }
+
+    public List<OrderDetail> getOrderDetails() {
+        return orderDetails;
+    }
+
+    public void setOrderDetails(List<OrderDetail> orderDetails) {
+        this.orderDetails = orderDetails;
+    }
+
+    public void setCartDetails(List<CartDetailEntity> cartDetailEntities) {
+        this.cartDetailEntities = cartDetailEntities;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public LocalDate getLocalDate() {
+        return localDate;
+    }
+
+    public void setLocalDate(LocalDate localDate) {
+        this.localDate = localDate;
+    }
+
+    public double getTotal() {
+        return total;
+    }
+
+    public void setTotal(double total) {
+        this.total = total;
+    }
+
+    public UserEntity getUser() {
+        return user;
+    }
+
+    public void setUser(UserEntity user) {
+        this.user = user;
+    }
+}
+
Index: src/main/java/com/example/salonbella/entity/ProductEntity.java
===================================================================
--- src/main/java/com/example/salonbella/entity/ProductEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/entity/ProductEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,87 @@
+package com.example.salonbella.entity;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "products")
+public class ProductEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @Column(name = "name", nullable = false, length = 50)
+    private String name;
+
+    @Column(name = "description", nullable = false, length = 60)
+    private String description;
+
+    @Column(name = "category", nullable = false, length = 50)
+    private String category;
+
+    @Column(name = "price", nullable = false, length = 50)
+    private double price;
+
+
+    @Lob
+    private byte[] content;
+
+    private String base64;
+
+
+    public String getBase64() {
+        return base64;
+    }
+
+    public void setBase64(String base64) {
+        this.base64 = base64;
+    }
+
+    public byte[] getContent() {
+        return content;
+    }
+
+    public void setContent(byte[] content) {
+        this.content = content;
+    }
+
+    public String getCategory() {
+        return category;
+    }
+
+    public void setCategory(String category) {
+        this.category = category;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+}
Index: src/main/java/com/example/salonbella/entity/UserEntity.java
===================================================================
--- src/main/java/com/example/salonbella/entity/UserEntity.java	(revision 0f3491d058d3b33632a55dae8783810ed77e16f9)
+++ src/main/java/com/example/salonbella/entity/UserEntity.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -37,4 +37,7 @@
     @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
     private Set<ReservationEntity> reservations;
+
+    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
+    private Set<OrderEntity> orders;
 
     public UserEntity() {
Index: src/main/java/com/example/salonbella/repository/OrderRepository.java
===================================================================
--- src/main/java/com/example/salonbella/repository/OrderRepository.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/repository/OrderRepository.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,14 @@
+package com.example.salonbella.repository;
+
+import com.example.salonbella.entity.OrderEntity;
+import com.example.salonbella.entity.UserEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.List;
+
+public interface OrderRepository extends JpaRepository<OrderEntity, Long> {
+
+    List<OrderEntity> findAllByUser(UserEntity user);
+
+
+}
Index: src/main/java/com/example/salonbella/repository/ProductRepository.java
===================================================================
--- src/main/java/com/example/salonbella/repository/ProductRepository.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/repository/ProductRepository.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,10 @@
+package com.example.salonbella.repository;
+
+import com.example.salonbella.entity.ProductEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.Optional;
+
+public interface ProductRepository extends JpaRepository<ProductEntity,Long> {
+    Optional<ProductEntity> findById(Long id);
+}
Index: src/main/java/com/example/salonbella/service/ProductService.java
===================================================================
--- src/main/java/com/example/salonbella/service/ProductService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/service/ProductService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,68 @@
+package com.example.salonbella.service;
+
+import com.example.salonbella.entity.ProductEntity;
+import com.example.salonbella.repository.ProductRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.transaction.Transactional;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Optional;
+import java.util.zip.DataFormatException;
+import java.util.zip.Inflater;
+
+@Service
+public class ProductService {
+
+    private final ProductRepository productRepository;
+
+    @Autowired
+    public ProductService(ProductRepository productRepository) {
+        this.productRepository = productRepository;
+    }
+
+    public void addProduct(String name, String category, String price, String description, MultipartFile multipartFile) throws IOException {
+        ProductEntity productEntity = new ProductEntity();
+        productEntity.setName(name);
+        productEntity.setContent(multipartFile.getBytes());
+        productEntity.setDescription(description);
+        productEntity.setCategory(category);
+        productEntity.setPrice(Double.parseDouble(price));
+        productRepository.save(productEntity);
+    }
+
+    public List<ProductEntity> getProducts() {
+        List<ProductEntity> productEntities = productRepository.findAll();
+        for (ProductEntity p : productEntities) {
+            p.setBase64(Base64.getEncoder().encodeToString(p.getContent()));
+        }
+        return productEntities;
+    }
+
+    public byte[] decompressBytes(byte[] data) {
+        Inflater inflater = new Inflater();
+        inflater.setInput(data);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
+        byte[] buffer = new byte[1024];
+        try {
+            while (!inflater.finished()) {
+                int count = inflater.inflate(buffer);
+                outputStream.write(buffer, 0, count);
+            }
+            outputStream.close();
+        } catch (IOException ioe) {
+        } catch (DataFormatException e) {
+        }
+        return outputStream.toByteArray();
+    }
+
+    @Transactional
+    public void removeProduct(Long id) {
+        Optional<ProductEntity> product = productRepository.findById(id);
+        product.ifPresent(productRepository::delete);
+    }
+}
Index: src/main/java/com/example/salonbella/service/ShoppingCartService.java
===================================================================
--- src/main/java/com/example/salonbella/service/ShoppingCartService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/service/ShoppingCartService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,62 @@
+package com.example.salonbella.service;
+
+import com.example.salonbella.controller.cart.ShoppingCart;
+import com.example.salonbella.entity.CartDetailEntity;
+import com.example.salonbella.entity.ProductEntity;
+import com.example.salonbella.repository.ProductRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+
+@Service
+public class ShoppingCartService {
+
+    private ShoppingCart shoppingCart;
+    private ProductRepository productRepository;
+
+    @Autowired
+    public ShoppingCartService(ShoppingCart shoppingCart, ProductRepository productRepository) {
+        this.shoppingCart = shoppingCart;
+        this.productRepository = productRepository;
+    }
+
+    public void addProduct(Long id) {
+        shoppingCart.getCartDetails().add(new CartDetailEntity(id, 1));
+    }
+
+    public ShoppingCart getShoppingCart() {
+        return shoppingCart;
+    }
+
+    public List<ProductEntity> getShoppingCartDetails() {
+        List<ProductEntity> productEntities = new ArrayList<>();
+        List<CartDetailEntity> cartDetailEntities = shoppingCart.getCartDetails();
+        for (CartDetailEntity c : cartDetailEntities) {
+            ProductEntity p = productRepository.findById(c.getId()).get();
+            p.setBase64(Base64.getEncoder().encodeToString(p.getContent()));
+            productEntities.add(p);
+        }
+        return productEntities;
+    }
+
+    public void deleteProductFromCart(Long id) {
+        for (CartDetailEntity c : shoppingCart.getCartDetails()) {
+            if (c.getId() == id) {
+                shoppingCart.getCartDetails().remove(c);
+                break;
+            }
+        }
+    }
+
+    public boolean existInCart(Long id) {
+        for (CartDetailEntity c : shoppingCart.getCartDetails()) {
+            if (c.getId() == id) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
Index: src/main/java/com/example/salonbella/service/order/OrderDetail.java
===================================================================
--- src/main/java/com/example/salonbella/service/order/OrderDetail.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/service/order/OrderDetail.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,27 @@
+package com.example.salonbella.service.order;
+
+public class OrderDetail {
+    private String name;
+    private int quantity;
+
+    public OrderDetail(String name, int quantity) {
+        this.name = name;
+        this.quantity = quantity;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getQuantity() {
+        return quantity;
+    }
+
+    public void setQuantity(int quantity) {
+        this.quantity = quantity;
+    }
+}
Index: src/main/java/com/example/salonbella/service/order/OrderResponse.java
===================================================================
--- src/main/java/com/example/salonbella/service/order/OrderResponse.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/service/order/OrderResponse.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,73 @@
+package com.example.salonbella.service.order;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+
+public class OrderResponse {
+
+    private long id;
+    private String status;
+    private LocalDate localDate;
+    private double total;
+    private List<OrderDetail> orderDetails = new ArrayList<>();
+    private String name;
+    private String surname;
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public LocalDate getLocalDate() {
+        return localDate;
+    }
+
+    public void setLocalDate(LocalDate localDate) {
+        this.localDate = localDate;
+    }
+
+    public double getTotal() {
+        return total;
+    }
+
+    public void setTotal(double total) {
+        this.total = total;
+    }
+
+    public List<OrderDetail> getOrderDetails() {
+        return orderDetails;
+    }
+
+    public void setOrderDetails(List<OrderDetail> orderDetails) {
+        this.orderDetails = orderDetails;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getSurname() {
+        return surname;
+    }
+
+    public void setSurname(String surname) {
+        this.surname = surname;
+    }
+}
+
Index: src/main/java/com/example/salonbella/service/order/OrderService.java
===================================================================
--- src/main/java/com/example/salonbella/service/order/OrderService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
+++ src/main/java/com/example/salonbella/service/order/OrderService.java	(revision e9b70f6947737fd8ec8c5599c2b85b09f0ce2276)
@@ -0,0 +1,157 @@
+package com.example.salonbella.service.order;
+
+import com.example.salonbella.controller.cart.ShoppingCart;
+import com.example.salonbella.entity.CartDetailEntity;
+import com.example.salonbella.entity.OrderEntity;
+import com.example.salonbella.entity.UserEntity;
+import com.example.salonbella.repository.OrderRepository;
+import com.example.salonbella.repository.ProductRepository;
+import com.example.salonbella.repository.UserRepository;
+import com.twilio.Twilio;
+import com.twilio.rest.api.v2010.account.Message;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.stereotype.Service;
+
+import javax.transaction.Transactional;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+@Service
+public class OrderService {
+
+    private final UserRepository userRepository;
+    private final ProductRepository productRepository;
+    private final OrderRepository orderRepository;
+    private static final String ACCOUNT_SID = "AC773ecbd8592a5cad8169cd06c3ce6e13";
+    private static final String AUTH_TOKEN = "51247e92a82c11f5a929c4e3ae6c2953";
+
+    @Autowired
+    public OrderService(UserRepository userRepository, ProductRepository productRepository, OrderRepository orderRepository) {
+        this.userRepository = userRepository;
+        this.productRepository = productRepository;
+        this.orderRepository = orderRepository;
+    }
+
+    public void saveOrder(ShoppingCart shoppingCart) {
+        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
+        String username = auth.getName();
+        UserEntity user = userRepository.findByUsername(username);
+        OrderEntity orderEntity = new OrderEntity();
+        orderEntity.setCartDetails(shoppingCart.getCartDetails());
+        orderEntity.setUser(user);
+        orderEntity.setLocalDate(LocalDate.now());
+        orderEntity.setTotal(calculateTotalPrice(shoppingCart));
+        orderEntity.setStatus("PENDING");
+        orderRepository.save(orderEntity);
+    }
+
+    public void changeQuantities(Map<String, String> allParams, ShoppingCart shoppingCart) {
+        for (String id : allParams.keySet()) {
+            for (CartDetailEntity c : shoppingCart.getCartDetails()) {
+                if (c.getId() == Long.parseLong(id)) {
+                    c.setQuantity(Integer.parseInt(allParams.get(id)));
+                    break;
+                }
+            }
+        }
+    }
+
+
+    public double calculateTotalPrice(ShoppingCart shoppingCart) {
+        double total = 0.0;
+        for (CartDetailEntity c : shoppingCart.getCartDetails()) {
+            total += (c.getQuantity() * productRepository.findById(c.getId()).get().getPrice());
+        }
+        return total;
+    }
+
+    public void clearCart(ShoppingCart shoppingCart) {
+        for (int i = 0; i < shoppingCart.getCartDetails().size(); i++) {
+            shoppingCart.getCartDetails().remove(i);
+            i--;
+        }
+    }
+
+    public List<OrderEntity> getUserOrders() {
+        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
+        String username = auth.getName();
+        return makeOrderDetails(username);
+    }
+
+    public List<OrderEntity> makeOrderDetails(String username) {
+        UserEntity user = userRepository.findByUsername(username);
+        List<OrderEntity> orderEntityList = orderRepository.findAllByUser(user);
+        for (OrderEntity order : orderEntityList) {
+            for (CartDetailEntity cartDetailEntity : order.getCartDetails()) {
+                String name = productRepository.findById(cartDetailEntity.getId()).get().getName();
+                int quantity = cartDetailEntity.getQuantity();
+                order.getOrderDetails().add(new OrderDetail(name, quantity));
+            }
+        }
+        return orderEntityList;
+    }
+
+    @Transactional
+    public void cancelOrder(Long id) {
+        Optional<OrderEntity> orderEntity = orderRepository.findById(id);
+        if (orderEntity.isPresent()) {
+            if (getUser().getId().equals(orderEntity.get().getUser().getId())) {
+                orderRepository.delete(orderEntity.get());
+            }
+        }
+    }
+
+    @Transactional
+    public void cancelOrderAdmin(Long id) {
+        Optional<OrderEntity> orderEntity = orderRepository.findById(id);
+        orderEntity.ifPresent(orderRepository::delete);
+    }
+
+    @Transactional
+    public void changeOrderStatus(Long id) {
+        Optional<OrderEntity> orderEntity = orderRepository.findById(id);
+        orderEntity.ifPresent(entity -> entity.setStatus("AWAITING PICKUP"));
+//        Twilio.init(ACCOUNT_SID,AUTH_TOKEN);
+//        Message message = Message.creator(
+//                        new com.twilio.type.PhoneNumber(getUser().getNumber().replaceFirst("0","+389")),
+//                        new com.twilio.type.PhoneNumber("+1 815 567 9673"),
+//                        "YOUR ORDER IS HERE")
+//                .create();
+    }
+
+    public UserEntity getUser() {
+        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
+        String username = auth.getName();
+        UserEntity user = userRepository.findByUsername(username);
+        return user;
+    }
+
+    public List<OrderResponse> getAllOrders() {
+        List<OrderResponse> orderResponses = new ArrayList<>();
+        List<OrderEntity> orderEntities = orderRepository.findAll();
+        for (OrderEntity order : orderEntities) {
+            for (CartDetailEntity cartDetailEntity : order.getCartDetails()) {
+                String name = productRepository.findById(cartDetailEntity.getId()).get().getName();
+                int quantity = cartDetailEntity.getQuantity();
+                order.getOrderDetails().add(new OrderDetail(name, quantity));
+            }
+            OrderResponse orderResponse = new OrderResponse();
+            orderResponse.setId(order.getId());
+            orderResponse.setOrderDetails(order.getOrderDetails());
+            orderResponse.setLocalDate(order.getLocalDate());
+            orderResponse.setStatus(order.getStatus());
+            orderResponse.setTotal(order.getTotal());
+            orderResponse.setName(order.getUser().getName());
+            orderResponse.setSurname(order.getUser().getSurname());
+            orderResponses.add(orderResponse);
+        }
+        return orderResponses;
+    }
+
+}
+
