Let’s add the Place Order feature to our E-commerce app using Spring Boot
In this tutorial, we will learn how to create an order and display the order history of the user
We are building an e-commerce app from scratch using Vue.js in the frontend and Java with Spring boot for the backend. You can check out the first front-end tutorial of this series here.
Let’s develop an Ecommerce Application from Scratch using Java and Spring
We are going to build an e-commerce application from scratch using Java, Spring backend, build web UI in Vue.js, and…
Placing the order is an important feature for an e-commerce app that allows users to order the products of their choice.
We will first develop the backend API using Spring Boot and in the next tutorial, we will use this API for the frontend part of our e-commerce website.
Refer the tutorial where we implemented the checkout feature here for the backend part
Preview of the API we will be building
placeOrder(/order/add) : This is a post method that is responsible for saving the order of the user.
getAllOrders(/order/): This is a get method that returns the list of all the orders of a particular user. Therefore it requires only the token of the user as a parameter.
The flow of the tutorial
Implement Service class (Business logic)
Implement Controller class (Building APIs)
Before we begin to code, we must invest some time thinking about the model/table design. So we require two tables which will be linked with each other. So in the existing
modelpackage, create two files
The first table/model (we name it as
order)will contain attributes such as
session-id (the payment session-id we generated), and the
User id we will be fetching from the
If you are wondering about the session id, please refer to this tutorial where we have explained the checkout session.
The second table/model (we name it as
orderitems) will actually contain the products that are present in a particular order. There we need to link these two tables. So the primary key i.e.
order table will be used as a foreign key in the
orderitems table. So the attributes of the
orderitems table will be
Product id we will be obtained from the
order table and
orderitems table has a relation type of one to many, i.e. one row(entry) in
order table is related to multiple rows(items of that particular order) in the
user tables have a relation type of many to one i.e. multiple orders (entries/rows) are related to a single user (row/entry), because a user may have multiple orders.
Order items model
ordertable we created just now and this
orderitems table has a relation type of many to one since multiple items (products) can be in relation with a particular order.
products table and the
orderitems table has a relation type of one to one since a row(item) in the
orderitems table is related to only one row(item) in the
Create two java files in the existing
- Firstly we create
repository interfacefor the
Creating CRUD methods manually means writing a lot of boilerplate code unless you let the
JPARepository interface carry about routine implementations for you. So, we will extend the
JPARepository and create the interface
JPARepositorywill automatically create and implement methods for the basic CRUD operations.
- We will define a method
findAllByUserIdOrderByCreatedDateDescto fetch the orders list of a user and then order the list by created the date of each entry in the
ordertable. The implementation of this method will be managed automatically by the
2. Similarly we create
repository interface for the
We do not declare any method here but instead, we will use one method in the service class later.
dto package create a package named
order and in which create 3 files
- We create
OrderDtofor as a dummy object for the Order model with two fields
2. Similarly we create
Currently, we won't be using this DTO but for future use or modifications, we keep this dto ready.
3. Now in the PlaceOrderDto we declare three fields
userId, and the
totalCostof the order. This dto will be used to save the order in the order table.
Now, let’s implement the
Service class to interact with the
orderitems table. In the
OrderRepositoryinterface, we defined the methods to interact with the database.
Service class, we will call these methods and implement the so-called business logic. We will create methods for adding the order, fetching all orders of a user. Inside these methods, we will call the methods defined in the
- First, let’s implement the
orderitemsservice class. In this class, we implement one method in order to save an item/product in a particular order.
2. Now let’s create the order service class which will contain the logic for placing the order and fetching the order details.
placeOrder: This method is used to set the PlaceOrderDto and it receives the id of the order from the
saveOrder method. This id is returned from the
saveOrder method because we need this id as an attribute in the
orderItems table. Also using the object of
cartService class we fetch the items in the cart and then save those in the
orderitems table using the method in the
orderItemsService class. And once the order is placed we delete the cart i.e. remove the items in the cart.
saveOrder: This method first creates the order object and then using the method of orderRepository it saves the order in the order table and returns the id which we used in the previous method.
listOrders : This method will take the user id as a parameter and then return a list of all the orders from the
order table corresponding to that user id.
Here we will build our APIs.
controller package creates a file
OrderController.java and annotate the class with RestController annotation. In this OrderController class, we will use the methods we defined in the
- So firstly we will implement the API for saving the order. Create a method placeOrder that accepts token and the session Id as parameters.
This method which throws an exception
- if the invalid user(not logged in) user tries to place the order
- if the user (valid) tries to place the order of the product which does not exist in the products table.
2. Since we added the order to the table, now we build an API to fetch the orders of the user. This method throws an exception if an invalid user tries to fetch the order history.
We have now successfully added the order feature to our e-commerce app using Spring Boot. In the next tutorial we will implement the front end part using these APIs using Vuejs, so stay tuned!