API to Create, Fetch and Take orders.
- Docker as the container service to isolate the environment.
- Php to develop backend support.
- Laravel as the server framework / controller layer
- MySQL as the database layer
- NGINX as a proxy / content-caching layer
- Clone the repo.
src
folder contains the complete application code.- As we have used the Google distance matrix api to calculate distance from the Co-ordinates, you need API key for the same. Go to https://cloud.google.com/maps-platform/routes/ after login create new project and get the API key. update 'GOOGLE_API_KEY' in environment file located in ./src/.env file
- Within the root folder you will find "start.sh" file. Run
./start.sh
to build docker containers, executing migration and PHPunit Unit & Feature test cases - After starting container following will be executed automatically:
- Composer will install all dependencies required to run the code.
- Config and Application Cache will be cleared.
- Table migrations using artisan migrate command.
- Unit and Feature Integration test cases execution.
- For running migrations manually
docker exec orderapi_php php artisan migrate
- You can run
docker-compose up
from terminal - Server is accessible at
http://localhost:8080
- To run the Feature test cases only:
docker exec orderapi_php php ./vendor/phpunit/phpunit/phpunit ./tests/Feature
- To run the Unit test cases only:
docker exec orderapi_php php ./vendor/phpunit/phpunit/phpunit ./tests/Unit
- Run both Feature Integration & Unit test case suite:
docker exec orderapi_php php ./vendor/phpunit/phpunit/phpunit
- Open URL for API demo
http://localhost:8080/api-documentation
- Here you can perform all API operations like GET, UPDATE, POST
src folder contain application code.
./tests
- this folder contains both Feature and Unit test cases files.
./app
- Folder contains all the framework configuration file, controllers and models
- Migration files are present inside the database/migrations/ folder
- To run manually migrations use this command
docker exec orderapi_php php artisan migrate
- To run manually migrations use this command
OrderController
contains all the api's methods :- localhost:8080/orders?page=1&limit=4 - GET url to fetch orders with page and limit
- localhost:8080/orders - POST method to insert new order with origin and distination
- localhost:8080/orders - PATCH method to update status for taken. (handled the concurrent request for taking the order. If order already taken then other request will get response status 409)
DistanceService
contains the logic to calculate distance from Co-ordinates.
.env
- env file contain all project configuration, you can configure database, session and custom configuration. We have set the GOOGLE_API_URL, GOOGLE_API_KEY and GOOGLE_API_UNIT in the env file so that it is configurable.
-
Find below API documentation for the help.
-
localhost:8080/orders?page=:page&limit=:limit
:GET Method - to fetch orders with page number and limit
-
Header :
- GET /orders?page=2&limit=3
- Host: localhost:8080
- Content-Type: application/json
-
Responses :
- Response [ { "id": 4, "distance": 854523, "status": "TAKEN" }, { "id": 5, "distance": 147959, "status": "UNASSIGNED" }, { "id": 6, "distance": 19285, "status": "TAKEN" } ]
Code Description - 200 Successful Operation - 400 Request Denied - 406 Invalid Request Parameter
-
-
localhost:8080/orders
:POST Method - to create new order with origin and distination
-
Header :
- POST /orders
- Host: localhost:8080
- Content-Type: application/json
-
Post-Data :
{ "origin": ["-33.865143","151.209900"], "destination": ["-37.663712","144.844788"] }
- Responses :
- Response { "id": 1, "distance": 854523, "status": "UNASSIGNED" }
Code Description - 200 Successful Operation - 400 Request Denied - 406 Invalid Input
-
-
localhost:8080/orders/:id
:PATCH method to update status for orders already taken. (Handled simultaneous update request from multiple users at the same time with response status 409)
-
Header :
- PATCH /orders/3
- Host: localhost:8080
- Content-Type: application/json
-
Post-Data :
{ "status" : "TAKEN" }
- Responses :
- Response { "status": "SUCCESS" }
or
- Response { "status": "ORDER_ALREADY_TAKEN" }
Code Description - 200 successful operation - 400 Invalid Request Parameter - 404 Invalid Order Id - 409 Order already taken
-