Welcome to the eCommerce MVC project! This project is a simple example of a web application built using the Model-View-Controller (MVC) architecture. In this project, we've implemented a basic e-commerce system for managing products and categories, now with additional APIs and data validation. This README will provide an updated overview of the project structure and how to get started.
The project is organized into several packages, each responsible for a specific part of the application.
This package contains the controller class, which handles HTTP requests and communicates with the service layer. The controller is responsible for managing various endpoints related to products and categories. In this package, you'll find the following class:
- ProductController: This class is annotated with
@RestControllerand handles HTTP requests related to products. It now provides new endpoints and validation:POST /product: Adds a new product to the system. This endpoint now includes data validation to ensure the correctness of the input.GET /products: Retrieves a list of all products.GET /product/{productId}: Retrieves a single product by its ID.PUT /product/{productId}: Updates an existing product.DELETE /product/{productId}: Deletes a product by its ID.
The service package contains the business logic of the application. It communicates with the repository to perform operations on products and categories. Here, you'll find the following class:
- ProductService: This class is responsible for handling product-related business logic. It now includes validation logic to ensure that products meet certain criteria. It contains methods for adding a product, retrieving all products, updating a product, and deleting a product.
The repository package manages the data source, which in this case is an in-memory map of products. You'll find the same classes as before:
-
DataSource: This class is annotated with
@Configurationand defines a@Beanthat initializes an empty map to serve as the data source for products. -
ProductRepo: This class is responsible for managing the product data. It is annotated with
@Componentand uses theproductMapbean provided byDataSourceto store and retrieve product information.
This package contains the data model classes, including the product and category classes, which have not changed.
To run this updated project, follow these steps:
-
Ensure you have the necessary dependencies and tools installed, including Java, Spring Boot, and an Integrated Development Environment (IDE) of your choice.
-
Clone the updated project repository to your local machine.
-
Open the updated project in your IDE.
-
Build and run the updated project.
-
Use a tool like Postman or a web browser to interact with the project's APIs by making HTTP requests to the defined endpoints in the
ProductController.
Here's how you can interact with the updated project:
-
To add a product, make a POST request to
http://localhost:8080/productwith a JSON body containing the product details. The data validation will ensure that the input is correct. -
To retrieve a list of all products, make a GET request to
http://localhost:8080/products. -
To retrieve a single product by its ID, make a GET request to
http://localhost:8080/product/{productId}. -
To update an existing product, make a PUT request to
http://localhost:8080/product/{productId}with a JSON body containing the updated product details. -
To delete a product by its ID, make a DELETE request to
http://localhost:8080/product/{productId}.