Code Walkthrough of Offer and Category Table

Code Walkthrough of Offer and Category Table

Welcome back friends, in this video, we will be going to explore Offer Table and Category Table. The user of the Offer table is, if you want to add a new Offer to the Product and if you want to update the existing product. We are using the offer Table in the billing System. Following is the structure of the Offer Table.

offer table code walkthrough

The offer relationship with the Product table. Using join table it has Many to Many relation because One can apply to multiple Product and Product can have multiple Offer so, I have given Many to Many relation. As part of this story, we are not dealing with storing Offer and Product Table data. Only we will be going to cover storing Offer information in Offer Table.
Following is the Offer Controller Java class where I have implemented CRUD Operation of Offer Table.

@Autowired
private OfferService offerService;
// This method save or update the offer.
@PostMapping
public ResponseEntity<ResponseMessage<?>> save(@Valid @RequestBody OfferInfo offerInfo) {
    ResponseMessage responseMessage = offerService.save(offerInfo);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}
// This method gets a particular offer by providing the ID
@GetMapping("/{id}")
public ResponseEntity<ResponseMessage<?>> get(@PathVariable String id) throws Exception{
    ResponseMessage responseMessage = offerService.findResourceById(id);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}
// This method return all Offer Data from Database
@GetMapping
public ResponseEntity<ResponseMessage<?>> geAll() throws Exception{
    ResponseMessage responseMessage = offerService.findAll();
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}

Following is an OffferService class where I Autowired in OfferController.java

// This repository for performing CRUD Operation against Offer Table
@Autowired
private OfferRepository offerRepository;
//This Class for generating a Unique Offer code for each row in the Offer Table
@Autowired
private CodeGenerator codeGenerator;
// This method for saving new or update existing offer if it is a new offer,
// It generates a new Offer Code otherwise just update the existing Offer.
// OfferInfo is Request Body coming from the client. I am converting the OfferInfo to
// Offer Entity object using MapStruct Framework. This method I cam calling Offer
// Controller class in save method

public ResponseMessage save(OfferInfo offerInfo) {

    Offer offer = OfferMapper.INSTANCE.offerInfoToOffer(offerInfo);
    if(StringUtils.isEmpty(offer.getOfferCode())) {
        String offerCode = codeGenerator.newCode(CodeType.OFFER_CODE);
        offer.setOfferCode(offerCode);
        offer.setStatus(STATUS.PENDING.name());
    }
    Offer newOffer = offerRepository.save(offer);
    OfferInfo newOfferInfo = OfferMapper.INSTANCE.offerToOfferInfo(newOffer);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(newOfferInfo, "Offer saved successfully", Constants.INFO_TYPE);
    return responseMessage;
}
// This method for returning a particular offer by providing an offer Id. It will
// return Offer entity object and I am converting Offer entity object as
// OfferInfo object using MapStruct framework then I am binding the OfferInfo
// Object with ResponseMessge
@Override
public ResponseMessage findResourceById(String id) throws Exception {
    Offer offer = offerRepository.findById(Long.parseLong(id)).orElse(null);
    OfferInfo offerInfo = OfferMapper.INSTANCE.offerToOfferInfo(offer);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(offerInfo, Constants.SUCCESS_STATUS, Constants.INFO_TYPE);
    return responseMessage;
}
// This method return all offers from the Database. Once we get all Offer
// Entity Object, I am converting those object as OfferInfo and adding into Array
// List. Finally, I am converting binding the Offer Info list with ResponseMessage
@Override
public ResponseMessage findAll() throws Exception {
    List<OfferInfo> offerInfos = new ArrayList<>();
    List<Offer> offers = offerRepository.findAll();
    for (Offer offer : offers) {
        OfferInfo offerInfo = OfferMapper.INSTANCE.offerToOfferInfo(offer);
        offerInfos.add(offerInfo);
    }
    ResponseMessage responseMessage = ResponseMessage.withResponseData(offerInfos, Constants.SUCCESS_STATUS, Constants.INFO_TYPE);
    return responseMessage;
}

Following class Repository class for Offer Entity Object. It doesn’t have any method. I am using the default method which is coming from JpaRepository for CRUD Operation.

public interface OfferRepository extends JpaRepository<Offer,Long> {
}

The CodeGenerator class for generating a new Offer code for Offer Table. This is a generic Component class to generate code for all Entity Object. To understand more about this class. Please read the following Article. or watch the following video.

The following article explains how the CodeGenerator class works.
https://pinepad.in/java/billing-system/billing-system-using-spring-boot-custom-id-generator-for-employee-customer-and-vendor/

The following video link explains how CodeGenerator class works
https://youtu.be/0Uoa2Wpnp-0

Now explore the Category Table
Category table is One to One relationship with Product able. Each Product associate with one Category. Following is the Structure for the Category and Product Table.
Billing System code walkthorug

 

I have created a Controller class called CategoryController.java which implements endpoints for CRUD operation against the Category Table. Following is the code implementation of the Category Controller.

// I have Autowired the CategoryService which used for performing CRUD Operation.
@Autowired
CategoryService categoryService;

// This save method endpoint for adding new Category or updating existing Category
// in the Category Table. CategoryInfo is a Request Boday which coming from the client
// which is having the request data to store the Category in the Category Table.
@PostMapping
public ResponseEntity<ResponseMessage<?>> save(@Valid @RequestBody CategoryInfo categoryInfo) {
    ResponseMessage responseMessage  = categoryService.save(categoryInfo);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
}

// This endpoint method return Categoryinfo object as a response by providing
// particular Category Id
@GetMapping("/{id}")
public ResponseEntity<ResponseMessage<?>> get(@PathVariable String id) throws Exception {
    ResponseMessage responseMessage = categoryService.findResourceById(id);
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}

// This endpoint method getAll return all Category Object form the DB
@GetMapping()
public ResponseEntity<ResponseMessage<?>> getAll() throws Exception {
    ResponseMessage responseMessage = categoryService.findAll();
    return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.OK);
}

Following is code implementation of the CategoryService method which uses the CategoryRepository to perform CRUD operation. This CategoryService used in the above controller class to implement Endpoint for CRUD Operation.

// Autowired the Category Repository for implement CRUD operation
@Autowired
private CategoryRepository categoryRepository;

// This class for Generating Unique Category code. 
@Autowired
private CodeGenerator codeGenerator;
// This save method for adding and updating category in the Category Table.
// Categoryinfo is the request body that is coming from the client-side. 
// I am converting CategoryInfo to Category Entity Object using MapStruct 
// Framework. Then, saving it to Database. After successful saving the Category
// it returns back to the new Category entity object, I am converting it to 
// CategoryInfo using MapStruct Framework.Then I am binding the CategoryInfo 
// with ResponseMessage to send back to the client.
// if the getCategoryCode is returned empty, I am assuming that it is a new Category
// Object creating a scenario so I am generating new Category Code otherwise just
// updating the Category.
public ResponseMessage save(CategoryInfo categoryInfo) {
    Category category = CategoryMapper.INSTANCE.categoryInfoToCategory(categoryInfo);
    if(StringUtils.isEmpty(category.getCategoryCode())) {
        String categoryCode = codeGenerator.newCode(CodeType.CATEGORY_CODE);
        category.setCategoryCode(categoryCode);
    }
    Category newCategory = categoryRepository.save(category);
    CategoryInfo categoryInfoResponse = CategoryMapper.INSTANCE.categoryToCategoryInfo(newCategory);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(categoryInfoResponse, "Category Saved" +
            " Successfully", Constants.INFO_TYPE);
    return responseMessage;
}
// This method is very similar to offer's findResourceByid so 
// it is self-explanatory
@Override
public ResponseMessage findResourceById(String id) throws Exception {
    Category category = categoryRepository.findById(Long.parseLong(id)).orElse(null);
    CategoryInfo categoryInfoResponse = CategoryMapper.INSTANCE.categoryToCategoryInfo(category);
    ResponseMessage responseMessage = ResponseMessage.withResponseData(categoryInfoResponse, Constants.SUCCESS_STATUS, Constants.INFO_TYPE);
    return responseMessage;
}
// This method is very similar to offer's findAll so,
// it is self-explanatory
@Override
public ResponseMessage findAll() throws Exception {
    List<CategoryInfo> categoryInfos = new ArrayList<>();
    List<Category> categories = categoryRepository.findAll();
    for (Category category:categories) {
        CategoryInfo categoryInfo = CategoryMapper.INSTANCE.categoryToCategoryInfo(category);
        categoryInfos.add(categoryInfo);
    }
    ResponseMessage responseMessage = ResponseMessage.withResponseData(categoryInfos,Constants.SUCCESS_STATUS,Constants.INFO_TYPE);
    return responseMessage;
}

The following article explains how the CodeGenerator class works.
https://pinepad.in/java/billing-system/billing-system-using-spring-boot-custom-id-generator-for-employee-customer-and-vendor/

The following video link explains how CodeGenerator class works
https://youtu.be/0Uoa2Wpnp-0


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