mirror of
https://github.com/Nezumi-2711/spring-boot-ecommerce.git
synced 2026-09-22 13:48:34 +00:00
Save the order to database - Backend
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.luv2code.ecommerce.controller;
|
||||
|
||||
import com.luv2code.ecommerce.dto.Purchase;
|
||||
import com.luv2code.ecommerce.dto.PurchaseResponse;
|
||||
import com.luv2code.ecommerce.service.CheckoutService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@CrossOrigin("http://localhost:4200")
|
||||
@RestController
|
||||
@RequestMapping("/api/checkout")
|
||||
public class CheckoutController {
|
||||
|
||||
private CheckoutService checkoutService;
|
||||
|
||||
public CheckoutController(CheckoutService checkoutService){
|
||||
this.checkoutService = checkoutService;
|
||||
}
|
||||
|
||||
@PostMapping("/purchase")
|
||||
public PurchaseResponse placeOrder(@RequestBody Purchase purchase) {
|
||||
PurchaseResponse purchaseResponse = checkoutService.placeOrder(purchase);
|
||||
|
||||
return purchaseResponse;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.luv2code.ecommerce.dao;
|
||||
|
||||
import com.luv2code.ecommerce.entity.Customer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.luv2code.ecommerce.dto;
|
||||
|
||||
import com.luv2code.ecommerce.entity.Address;
|
||||
import com.luv2code.ecommerce.entity.Customer;
|
||||
import com.luv2code.ecommerce.entity.Order;
|
||||
import com.luv2code.ecommerce.entity.OrderItem;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
public class Purchase {
|
||||
|
||||
private Customer customer;
|
||||
private Address shippingAddress;
|
||||
private Address billingAddress;
|
||||
private Order order;
|
||||
private Set<OrderItem> orderItems;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.luv2code.ecommerce.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PurchaseResponse {
|
||||
|
||||
private final String orderTrackingNumber;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.luv2code.ecommerce.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "address")
|
||||
@Getter
|
||||
@Setter
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "street")
|
||||
private String street;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@Column(name = "state")
|
||||
private String state;
|
||||
|
||||
@Column(name = "country")
|
||||
private String country;
|
||||
|
||||
@Column(name = "zip_code")
|
||||
private String zipCode;
|
||||
|
||||
@OneToOne
|
||||
@PrimaryKeyJoinColumn
|
||||
private Order order;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.luv2code.ecommerce.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "customer")
|
||||
@Getter
|
||||
@Setter
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "first_name")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name")
|
||||
private String lastName;
|
||||
|
||||
@Column(name = "email")
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
|
||||
private Set<Order> orders = new HashSet<>();
|
||||
|
||||
public void add(Order order){
|
||||
if(order != null) {
|
||||
if(orders == null){
|
||||
orders = new HashSet<>();
|
||||
}
|
||||
|
||||
orders.add(order);
|
||||
order.setCustomer(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.luv2code.ecommerce.entity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name="orders")
|
||||
@Getter
|
||||
@Setter
|
||||
public class Order {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
private Long id;
|
||||
|
||||
@Column(name="order_tracking_number")
|
||||
private String orderTrackingNumber;
|
||||
|
||||
@Column(name="total_quantity")
|
||||
private int totalQuantity;
|
||||
|
||||
@Column(name="total_price")
|
||||
private BigDecimal totalPrice;
|
||||
|
||||
@Column(name="status")
|
||||
private String status;
|
||||
|
||||
@Column(name="date_created")
|
||||
@CreationTimestamp
|
||||
private Date dateCreated;
|
||||
|
||||
@Column(name="last_updated")
|
||||
@UpdateTimestamp
|
||||
private Date lastUpdated;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order")
|
||||
private Set<OrderItem> orderItems = new HashSet<>();
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "customer_id")
|
||||
private Customer customer;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "shipping_address_id", referencedColumnName = "id")
|
||||
private Address shippingAddress;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "billing_address_id", referencedColumnName = "id")
|
||||
private Address billingAddress;
|
||||
|
||||
public void add(OrderItem item) {
|
||||
|
||||
if (item != null) {
|
||||
if (orderItems == null) {
|
||||
orderItems = new HashSet<>();
|
||||
}
|
||||
|
||||
orderItems.add(item);
|
||||
item.setOrder(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.luv2code.ecommerce.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
@Table(name="order_item")
|
||||
@Getter
|
||||
@Setter
|
||||
public class OrderItem {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
private Long id;
|
||||
|
||||
@Column(name="image_url")
|
||||
private String imageUrl;
|
||||
|
||||
@Column(name="unit_price")
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
@Column(name="quantity")
|
||||
private int quantity;
|
||||
|
||||
@Column(name="product_id")
|
||||
private Long productId;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.luv2code.ecommerce.service;
|
||||
|
||||
import com.luv2code.ecommerce.dto.Purchase;
|
||||
import com.luv2code.ecommerce.dto.PurchaseResponse;
|
||||
|
||||
public interface CheckoutService {
|
||||
|
||||
PurchaseResponse placeOrder(Purchase purchase);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.luv2code.ecommerce.service;
|
||||
|
||||
import com.luv2code.ecommerce.dao.CustomerRepository;
|
||||
import com.luv2code.ecommerce.dto.Purchase;
|
||||
import com.luv2code.ecommerce.dto.PurchaseResponse;
|
||||
import com.luv2code.ecommerce.entity.Customer;
|
||||
import com.luv2code.ecommerce.entity.Order;
|
||||
import com.luv2code.ecommerce.entity.OrderItem;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class CheckoutServiceImpl implements CheckoutService{
|
||||
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
public CheckoutServiceImpl(CustomerRepository customerRepository){
|
||||
this.customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public PurchaseResponse placeOrder(Purchase purchase) {
|
||||
|
||||
//Retrieve the order info from dto
|
||||
Order order = purchase.getOrder();
|
||||
|
||||
//Generate tracking number
|
||||
String orderTrackingNumber = generateOrderTrackingNumber();
|
||||
order.setOrderTrackingNumber(orderTrackingNumber);
|
||||
|
||||
//Populate order with orderItems
|
||||
Set<OrderItem> orderItems = purchase.getOrderItems();
|
||||
orderItems.forEach(item -> order.add(item));
|
||||
|
||||
//Populate order with billingAddress and shippingAddress
|
||||
order.setBillingAddress(purchase.getBillingAddress());
|
||||
order.setShippingAddress(purchase.getShippingAddress());
|
||||
|
||||
//Populate customer with order
|
||||
Customer customer = purchase.getCustomer();
|
||||
customer.add(order);
|
||||
|
||||
//Save to the database
|
||||
customerRepository.save(customer);
|
||||
|
||||
//return a response
|
||||
return new PurchaseResponse(orderTrackingNumber);
|
||||
}
|
||||
|
||||
private String generateOrderTrackingNumber() {
|
||||
//Generate a random UUID number (UUID version - 4)
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user