Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3886

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:52:11+00:00 2024-11-26T06:52:11+00:00

WishList Feature for eCommerce App using Java and Spring Boot

  • 61k

We will create a back-end of a very important feature in every e-Commerce site — Wishlist, using Java and Spring Boot
Alt Text
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).

Youtube Discussion

Discussion about Wishlist feature of E-commerce app

Live Demo

You can test the API at the following swagger link. You will find the wishlist API in wish-list-controller section.
Swagger UI
You can find the complete code at Github.

Pre-requisites

  1. Knowledge of Java, OOP & Spring Boot Framework
  2. Java Development Kit (JDK)
  3. IntelliJ IDEA Ultimate — open-source (Recommended)
  4. MySQL/MariaDB Database
  5. 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:
    Alt Text
    Project Structure of the API

    We will now describe the following directories:-

    1. controller— contains the controllers for various API endpoints
    2. 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.
    3. model — contains the data models (and entities)
    4. repository — contains the methods for CRUD operations in corresponding tables of the database
    5. service — contains the class files with @service annotations. These class files are used to write business logic in a different layer, separated from @RestController class 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:-
    6. Adding to wishlist
    7. 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. Alt Text 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 Alt Text 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 Alt Text ## Table Design Now, let’s discuss the table design. We had already created the ecommerce database 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,
    8. id is the primary key and will be auto-generated
    9. user_id — stores userId
    10. product_id — stores the product id
    11. created_date — stores the data & time at which the entry was created The schema of the database looks like the following:-

      Ideally, each user’s wishlist should have a particular product only once. But for simplicity, we are not considering such cases. ## Let’s Code We will now begin to write code. ## Model (Entity) Let’s begin with writing the code for the Model class of each entry in the wishlist table. If you are familiar with Spring Boot or any other MVC framework, you would know that Model class is used to store each entry of the table. In Spring Boot, we use Annotations to map the columns of the table with the class members. To create the model class, create a new class inside the Model directory. We will call this class — WishList.

    12. 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.
    13. We will also create one class object of Product class. This object will store all the details of the product like name, price, description etc.
    14. Also, note that the column created_date should be filled with the current date and time. For this, we will use the java.util.Date class. Following is the complete code of WishList.java

      ## Repository It is time to create the repository interface for the wishlist table. Create a new file called WishListRepository.java inside the Repository directory. If you are familiar with Spring Boot, you would know that Repository the interface contains methods to fetch data from the table. 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 WishListRepository.

    15. Extending JPARepository will automatically create and implement methods for the basic CRUD operations.
    16. 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 WishListRepository.java

      ## Service Now, let's implement the Service class to interact with the wishlist table. In the WishListRepository interface, we defined the methods to interact with the database. In the Service class, 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 createWishlist() and readWishlist() . Inside these methods, we will call the methods defined in the WishListRepository interface. Following is the complete code of WishListService.java

      ## Controller Now, Let’s begin with writing code for the controller. If you are familiar with Spring Boot or any other MVC framework, you would already know that controller defines the endpoints of the API. Create a new file inside the Controller directory with the name WishlistController.java . Since we have two endpoints, we will create two methods in the WishlistController class.

    17. To use the WishListService and AuthenticationService , we will create the two objects of respective types. We have already created the WishListService in the previous section and is used to interact with the database. We created AuthenticationService in a previous tutorial and is used to fetch user id of the corresponding token
    18. We will create one method with @GetMapping for the GET request and @PostMapping for the POST request.
    19. Now, each table entry contains the created_date value also. We do not want to send that with the response. So, here comes the use of DTO. Using the getDtoFromProduct() method of the ProductService Class, we will store only that information about each product which we want to send as response. Hence, we create a list of ProductDto object and store only the required details. We will send this list in the response. The following is the complete code of WishlistController.java

      ## Congratulations!!! Congratulations, we have now added the wishlist feature to our backend. ## Suggested PRs If you wish to contribute to our eCommerce-backend, you clone this Github repository and work on the following features related to the wishlist

    20. 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

      Nil Madhab

      Let’s Develop an E-Commerce Application From Scratch Using Java and Spring | by Nil Madhab | Javarevisited | Medium

      Nil Madhab ・ Jun 10, 2022 ・

      Medium Logo Medium

      Nil Madhab

      Let’s Build Signup, SignIn, and Role-Based Access in Our E-Commerce App | by Nil Madhab | Javarevisited | Medium

      Nil Madhab ・ Apr 16, 2021 ・

      Medium Logo Medium

      Nil Madhab

      Add Products for Ecommerce | Medium

      Nil Madhab ・ Apr 16, 2021 ・

      Medium Logo Medium

javaprogrammingsqlwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.