This is a Java REST API to handle a shopping cart and apply the "Get 3 for the price of 2" promotion. The API allows users to add and remove items from the shopping cart and calculate the total price with the promotion applied. It is also possible to clear the cart.
The project follows a simple and straightforward architecture, consisting of the following components:
-
Controller: The
CartController
class is responsible for handling incoming HTTP requests and defining the API endpoints. -
Service: The
CartServiceImpl
class contains the business logic for adding, removing items, clearing the cart and calculating the total price with the promotion. -
Domain -> Entity: The
Cart
andProduct
classes represents the Cart and the Item, respectively. -
Domain -> DTO: The
ProductRequest
andCheckoutResponse
classes handle the endpoints input and output. -
Exception: The
InvalidAmountException
andInvalidProductException
classes represents custom exceptions for handling errors. -
Database: The project uses a mocked Database to define the items available in the store. The source is
resources/fake_products_database.json
-
Clone the repository or download the source code.
-
Open the project in your preferred Java IDE.
-
Build the project to resolve the dependencies.
-
Run the
Main
class to start the API.
- HTTP Method: POST
- Endpoint:
/api/shopping-cart/v1/cart/product'
- Request Body:
{ "productId": "1", "amount": 3 }
- Response: Status 200 OK
- HTTP Method: DELETE
- Endpoint:
/api/shopping-cart/v1/cart/product'
- Request Body:
{ "productId": "1", "amount": 3 }
- Response: Status 200 OK
- HTTP Method: DELETE
- Endpoint:
/api/shopping-cart/v1/cart/clear
- Response: Status 200 OK
- HTTP Method: GET
- Endpoint:
/api/shopping-cart/v1/cart/checkout'
- Response: Status 200 OK
{ "cart": [ { "productId": "1", "amount": 3 } ], "totalPrice": 25.98 }