Sample product / cart API
This is sample REST service that provides simple functionality of product store and shopping cart.
Requirements
Client should be able to:
- add product to the catalog
- remove product from the catalog
- update product name
- update product price
- list all products in the catalog as a paginated list with at most 3 products per page
- create a cart
- add product to the cart
- remove product from the cart
- list all products in the cart
- cart can contain a max. of 3 products
- cart should display a total price of all products in it
Design
Solution is modelled as two "separate" microservices (although for simplicity living in one PHP application). Product microservice provides REST endpoints that allows to manipulate product and persist it in database. Cart microservice provides REST endpoints for manipulating cart and products in cart and persist it in cart database, separate from product database to allow independent scalability of both microservices. For the same reason cart database contains copy of relevant product data (for the sake of availability consistency is exchanged for eventual consistency).
Data synchronization between two services is conducted via event bus. Modifications in product database send appropriate events to event bus to which cart microservice subscribes. In real case scenario event bus would be realized using e.g. RMQ in this case, again, event bus is modelled directly in PHP. Still the only information sent via event bus is unique product identifier, the rest of information must be fetched from product microservice directly. Again, in real world scenario product microservice would expose REST endpoint and cart microservice would use it to fetch information about product. For simplicity this project allows cart service to reuse product repository.
REST Endpoints Definitions
See raw swagger file or Swagger UI (after you follow "How to run" section).
How to run
# install dependencies
composer install
# make sure tests pass (phpunit + behat), code is clean (phpcs + phpmd)
composer check
# start built in www server on port 8000
php bin/console server:run
Now you can play with API directly, or via built in Swagger UI.
Libraries / frameworks used:
Software used in project include: