Add to Basket Operation - Part 2
Start course
2h 34m

In this course, we will learn the concepts of microservice and spring framework with a focus on Spring Boot.

Learning Objectives

  • Understanding Spring Boot

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will continue with add to basket operations. Open the EmployeeController. In previous videos, we've already designed our Add Basket page. Now, we have to implement this operation. First, I want to inject the Bill and Sold ProductService to the EmployeeController. Now, we need a new method with PostMapping. Path will be AddBasket. The name of the method will be AddBasket. Now, in this method, we need three parameters. The first is piece or sold product amount. I will use piece. The second one is ProductId, and last one is HttpServeletResponse. First, we have to getAllBills. Now, we have to check to see if there are any empty bills. Also we need an emptyBillId.

Now, let's check. If bill list's size == 0, this means we don't have any bill, so we have to create a bill and run the method again. Create a bill instance. Now, fill values: Total_ price is 0, The date will be a, Employee_confirmed will be emptyBills value, Admin_confirmed must be false. Save bill with save() method of billservice. Now, call the method again. Okay, done. Else. This means we have a bill but we don't have any emptyBill, so we need if for each loop. With this for each, we will get not confirmed bills ProductId, and we will make haveBillId variable to the true. We will use this in the addSoldProduct part but first we have to determine which products are already in the basket.

We need a Boolean value named alreadyinbasket. Now, getAllSoldProducts. Now, we will use this list in a for each loop. SoldProduct sp:soldProducts, is sp.isIn_basket. We must set the already In_basket variable to true. After that, we must break the loop. We must now update the stock amount for each SoldProduct operation. We can use an item that is already in the basket. If already in basket, this means we need to reduce the amount of products sold from Stock. First getProductById. Now, setStock. Now, save, else getProduct, setNewStock. Save. Done Now, we can add the SoldProduct to our database.

If haveEmptyBill else, we will create a new empty bill. Let's copy this area. Okay, done. Now, in the if condition, first we need a Boolean variable called alreadyeexist. This must be true. Now, getAllSoldProducts. Now, we can use a for loop for each item in the list. If sold product's ProductId = productId, and get it basket = true. This means we have to add piece of this product to our SoldProduct's piece. Save. Set alreadyexist false. If there is no item in the list, this means our alreadyexist value will be true. So, let's check. If alreadyexist, now we can add new SoldProduct.

First, create an instance from SoldProduct. Set ID. piece in basket true, setBillId with emptyBilId. Now save. Now, the last process is to redirect the page to the EmployeeProductList. With this, we can see the product stock amount. Done. Now, let's try. Login as Employee. Select this one. Add Basket. Now look, our stock has decreased. So, let's take a short break here. In the next video, we will continue with the Basket page and create the Bill page. So, I'll see you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.