We will create a back-end of a very important feature in every e-Commerce site — Wishlist, using Java and Spring Boot
A Wishlist is an eCommerce feature that allows shoppers to create personalized collections of products they want to buy and save them in their user account. It is a must-have feature for eCommerce applications.
We will first develop the back-end API using Java & Spring Boot (in this tutorial). After the API has been created, we will use that API in our Vue.Js front-end and Android front-end (in other tutorials).
- Knowledge of Java, OOP & Spring Boot Framework
- Java Development Kit (JDK)
- IntelliJ IDEA Ultimate — open-source (Recommended)
- MySQL/MariaDB Database
- A good browser (Chrome — recommended)
This tutorial is part of our series on Back-end Development with Java. We will extend the code which we developed in the previous tutorials in this series. So, if you have any doubt regarding anything that we developed earlier, you can read about it in the corresponding tutorial in the series.
## Project Structure
If you have not read the previous tutorials in the back-end series, don’t worry. This section is specifically for you. As we will use the project structure that we created in the previous tutorials, we intend to describe the structure here before we begin working on the Wishlist feature. This will help you in understanding the code in a better way.
Following is the project structure:
We will now describe the following directories:-
controller— contains the controllers for various API endpoints
dto— contains the Data Transfer Objects (DTO) for our back-end. In client-server projects, data is often structured differently. There are some details in the database that we do not want to send as a response to the API calls. So, the server stores its information in a database-friendly way. While retrieving that information from the database, it can use DTOs to filter this information and then send it to the client. Don’t worry if you could not understand DTOs. You will understand it when we implement Wishlist DTO in this tutorial.
model— contains the data models (and entities)
repository— contains the methods for CRUD operations in corresponding tables of the database
service— contains the class files with
@serviceannotations. These class files are used to write business logic in a different layer, separated from
@RestControllerclass files. Business logic or domain logic is that part of the program which encodes the real-world business rules that determine how data can be created, stored, and changed inside the database. ## API Design Before we begin to code, we must spend some time to think about the API design and the database design. Let’s begin with the API design. Currently, we need only two API endpoints:-
- Adding to wishlist
- Getting wishlist
Also, we had already added the token-based authorization in our eCommerce backend. So, instead of user id, we will pass the token to every endpoint of the API. Hence, we decide to have the following endpoints.
Also, in the body of the POST method, we will have to send the id of the product so that the given product can be added to the corresponding user’s wishlist. Hence, the body of the POST request should look like the following
Now, the response of the POST request should send the list of all products in the wishlist with the necessary details. Hence, the response should look like the following
## Table Design
Now, let’s discuss the table design. We had already created the
ecommercedatabase in previous tutorials. In this database, we will create a new table called
wishlist.We will keep the design simple. The database should have three columns —
id, user_id, product_id,created_date. Here,
idis the primary key and will be auto-generated
user_id— stores userId
product_id— stores the product id
created_date— stores the data & time at which the entry was created The schema of the database looks like the following:-
Modeldirectory. We will call this class —
- We have already described the schema of the table. Using the schema, we will create the class variables of the model class representing each column of the database.
- We will also create one class object of
Productclass. This object will store all the details of the product like name, price, description etc.
- Also, note that the column
created_dateshould be filled with the current date and time. For this, we will use the
java.util.Dateclass. Following is the complete code of
repository interfacefor the
wishlisttable. Create a new file called
WishListRepository.javainside the Repository directory. If you are familiar with Spring Boot, you would know that
Repositorythe interface contains methods to fetch data from the table. Creating CRUD methods manually means writing a lot of boilerplate code unless you let the
JPARepositoryinterface carry about routine implementations for you. So, we will extend the
JPARepositoryand create the interface
JPARepositorywill automatically create and implement methods for the basic CRUD operations.
- We will define a method
findAllByUserIdOrderByCreatedDateDesc()to fetch the wishlist of a user and order the list by created the date of each entry in the wishlist. The implementation of this method will be managed automatically by the
JPARepository. Following is the complete code of
Serviceclass to interact with the
wishlisttable. In the
WishListRepositoryinterface, we defined the methods to interact with the database. In the
Serviceclass, we will call these methods and implement the so-called business logic. To keep things simple, we do not have any business logic, i.e. business constraints or rules defined. So, we will simply create two methods
readWishlist(). Inside these methods, we will call the methods defined in the WishListRepository interface. Following is the complete code of
WishlistController.java. Since we have two endpoints, we will create two methods in the
- To use the
AuthenticationService, we will create the two objects of respective types. We have already created the
WishListServicein the previous section and is used to interact with the database. We created
AuthenticationServicein a previous tutorial and is used to fetch user id of the corresponding token
- We will create one method with
@GetMappingfor the GET request and
@PostMappingfor the POST request.
- Now, each table entry contains the
created_datevalue also. We do not want to send that with the response. So, here comes the use of DTO. Using the
getDtoFromProduct()method of the
ProductServiceClass, we will store only that information about each product which we want to send as response. Hence, we create a list of
ProductDtoobject and store only the required details. We will send this list in the response. The following is the complete code of
- Create an API end-point for Deleting a Product from Wishlist After you have implemented the feature, send us a PR. We will review and merge it into our master branch ## Reference