Billing System using Spring Boot walkthrough of the Registration flow

Billing System using Spring Boot walkthrough of the Registration flow
Billing System

Welcome back friends, in this story I will be going to explain what is the flow of User registration in our Billing System. It has the following three flows in the Registration flow.
Employee Registration
Vendor Registration
Customer Registration

Employee Registration
In UserPrivateController.java I have defined the following Rest endpoint.

@PostMapping(“/employees”) // http://localhost:9091/api/public/users/employees
public ResponseEntity<ResponseMessage<?>> saveEmployee(@Valid @RequestBody EmployeeDTO requestBody) throws Exception {

requestBody.setType(UserType.EMPLOYEE.name());
userValidator.validate(requestBody);
ResponseMessage responseMessage = registrationService.doRegistration(requestBody);
return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}
This method calls the validate method which is my custom validation class. This method accepts the EmployeeDTO as Request Body. The validate method do all mandatory fields validation from the UserDTO object which defined in the EmployeeDTO class. If there is no validation, it invokes the doRegistration method by passing EmployeeDTO as the request body. if the Type variable is “EMPLOYEE”, it is doing the following task and save the save employee info in the DB.
For further reading please click here.

1.) Converting EmployeeDTO to Employee object using MapStruct.
2.) It set the default role name to the Employee.
3.) Generates the Unique Employee code using “CodeGenerator.java” and set it to the employeeCode attribute.
Once the above task is done successfully, it stores the employee information to the user, employee, and address table.
Vendor Registration
The vendor registration is very similar to Employee registration. The Vendor Registration implemented in the following Rest Service method.

@PostMapping(“/vendors”)
public ResponseEntity<ResponseMessage<?>> saveVendor(@Valid @RequestBody VendorDTO requestBody) throws Exception {
requestBody.setType(UserType.VENDOR.name());
userValidator.validate(requestBody);
ResponseMessage responseMessage = registrationService.doRegistration(requestBody);
return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}
As usual, it validates all necessary validation from VendorDTO using UserValidator .validate() method. This method accepts the BaseDTO as a parameter, Here I am passing VendorDTO reference to BaseDTO because BaseDTO is the parent of the Vendor. if the validation is successful, this Service method is calling do Registration method from the Registration Service class. The doRegistration method based on, user type, it does the respective registration. in our case, it will call the following section.
else if (type.equalsIgnoreCase(UserType.VENDOR.name())) {
Vendor vendor = VendorMapper.INSTANCE.dtoToVendor((VendorDTO)baseDTO);
vendor.getUser().setRoles(roleList);
if(vendor.getId() == null) {
vendor.setVendorCode(codeGenerator.newCode(CodeType.VENDOR_CODE));
}
// vendor.getUser().setPassword(passwordEncoder.encode(vendor.getUser().getPassword()));
vendor.getUser().setPassword(vendor.getUser().getPassword());

vendor.getUser().getAddressList().get(0).setUser(vendor.getUser());
BaseObject newObject = billingBaseService.save(vendor);
VendorDTO vendorDTO = VendorMapper.INSTANCE.vendorToDTO((Vendor) newObject);
responseMessage = ResponseMessage.withResponseData(vendorDTO, “Vendor Created Successfully”, “message”);
}
This block of code does the following three tasks
1. Converting VendorDTO to Vendor object using MapStruct.
2. It set the default role name to the Employee. Generates the Unique
3. Vendor code using “CodeGenerator.java” and set it to the vendorCode attribute.
Once the above task is done successfully, it stores the Vendor information to the user, Vendor, and address table.
Customer Registration
The customer registration is different from Employee and Vendor registration. We don’t have to fill up all data while doing the registration because, in the Supermarket or any other small retailer shop, they will ask only for the mobile number. So, I have made all fields optional fields. The following is the Service endpoint for registering new Customers.
@PostMapping(“/customers”) // http://localhost:9091/api/public/users/customers
public ResponseEntity<ResponseMessage<?>> saveCustomer(@Valid @RequestBody CustomerDTO requestBody) throws Exception {

requestBody.setType(UserType.CUSTOMER.name());
userValidator.validate(requestBody);
ResponseMessage responseMessage = registrationService.doRegistration(requestBody);
return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}
Before saving the Customer information, validating the customer attribute using the UserService.validate method and, the validate method validate the only mobile number, if the Mobile Number is valid, it allows us to save the Customer details. Following is the code snippet of the Customer Registration flow.
else if (type.equalsIgnoreCase(UserType.CUSTOMER.name())) {
Customer customer = CustomerMapper.INSTANCE.dtoToCustomer((CustomerDTO) baseDTO);
customer.getUser().setRoles(roleList);
if(customer.getId() == null) {
customer.setCustomerCode(codeGenerator.newCode(CodeType.CUSTOMER_CODE));
}
customer.getUser().getAddressList().get(0).setUser(customer.getUser());
// customer.getUser().setPassword(passwordEncoder.encode(customer.getUser().getPassword()));
customer.getUser().setPassword(customer.getUser().getPassword());

BaseObject newObject = billingBaseService.save(customer);
CustomerDTO customerDto = CustomerMapper.INSTANCE.customerToDTO((Customer) newObject);
responseMessage = ResponseMessage.withResponseData(customerDto, “Customer Created Successfully”, “message”);
}

The above code does the following three tasks.
1) Converting CustomerDTO to Customer object using MapStruct
2) It set the default role name to the Customer. Generates the Unique
3) Customer Code using “CodeGenerator.java” and set it to the CustomerCode attribute.

Once the above task is done successfully, it stores the Customer information to the user, Customer, and address table.

 Please watch this full YouTube video for better understanding
https://youtu.be/-5RlaJ1n5iU
You can download the code from the following link

https://github.com/sureshstalin/billingsystem
 


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 who is 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