Skip to content
Snippets Groups Projects
Commit 8887f604 authored by dmz39's avatar dmz39
Browse files

Persisting orders to the database

parent a29d0dad
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,10 @@ import java.util.List;
import javax.servlet.http.HttpSession;
import edu.drexel.TrainDemo.Utils;
import edu.drexel.TrainDemo.models.sales.Order;
import edu.drexel.TrainDemo.models.users.Address;
import edu.drexel.TrainDemo.services.sales.OrderService;
import edu.drexel.TrainDemo.services.users.UserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -12,9 +15,7 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import edu.drexel.TrainDemo.models.sales.Cart;
import edu.drexel.TrainDemo.models.sales.ticket.Ticket;
......@@ -27,6 +28,9 @@ public class CartController {
@Autowired
UserService userService;
@Autowired
OrderService orderService;
public CartController(Logger logger){
this.logger = logger;
}
......@@ -96,4 +100,14 @@ public class CartController {
return "cart/checkout";
}
@RequestMapping("/cart/complete")
@ResponseBody
public String completeOrder(@AuthenticationPrincipal OAuth2User principal, HttpSession session, @RequestParam Long shippingId, @RequestParam Long billingId, @RequestParam Long paymentId) {
System.out.println("Completing order: " + shippingId + " | " + billingId + " | " + paymentId);
Cart cart = getCart(session);
Order finalOrder = new Order(cart.getTotal(), this.userService.getUser(principal), billingId, shippingId, paymentId);
this.orderService.addOrder(finalOrder);
return ("Order " + finalOrder.getId() + " Was Successful!");
}
}
\ No newline at end of file
package edu.drexel.TrainDemo.models.sales;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import edu.drexel.TrainDemo.models.sales.ticket.Ticket;
import edu.drexel.TrainDemo.models.users.UserEntity;
@Entity
......@@ -22,23 +18,25 @@ public class Order {
@Column(name = "user_id")
private Long userId;
/*
@Column(name = "billing_id")
private Long billing_id;
private Long billingId;
@Column(name = "mailing_id")
private Long mailing_id;
@Column(name = "shipping_id")
private Long shippingId;
@Column(name = "payment_id")
private Long payment_id;
*/
private Long paymentId;
protected Order() {
}
public Order(double price, UserEntity user){
public Order(double price, UserEntity user, Long shippingId, Long billingId, Long paymentId){
this.price = price;
this.userId = user.getId();
this.billingId = billingId;
this.shippingId = shippingId;
this.paymentId = paymentId;
}
public Long getId() {
......@@ -49,6 +47,19 @@ public class Order {
// this.id = id;
// }
public Long getBillingId() {
return billingId;
}
public Long getShippingId() {
return shippingId;
}
public Long getPaymentId() {
return paymentId;
}
public double getPrice() {
return price;
}
......
......
......@@ -11,5 +11,4 @@ public interface OrderService {
Order getOrder(Long orderid);
List<Order> getUserOrders(UserEntity userEntity);
List<Order> getAllOrders();
}
......@@ -19,7 +19,6 @@ public class OrderServiceImpl implements OrderService {
this.orderRepository.save(order);
}
public Order getOrder(Long orderid) {
return this.orderRepository.getOrderById(orderid);
}
......
......
......@@ -50,3 +50,7 @@ body {
margin-top: 10px;
margin-bottom: 10px;
}
.checkout-button {
float: right;
}
\ No newline at end of file
function completeOrder() {
var shippingVal = $('#shippingAddress').children("option:selected").val();
var shippingId = $('#'+shippingVal).html();
var billingVal = $('#billingAddress').children("option:selected").val();
var billingId = $('#'+billingVal).html();
$.post("/cart/complete", {
shippingId: shippingId,
billingId: billingId,
paymentId:0
}, function (returnstring) {
console.log(returnstring);
});
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<th:block th:replace="base::header"/>
<script type="text/javascript" src="/js/cart.js"></script>
</head>
<body>
<th:block th:replace="base::navbar"/>
......@@ -42,9 +43,10 @@
<br>
<div th:each="address,iter : ${billingAddresses}" class="container address">
<div class="row address-header">
<pre>Billing Info #</a><a th:text="${iter.index + 1}"></a>:</pre>
<pre><a>Billing Info #</a><a th:text="${iter.index + 1}"></a>:</pre>
</div>
<div th:id="'billing'+${iter.index + 1}" class="row" th:object="${address.getId()}">
<div class="row">
<a hidden th:id="'billing'+${iter.index + 1}" th:text="${address.getId()}"></a>
<p>
<a th:id="addrName" th:text="${address.getName()}"></a><br>
<a th:id="addrLine1" th:text="${address.getLine1()}"></a><br>
......@@ -68,9 +70,10 @@
<br>
<div th:each="address,iter : ${shippingAddresses}" class="container address">
<div class="row address-header">
<pre>Shipping Info #</a><a th:text="${iter.index + 1}"></a>:</pre>
<pre><a>Shipping Info #</a><a th:text="${iter.index + 1}"></a>:</pre>
</div>
<div th:id="'shipping'+${iter.index + 1}" class="row" th:object="${address.getId()}">
<div class="row">
<a hidden th:id="'shipping'+${iter.index + 1}" th:text="${address.getId()}"></a>
<p class="ticket-para">
<a th:id="addrName" th:text="${address.getName()}"></a><br>
<a th:id="addrLine1" th:text="${address.getLine1()}"></a><br>
......@@ -82,6 +85,9 @@
</div>
</div>
</div>
<div class="row spacer">
<a class="checkout-button"><button class="btn btn-primary checkout-button" th:type="button" th:onclick="completeOrder()">Complete Order</button></a>
</div>
</div>
</div>
</main>
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment