An e-commerce cart API.
The API receives a POST request in its only endpoint (/cart/add
). The payload includes a list of products and its quantities like the example below:
{
"products": [
{
"id": 1,
"quantity": 1 // Product quantity to be bought
}
]
}
The API data comes from the JSON file products.json
, so the application has no database.
For each product the API calculate the discount percentage by consuming a gRPC service provided by an external provider.
If the discount service is unavailable, the cart endpoint continue to work, but it will not calculate the discount.
If the BLACKFRIDAY_DATE
environment variable indicates that is black friday, the API must add a free product to the cart.
Gift products have the flag is_gift = true
and should not be accepted in requests to add them to the cart.
There must be only one free product entry in the cart.
The API response should bring the total value of the cart with and without discount, total value of discounts and the list of products with their individual discounts. The response must respect the structure of the following example payload and all monetary values must be in cents:
{
"total_amount": 20000, // Purchase total amount without discount
"total_amount_with_discount": 19500, // Purchase total amount with discount
"total_discount": 500, // Discount total amount
"products": [
{
"id": 1,
"quantity": 2,
"unit_amount": 10000, // Product unity price in cents
"total_amount": 20000, // Total amount for this product in cents
"discount": 500, // Discount total amount in cents
"is_gift": false // Is gift?
},
{
"id": 3,
"quantity": 1,
"unit_amount": 0, // Product unity price in cents
"total_amount": 0, // Total amount for this product in cents
"discount": 0, // Discount total amount in cents
"is_gift": true // Is gift?
}
]
}
- PHP 8
- Framework Lumen 8
- PHPUnit 9.5
- gRPC
- Docker
- Docker Compose
Copy content of file .env.example into a new file .env.
Execute the following command in the project root directory:
$ docker-compose up -d
The installation of the dependencies is done when docker is initialized. Therefore, there is no need to do this procedure manually.
To execute the unit tests, with the containers running, just use the following command:
$ docker-compose run phpunit
To execute any action in the composer, use the following command pattern:
$ docker run --rm --interactive --tty \
--volume $PWD:/app \
composer <command>
If some proto file is created or changed, is possible to update them using the following command:
$ docker run -v `pwd`:/defs namely/protoc-all -f discount.proto -l php -o app/Proto/
In the example above, the classes for the discount.proto
file are being generated.