This project is a backend application for an e-commerce platform that manages shopping carts. The service allows creating (with automatic ID generation) and deleting carts, adding products to carts, and automatically removing inactive carts after 10 minutes. The application is developed in Java and uses Maven for dependency management.
- Java 17: Ensure you have Java 17 installed on your system.
- Maven: This project uses Maven as the build tool and dependency manager.
- Docker: Optional, if you want to build and run the application within a Docker container.
- Postman: For testing API endpoints.
Clone the repository to your local machine:
git clone https://github.com/Diego-on-c/e-commerce-oneboxUse Maven to build the project. This will compile the code and generate a JAR file in the target directory:
mvn clean installYou can run the unit tests with the following command:
mvn testTo run the application locally, use the following command:
java -jar target/e-commerce-0.0.1.jarThis will start the server on port 8080.
- Create a cart:
POST /carts - Get cart information:
GET /carts/{id} - Update cart with new products:
PUT /carts/{id} - Delete a cart:
DELETE /carts/{id}
-
Create a Cart
- Open Postman and create a new
POSTrequest. - Enter the URL:
http://localhost:8080/carts. - In the "Body" tab, select "raw" and "JSON" format.
- Enter the following JSON data:
[ { "id": 1, "description": "Product 1", "amount": 20.0 }, { "id": 2, "description": "Product 2", "amount": 10.0 } ] - Click "Send".
- The response will include the newly created cart's ID, which you can use in the following steps.
- Open Postman and create a new
-
Update a Cart
-
Create a
PUTrequest in Postman. -
Enter the URL:
http://localhost:8080/carts/{id}, replacing{id}with the ID you received in the previous step. -
In the "Body" tab, select "raw" and "JSON" format.
-
Enter the following JSON data:
[ { "id": 1, "description": "Product 1 Updated!", "amount": 2.0 }, { "id": 2, "description": "Product 2 Updated!", "amount": 1.0 } ] -
Click "Send".
-
The response will confirm that the products were added to the cart.
-
-
Get Cart Information
- Create a new
GETrequest in Postman. - Enter the URL:
http://localhost:8080/carts/{id}, replacing{id}with your cart ID. - Click "Send".
- The response will show the details of the cart, including the products added.
- Create a new
-
Delete a Cart
- Create a new
DELETErequest in Postman. - Enter the URL:
http://localhost:8080/carts/{id}, replacing{id}with your cart ID. - Click "Send".
- The response will confirm that the cart has been deleted.
- Create a new
Cart IDs are generated using a method that creates an auto-incremental ID.
Carts are automatically deleted after 10 minutes of inactivity. This is managed by an internal scheduler that checks and cleans up inactive carts.
Carts are stored in memory using a volatile data structure (HashMap). No database engine is used, so data is lost upon application restart.
The project includes a Dockerfile that allows you to build and run the application within a Docker container.
docker build -t ecommerce:latest .docker run -p 8080:8080 ecommerce:latestThis will expose the application on port 8080 of your local machine.