This is a simple Laravel web application providing a REST API.
Clone the project
git clone https://github.com/ammarkero/laravel-rest-api-assessment.git
Go to the project directory
cd laravel-rest-api-assessment
Generate app key
php artisan key:generate
Run database migration and seeder
php artisan migrate:refresh --seed
Install dependencies
composer install
Start the server
php artisan serve
- 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:one-to-one]
- get post's image
[polymorhpic relationship:one-to-one]
- store post's image
The REST API to the app is described below.
GET /api/v1/users
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users
{
"data":[
{
"id":1,
"name":"Jake Smith",
"email":"jakesmith@email.com",
"created_at": "2023-07-04T05:36:14.000000Z",
"updated_at": "2023-07-04T10:24:28.000000Z"
},
{
"id":2,
"name":"Donato Padberg",
"email":"donato.padberg@email.com",
"created_at": "2023-07-04T05:36:14.000000Z",
"updated_at": "2023-07-04T10:24:28.000000Z"
}
]
}
POST /api/v1/users
url \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"name": "Xavier", "email": "hello@xavier.com","password":"12345678"}' \
http://localhost:8888/api/v1/users
{
"data": {
"id": 3,
"name": "Xavier",
"email": "hello@xavier.com",
"created_at": "2023-07-04T14:39:11.000000Z",
"updated_at": "2023-07-04T14:39:11.000000Z"
}
}
GET /api/v1/users/:id
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users/4
{
"data": {
"id":2,
"name":"Donato Padberg",
"email":"donato.padberg@email.com",
"created_at": "2023-07-04T05:36:14.000000Z",
"updated_at": "2023-07-04T10:24:28.000000Z"
}
}
PUT /api/v1/users/:id
curl \
-i -X PUT \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"name": "Sara","email": "hello@sara.com","password": "abc1234567"} \
http://localhost:8888/api/v1/users/1
{
"data": {
"id": 1,
"name": "Sara",
"email": "hello@sara.com",
"created_at": "2023-07-04T14:39:11.000000Z",
"updated_at": "2023-07-04T14:39:11.000000Z"
}
}
POST /api/v1/users/:id/roles
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"role_id": "1"}'\
http://localhost:8888/api/v1/users/2/roles
{
"data": {
"user_id": 2,
"role_id": 1
}
}
GET api/v1/users/:id/roles
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users/2/roles
{
"data": {
"1": "Admin",
"2": "User"
}
}
DELETE /api/v1/users/:id
curl \
-i -X DELETE \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
http://localhost:8888/api/v1/users/1
// Returning response status of '204 No Content'
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": "zack@hello.com", "password": "12345678"}' \
http://localhost:8888/api/v1/auth/login
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAvYXBpL3YxL2F1dGgvbG9naW4iLCJpYXQiOjE2ODg0ODI2MjksImV4cCI6MTY4ODQ4NjIyOSwibmJmIjoxNjg4NDgyNjI5LCJqdGkiOiIxZUVrcURSUlVKNG9ydzRkIiwic3ViIjoiMyIsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.zHSfRL89l6LUdVRoWWWKfGOJzsC6c4MuPwiPClxr4BY",
"token_type": "bearer",
"expires_in": 3600
}
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
{
"message": "Successfully logged out"
}
GET /api/v1/external-data
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/external-data
{
"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
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
http://localhost:8888/api/v1/external-data
{
"message": "External data stored successfully",
"count": 20
}
POST /api/v1/posts/:id/image
curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"image_path": "unicorn-ice-cream.jpg"}'
http://localhost:8888/api/v1/posts/1/image
{
"data": {
"id": 1,
"title": "Tallest Mountain on Earth",
"content": "Mount Everest is Earth's highest...",
"image": {
"id": 1,
"image_path": "unicorn-ice-cream.jpg"
}
}
}
GET /api/v1/posts/:id/image
curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/posts/1/image
{
"data": {
"image_path": "unicorn-ice-cream.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 |
- Locate and import Postman Collection to test API calls via Postman.
root
|
|- rest_api_postman_collection.json
|