Skip to content

Instantly share code, notes, and snippets.

@fdciabdul
Created September 6, 2024 08:16
Show Gist options
  • Save fdciabdul/7a6d74bf89910da293276817860d4464 to your computer and use it in GitHub Desktop.
Save fdciabdul/7a6d74bf89910da293276817860d4464 to your computer and use it in GitHub Desktop.

Authentication

Login

POST /login

Authenticate a user and return a Bearer token.

Request Body (JSON)

{
  "email": "user@example.com",
  "password": "password123"
}

Example cURL Request

curl --request POST \
  --url http://localhost:3333/login \
  --header 'Content-Type: application/json' \
  --data '{
    "email": "user@example.com",
    "password": "password123"
  }'

Example Postman Request

  • Method: POST
  • URL: http://localhost:3333/login
  • Body: Raw JSON
{
  "email": "user@example.com",
  "password": "password123"
}

Example Response

{
  "type": "bearer",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": 1,
    "username": "budi",
    "email": "budi@example.com",
    "roleId": 1,
    "createdAt": "2024-09-03T06:09:36.000Z",
    "updatedAt": "2024-09-03T06:09:36.000Z"
  }
}

User Routes

Get All Users

GET /users

Get a list of all users.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/users \
  --header 'Authorization: Bearer <your_token>'

Example Postman Request

  • Method: GET
  • URL: http://localhost:3333/users
  • Authorization: Bearer Token

Example Response

[
  {
    "id": 1,
    "username": "budi",
    "email": "budi@example.com",
    "roleId": 1,
    "createdAt": "2024-09-03T06:09:36.000Z",
    "updatedAt": "2024-09-03T06:09:36.000Z"
  },
  {
    "id": 2,
    "username": "siti",
    "email": "siti@example.com",
    "roleId": 2,
    "createdAt": "2024-09-03T06:09:36.000Z",
    "updatedAt": "2024-09-03T06:09:36.000Z"
  }
]

Get User by ID

GET /users/:id

Retrieve a specific user by their ID.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/users/1 \
  --header 'Authorization: Bearer <your_token>'

Example Postman Request

  • Method: GET
  • URL: http://localhost:3333/users/1
  • Authorization: Bearer Token

Example Response

{
  "id": 1,
  "username": "budi",
  "email": "budi@example.com",
  "roleId": 1,
  "createdAt": "2024-09-03T06:09:36.000Z",
  "updatedAt": "2024-09-03T06:09:36.000Z"
}

Create User

POST /users

Create a new user.

Request Body (JSON)

{
  "username": "agus",
  "email": "agus@example.com",
  "password": "password123",
  "roleId": 3
}

Example cURL Request

curl --request POST \
  --url http://localhost:3333/users \
  --header 'Authorization: Bearer <your_token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "username": "agus",
    "email": "agus@example.com",
    "password": "password123",
    "roleId": 3
  }'

Example Postman Request

  • Method: POST
  • URL: http://localhost:3333/users
  • Body: Raw JSON
  • Authorization: Bearer Token
{
  "username": "agus",
  "email": "agus@example.com",
  "password": "password123",
  "roleId": 3
}

Example Response

{
  "id": 3,
  "username": "agus",
  "email": "agus@example.com",
  "roleId": 3,
  "createdAt": "2024-09-03T06:09:36.000Z",
  "updatedAt": "2024-09-03T06:09:36.000Z"
}

Update User

PUT /users/:id

Update a user’s details by ID.

Request Body (JSON)

{
  "username": "agus",
  "email": "agus@example.com"
}

Example cURL Request

curl --request PUT \
  --url http://localhost:3333/users/3 \
  --header 'Authorization: Bearer <your_token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "username": "agus",
    "email": "agus@example.com"
  }'

Example Postman Request

  • Method: PUT
  • URL: http://localhost:3333/users/3
  • Body: Raw JSON
  • Authorization: Bearer Token
{
  "username": "agus",
  "email": "agus@example.com"
}

Example Response

{
  "id": 3,
  "username": "agus",
  "email": "agus@example.com",
  "roleId": 3,
  "createdAt": "2024-09-03T06:09:36.000Z",
  "updatedAt": "2024-09-03T06:09:36.000Z"
}

Delete User

DELETE /users/:id

Delete a user by ID.

Example cURL Request

curl --request DELETE \
  --url http://localhost:3333/users/3 \
  --header 'Authorization: Bearer <your_token>'

Example Postman Request

  • Method: DELETE
  • URL: http://localhost:3333/users/3
  • Authorization: Bearer Token

Location Routes

Get All Locations

GET /locations

Get a list of all locations.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/locations \
  --header 'Authorization: Bearer <your_token>'

Example Postman Request

  • Method: GET
  • URL: http://localhost:3333/locations
  • Authorization: Bearer Token

Get Location by ID

GET /locations/:id

Retrieve a specific location by its ID.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/locations/1 \
  --header 'Authorization: Bearer <your_token>'

Barn Routes

Get All Barns

GET /barns

Get a list of all barns.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/barns \
  --header 'Authorization: Bearer <your_token>'

Floor Routes

Get All Floors

GET /floors

Get a list of all floors.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/floors \
  --header 'Authorization: Bearer <your_token>'

Stock Routes

Get All Stocks

GET /stocks

Get a list of all stocks.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/stocks \
  --header 'Authorization: Bearer <your_token>'

Stock History Routes

Get All Stock Histories

GET /stock-histories

Get a list of all stock histories.

Example cURL Request

curl --request GET \
  --url http://localhost:3333/stock-histories \
  --header 'Authorization: Bearer <your_token>'

Protected Routes

These routes require the Authorization header with a valid Bearer token to access. The Bearer token is obtained from the login response.

Example Authorization Header:

Authorization: Bearer <your_token>

This provides a comprehensive guide on how to interact with the API using either Postman or curl. Each route includes the HTTP method, endpoint, required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment