A simple example of a shopping platform implemented in Kotlin with hexagonal architecture.
- REST API Endpoints:
- Retrieve Product Information: Fetch details of a product using its UUID.
- Calculate Total Price: Compute the total price for a given product and quantity, applying applicable discounts.
- Configurable Discount Policies.
- Hexagonal Architecture.
- In-Memory Database: For simplicity, the service uses an in-memory database to store product information.
The architecture of the project is illustrated in the following diagram:
- If multiple discounts are applicable to a product, the service will apply the discount that results in the lowest total price for the customer.
- Percentage-based discounts are only applicable to specific products. There is a predefined list of product IDs for which each percentage-based discount can be applied. Products not on this list will not receive the percentage-based discount.
- The product data is fixed and hardcoded in the in-memory repository implementation.
- Discount policies are defined and configurable through the application.yaml file.
- The maximum number of products that can be processed in a single price calculation request is limited to 100
Run the following command to clean and build the project:
./gradlew clean build
./gradlew bootJar
Use the following command to build the Docker image:
docker build -t shopping-platform .
docker run -p 8080:8080 --name shopping-platform-container shopping-platform
docker stop shopping-platform-container
docker rm shopping-platform-container
The following products are preloaded into the in-memory database:
ID | Price (PLN) |
---|---|
aac7d817-93f0-4f6f-92c4-6752c95d23b0 | 599.99 |
aac7d817-93f0-4f6f-92c4-6752c95d23b1 | 11999.99 |
aac7d817-93f0-4f6f-92c4-6752c95d23b2 | 5299.99 |
aac7d817-93f0-4f6f-92c4-6752c95d23b3 | 1299.00 |
aac7d817-93f0-4f6f-92c4-6752c95d23b4 | 7699.99 |
The discounts applied to the products are configured in the application.yaml
file, located in the src/main/resources.
Here’s an example configuration:
discounts:
amount-based:
- amount: 50
threshold: 10
- amount: 30
threshold: 5
- amount: 10
threshold: 2
percentage-based:
- productIds:
- "aac7d817-93f0-4f6f-92c4-6752c95d23b0"
- "aac7d817-93f0-4f6f-92c4-6752c95d23b1"
percentage: 10
- productIds:
- "aac7d817-93f0-4f6f-92c4-6752c95d23b2"
percentage: 15
- productIds:
- "aac7d817-93f0-4f6f-92c4-6752c95d23b3"
percentage: 20
To retrieve product information by UUID, you can use the following curl
command.
curl -X GET "http://localhost:8080/products/aac7d817-93f0-4f6f-92c4-6752c95d23b0" -H "Accept: application/json"
To calculate total price of the product, you can use the following curl
command.
curl -X POST "http://localhost:8080/products/aac7d817-93f0-4f6f-92c4-6752c95d23b0/calculate-price" \
-H "Content-Type: application/json" \
-d '{"quantity": 2}'