This is a simple PHP application providing a REST API.
Looking for the Laravel version of this application? Click here
Clone the project
git clone https://github.com/ammarkero/rest-api-assessment.git
Go to the project directory
cd rest-api-assessment
Install dependencies
composer install
Start the server
php -S localhost:8888 -t public
- User:
- get all users
- create a new user
- get a specific user
- update an user
- store user's role
[many-to-many relationship]
- get user's role(s)
[many-to-many relationship]
- delete an user
- Authentication:
- user login (generate JWToken)
- user logout
- External data:
- Post:
- store post's image
[polymorhpic relationship]
- get post's image(s)
[polymorhpic relationship]
- store post's image
The REST API to the app is described below.
GET /api/v1/user/all
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/all
{
"status":200,
"message":"OK",
"data":[
{
"id":1,
"name":"Jake Smith",
"email":"[email protected]",
"password":"$2y$10$e\/pbdN7ji1UWp\/GheWPKJOaKF7zG14RNj2doLqP3CX7GHhkFFJGs2"
},
{
"id":2,
"name":"Donato Padberg",
"email":"[email protected]",
"password":"$2y$10$Ob7Rz3kUa0\/mqsQNHJKxdeFTwhDptoClk6hASn8aunp8sRyfqlkXi"
}
]
}
POST /api/v1/user/create
url \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"name": "Xavier", "email": "[email protected]","password":"12345678"}' \
http://localhost:8888/api/v1/user/create
{
"status": 201,
"message": "User successfully created",
"data": {
"id": 3,
"name": "Xavier",
"email": "[email protected]",
"password": "$2y$10$eAvte8C7yxnJk/nLqzzHN.xGIIDY81rn3CoDJCZNDAWuEDjZ1DWhu"
}
}
GET /api/v1/user/single?:id
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/single?id=4
{
"status": 200,
"message": "OK",
"data": {
"id": 4,
"name": "Rebeca Terry",
"email": "[email protected]",
"password": "$2y$10$.b1vaE0DKJ0k/SMTfR2IIe/HZ/urLaumrXJzZk3Fev2pzZjkRUHWe"
}
}
PUT /api/v1/user/update
curl \
-i -X PUT \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"id": "1","name": "Sara","email": "[email protected]","password": "abc1234567"} \
http://localhost:8888/api/v1/user/update
{
"status": 200,
"message": "OK",
"data": {
"id": 1,
"name": "Sara",
"email": "[email protected]",
"password": "$2y$10$4ajTn60a7slI6OgXQiI8OulFgmp6LOkOp8iQFVen\/y62YMSrzLDyi"
}
}
POST /api/v1/user/role/create
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"email": "[email protected]","role_id": "1"}'\
http://localhost:8888/api/v1/user/role/create
{
"status": 200,
"message": "OK",
"data": [
{
"title": "Admin"
}
]
}
GET /api/v1/user/role?:id
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/role?id=1
{
"status": 200,
"message": "OK",
"data": [
{
"title": "Admin"
}
]
}
DELETE /api/v1/user/delete
curl \
-i -X DELETE \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"id": "13"}' \
http://localhost:8888/api/v1/user/delete
{
"status": 200,
"message": "User successfully deleted",
"data": []
}
Request JWToken and store login_timestamp
value in user_logs
table
POST /api/v1/auth/login
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"email": "[email protected]","password":"abc1234567"}' \
http://localhost:8888/api/v1/auth/login
{
"status": 200,
"message": "User access token generated.",
"data": {
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb2NhbGhvc3Q6ODg4OCIsInN1YiI6MSwiaWF0IjoxNjg3NDk1MDE5LCJleHAiOjE2ODc0OTUzMTl9.PxFcZrSWLtgQZdxtm8C6XpZkHm5URSXisOqFJ52vUz8"
}
}
store logout_timestamp
value in user_logs
table
POST /api/v1/auth/logout
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-H "Authorization: Bearer {token}" \
http://localhost:8888/api/v1/auth/logout
{
"status": 200,
"message": "User logged out successfully",
"data": []
}
GET /api/v1/external-data/read
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/external-data/read
{
"status": 200,
"message": "External data retrieved successfully",
"data": [
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 1,
"id": 2,
...
POST /api/v1/external-data/read
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
http://localhost:8888/api/v1/external-data/store
{
"status": 200,
"message": "20 external data retrieved and stored successfully",
"data": []
}
POST /api/v1/post/image/store
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"post_id": "1","image_path": "placeholder-2.jpg"}'
http://localhost:8888/api/v1/post/image/store
{
"status": 200,
"message": "Image stored successfully",
"data": []
}
GET /api/v1/post/image/read?:id
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/post/image?id=1
{
"status": 200,
"message": "OK",
"data": [
{
"image_path": "unicorn.jpg"
},
{
"image_path": "cat.jpg"
}
]
}
Response returns the following status codes in its API:
Status Code | Description |
---|---|
200 | OK |
201 | CREATED |
400 | BAD REQUEST |
404 | NOT FOUND |
429 | TOO MANY REQUESTS |
500 | INTERNAL SERVER ERROR |
// config.php
return [
'database' => [
'host' => 'localhost',
'port' => 3306,
'dbname' => 'rest_api_db',
'charset' => 'utf8mb4'
],
'services' => [
'jwt' => [
'secret_key' => 'your-secret-key',
'expiry_time' => 5 * 60, // Expiry time in seconds (5 minutes)
]
]
];
To run tests, run the following command
composer test
or
vendor/bin/phpunit
- Locate (and pick one) and import SQL Dump into your database.
root
|
|- rest_api_db.sql
|- rest_api_db.gz
|
- Locate and import Postman Collection to test API calls via Postman.
root
|
|- rest_api_postman_collection.json
|