Billing System using Spring Boot. Code walkthrough and Tax Table CRUD Operation

Billing System using Spring Boot. Code walkthrough and Tax Table CRUD Operation

Welcome back friends, In this story, we will going to learn about the implementation of Tax, offer, and Category CRUD Operation. CRUD nothing but Create, Read, Update, and Delete operation. We will be going to perform these operations against these tables. Tax offer and category tables are important tables in the billing System. Let explore the table and its CRUD operation.

Tax Table
The tax table contains the HSN Code for various types of Products. each code mapped with Tax percentage. Following is the Table relation between Product and Tax table
Billing System using Spring Boot

The tax_id is associated with each product. Each tax_id is mapped with one HSN Code. The HSN code is having its respective GST percentage. Through this relation, we can easily find out what is the GST of each Product in our System.

I have created the following class for manipulating Tax CRUD operation.
TaxController.java
TaxService.java
TaxRepository.java
Tax.java
Taxinfo.java

TaxRepository.java
This is an interface class and currently, it doesn’t have any method. I can use its inbuild method for CRUD Operation.

Following is code implementation.

/*
 * This method for adding new Tax Records and updating existing Tax Records
 */
public ResponseMessage save(TaxInfo taxInfo) {
    Tax tax = TaxMapper.INSTANCE.taxInfoToTax(taxInfo);
    Tax newTax = taxRepository.save(tax);
    TaxInfo newTaxInfoResponse = TaxMapper.INSTANCE.taxToTaxInfo(newTax);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(newTaxInfoResponse,"Tax saved successfully",Constants.INFO_TYPE);
    return responseMessage;
}

In the following line, I am converting the TaxInfo object to Tax Object. If you do not understand the following line
Tax tax = TaxMapper.INSTANCE.taxInfoToTax(taxInfo);
Please check this video link for how to work with the MapStruct framework for mapping two objects or copying one object’s value to another object.
https://youtu.be/LnSwxREmzXk

Following method for view particular Tax Records

@Override
public ResponseMessage findResourceById(String id) throws Exception {
    Tax tax = taxRepository.findById(Long.parseLong(id)).orElse(null);
    TaxInfo newTaxInfoResponse = TaxMapper.INSTANCE.taxToTaxInfo(tax);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(newTaxInfoResponse,"Tax saved successfully",Constants.INFO_TYPE);
    return responseMessage;
}

Following method for view all Tax records. While iterating each record I am converting the record to my TaxInfo object and adding those into the list object. Finally, I am returning that list object as ResponseMessage to the Controller class

@Override
public ResponseMessage findAll() throws Exception {
    List<Tax> taxes = taxRepository.findAll();
    List<TaxInfo> taxInfos = new ArrayList<>();
    for (Tax tax: taxes) {
        TaxInfo taxInfo = TaxMapper.INSTANCE.taxToTaxInfo(tax);
        taxInfos.add(taxInfo);
    }
    ResponseMessage responseMessage = ResponseMessage.withResponseData(taxInfos,Constants.SUCCESS_STATUS, Constants.INFO_TYPE);
    return responseMessage;
}

TaxController.java
In the TaxController class, I have Autowired “TaxService” class for performing CRUD operation against TAX Table. 

The following end-point method is an implementation for adding or modifying Tax records. To access this method we have to use this endpoint URI “api/private/taxes” (Ex: http://localhost:9091/api/private/taxes)
This endpoint is “PostMapping” to save Tax records. After creating records it returns 201 (CREATED) status because we are creating new Records. Actually, we have to create a separate endpoint for updating the existing record using @PutMapping which is a web services standard. As of now, I am using the same endpoint method for creating/update (A bit I am deviating from Web service standards)

@PostMapping
public ResponseEntity<ResponseMessage<?>> save(@Valid @RequestBody TaxInfo taxInfo) {
    ResponseMessage responseMessage  = taxService.save(taxInfo);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}

The Following endpoint method for view particular Tax Records. it has ID as path variable where we need to pass tax id which you want to view. Passing that path variable in “findResourceById() method, it will return respective tax records from the Database

@GetMapping("/{id}")
public ResponseEntity<ResponseMessage<?>> get(@PathVariable String id) throws Exception {
    ResponseMessage responseMessage = taxService.findResourceById(id);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}

The following endpoint method is for viewing all Tax records. I am calling “findAll” method from the service class which is calling the “findAll” method from the repository interface class. it returns all tax records.

@GetMapping()
public ResponseEntity<ResponseMessage<?>> getAll() throws Exception {
    ResponseMessage responseMessage = taxService.findAll();
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}

  


Please follow our ITGARDEN YouTube channel for Billing System Project development work using Spring Boot. The Project source code absolutely free. This project is very good for those who are looking for real-time project experience in Spring Boot. To understand the Billing System Project code, watch all Billing System-related YouTube videos from this link. Click Here
Our YouTube Channel
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA?view_as=subscriber
Please follow me on Medium.com for all billing system related stories and Java-based technical Articles
https://suresh-stalin.medium.com/
Note: To find all billing System-related stories find “billing system itgarden” in medium.com

Close Menu
×
×

Cart