A shopping cart API built with Symfony.
First off, install Nix.
It is recommended that you configure Nix to avoid possible issues later on.
Specifically, this project uses flakes, which is officially still considered an experimental feature.
Enter this into ~/.config/nix/nix.conf
(create it if it doesn't exist):
experimental-features = nix-command flakes
Then, install direnv.
Restart your shell, re-enter the project directory, and run direnv allow
, as instructed.
Then, everything should be available in the shell, such as php
, composer
, and devenv
.
In order for this environment to be available in your editor, you either need to start your editor from a shell that has loaded the environment, or if you're using VSCode, it is recommended that you use the official Plugin mkhl.direnv
to dynamically load the environment via direnv.
Run composer install
to install the required PHP dependencies.
Now that the development shell and Composer dependencies have been set up, run devenv up
to start the web and database servers.
The application itself will run on localhost:8000.
Additionally, an AdminerEvo instance will run on localhost:8001.
The credentials for the database can be found in the .env
file.
Run symfony console doctrine:migrations:migrate
to initialize the database.
Use the command php bin/phpunit --testdox
to run all unit tests.
Here are the commands that will generate some example data for you to play around with:
# Generate 5 users with random data
symfony console app:example user -a 5
# Generate 20 products with random data
symfony console app:example product -a 20
The API documentation can be found at /api/doc
.
API routes are prefixed by /api/v<version-number>
, e.g. /api/v1
.
Here are some examples using cURL:
# Show all users
curl http://localhost:8080/api/v1/users
# Create a user
curl -X POST -H 'Content-Type: application/json' -d '{"email": "[email protected]", "firstName": "Some", "lastName": "Person"}' http://localhost:8000/api/v1/users
# Create or replace a user
curl -X PUT -H 'Content-Type: application/json' -d '{"email": "[email protected]", "firstName": "Omari", "lastName": "Boyle"}' http://localhost:8000/api/v1/users/9
# Delete a user
curl -X DELETE http://localhost:8000/api/v1/users/9
# Add a basket item to a user
curl -X POST -H 'Content-Type: application/json' -d '{"productId": 1}' http://localhost:8000/api/v1/users/2/basket-items
# List all basket items associated with this user
curl http://localhost:8000/api/v1/users/2/basket-items
# Delete a basket item
curl -X DELETE http://localhost:8000/api/v1/users/2/basket-items/1
After making changes to the code, it is recommended to run php-cs-fixer
as follows to alleviate any problems with the code style: php vendor/bin/php-cs-fixer fix --allow-risky=yes
.
For static analysis, run php vendor/bin/phpstan analyse
.