Laravel REST API Assessment

This is a simple Laravel web application providing a REST API.

Run Locally

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

List of APIs

Usage/Example

The REST API to the app is described below.

Get list of Users

Request

GET /api/v1/users

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users

Response

{
    "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"
        }
    ]
}

Create a new User

Request

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

Response

{
    "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 a specific User

Request

GET /api/v1/users/:id

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users/4

Response

{
	"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"
    }
}

Update a specific User

Request

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

Response

{
    "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"
    }
}

Create a new User's role

Request

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

Response

{
    "data": {
        "user_id": 2,
        "role_id": 1
    }
}

Get list of User's role

Request

GET api/v1/users/:id/roles

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/users/2/roles

Response

{
    "data": {
        "1": "Admin",
        "2": "User"
    }
}

Delete a specific User

Request

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

Response

// Returning response status of '204 No Content'

User Login

Request JWToken and store login_timestamp value in user_logs table

Request

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

Response

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAvYXBpL3YxL2F1dGgvbG9naW4iLCJpYXQiOjE2ODg0ODI2MjksImV4cCI6MTY4ODQ4NjIyOSwibmJmIjoxNjg4NDgyNjI5LCJqdGkiOiIxZUVrcURSUlVKNG9ydzRkIiwic3ViIjoiMyIsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.zHSfRL89l6LUdVRoWWWKfGOJzsC6c4MuPwiPClxr4BY",
    "token_type": "bearer",
    "expires_in": 3600
}

User Logout

store logout_timestamp value in user_logs table

Request

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

Response

{
    "message": "Successfully logged out"
}

Get data from external API call

Request

GET /api/v1/external-data

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/external-data

Response

{
    "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,

            // ...

Store data from external API call

Request

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

Response

{
    "message": "External data stored successfully",
    "count": 20
}

Store Post's image

Request

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

Response

{
    "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 Post's image

Request

GET /api/v1/posts/:id/image

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/posts/1/image

Response

{
    "data": {
        "image_path": "unicorn-ice-cream.jpg"
    }
}

Status Codes

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

Useful Resources

  • Locate and import Postman Collection to test API calls via Postman.
root
|
|- rest_api_postman_collection.json
|