This is a comprehensive RESTful API backend for an eCommerce platform built with Spring Boot following MVC architecture with clear separation of concerns.
- Controller Layer (
controller/): Handles HTTP requests/responses - Service Layer (
service/): Contains business logic - Repository Layer (
repository/): Data access using Spring Data JPA - Model Layer (
model/): Entity definitions - DTO Layer (
dto/): Data transfer objects for API - Exception Layer (
exception/): Custom exceptions and global error handling
- POST
/api/users - Body:
{
"username": "john_doe",
"email": "john@example.com",
"role": "customer",
"address": {
"zip": "12345",
"country": "USA",
"street": "123 Main St",
"province": "CA"
}
}- Response:
201 Createdwith UserDTO
- GET
/api/users/{id} - Response:
200 OKwith UserDTO
- GET
/api/users/email/{email} - Response:
200 OKwith UserDTO
- GET
/api/users - Response:
200 OKwith List
- GET
/api/users/role/{role} - Response:
200 OKwith List
- PUT
/api/users/{id} - Body: CreateUserRequest
- Response:
200 OKwith UserDTO
- DELETE
/api/users/{id} - Response:
204 No Content
- POST
/api/products - Body:
{
"name": "Laptop",
"quantity": 50,
"description": "High-performance laptop",
"image": "laptop.jpg",
"brandId": 1,
"categoryId": 1
}- Response:
201 Createdwith ProductDTO
- GET
/api/products/{id} - Response:
200 OKwith ProductDTO
- GET
/api/products?availableOnly=true - Query Params:
availableOnly(optional): boolean - filter only available products
- Response:
200 OKwith List
- GET
/api/products/brand/{brandId} - Response:
200 OKwith List
- GET
/api/products/category/{categoryId} - Response:
200 OKwith List
- GET
/api/products/search?name={searchTerm} - Response:
200 OKwith List
- GET
/api/products/filter?brandId={brandId}&categoryId={categoryId} - Response:
200 OKwith List
- PUT
/api/products/{id} - Body: CreateProductRequest
- Response:
200 OKwith ProductDTO
- PATCH
/api/products/{id}/quantity?quantity={newQuantity} - Response:
200 OKwith ProductDTO
- DELETE
/api/products/{id} - Response:
204 No Content
- POST
/api/basket - Body:
{
"userId": 1,
"productId": 5,
"quantity": 2
}- Response:
201 Createdwith BasketItemDTO
- GET
/api/basket/user/{userId} - Response:
200 OKwith List
- GET
/api/basket/user/{userId}/count - Response:
200 OKwith count
- PATCH
/api/basket/{basketItemId}?quantity={newQuantity} - Response:
200 OKwith BasketItemDTO
- DELETE
/api/basket/{basketItemId} - Response:
204 No Content
- DELETE
/api/basket/user/{userId} - Response:
204 No Content
- POST
/api/orders - Body:
{
"userId": 1,
"addressId": 1
}- Response:
201 Createdwith OrderDTO - Note: This converts the user's basket into an order and clears the basket
- GET
/api/orders/{id} - Response:
200 OKwith OrderDTO
- GET
/api/orders - Response:
200 OKwith List
- GET
/api/orders/user/{userId} - Response:
200 OKwith List (sorted by date descending)
- GET
/api/orders/status/{status} - Status values: PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED
- Response:
200 OKwith List
- PATCH
/api/orders/{id}/status?status={newStatus} - Response:
200 OKwith OrderDTO
- DELETE
/api/orders/{id} - Response:
204 No Content - Note: Only pending/processing orders can be cancelled. Inventory is restored.
The API uses a global exception handler that returns consistent error responses:
{
"timestamp": "2023-12-13T10:30:00",
"status": 400,
"error": "Bad Request",
"message": "Detailed error message",
"path": "/api/products/1",
"validationErrors": {
"fieldName": "error message"
}
}200 OK: Successful GET/PUT/PATCH request201 Created: Successful POST request204 No Content: Successful DELETE request400 Bad Request: Invalid input or business rule violation404 Not Found: Resource not found409 Conflict: Duplicate resource500 Internal Server Error: Unexpected error
- Product quantity cannot be negative
- Products must have valid brand and category
- Cannot add more items than available in stock
- Quantity must be positive
- If product already in basket, quantities are combined
- Stock is validated when adding/updating basket items
- Cannot create order with empty basket
- Stock is validated before order creation
- Inventory is reduced when order is created
- Basket is cleared after successful order
- Order status transitions are validated
- Cannot cancel shipped/delivered orders
- Cancelling an order restores inventory
- Email must be unique
- Username must be unique
- Valid email format required
- id, name, quantity, description, image
- brand (Brand entity)
- category (Category entity)
- id, username, email, role
- address (Address entity)
- id, quantity
- user (User entity)
- product (Product entity)
- id, status, total, timeCreated
- user (User entity)
- address (Address entity)
- basket (Basket entity - contains order items)
- PENDING
- PROCESSING
- SHIPPED
- DELIVERED
- CANCELLED
- Build the project:
mvn clean install- Run the application:
mvn spring-boot:run-
Access the API at:
http://localhost:8080/api -
H2 Database Console:
http://localhost:8080/h2-console- JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password: (leave blank)
- JDBC URL: