Roles API¶
Overview¶
The Roles API provides endpoints to manage roles within the application. It allows for creating roles, retrieving role information, and deleting roles.
Endpoints¶
Create Role¶
- URL:
/api/authService/role - Method:
POST - Description: Creates a new role with the provided name and description.
- Request Body:
{ "name": "role_name", "description": "role_description" } - Response:
{ "id": 1, "name": "role_name", "description": "role_description" } - Example Request:
curl -X POST http://localhost:3000/api/authService/role -H "Content-Type: application/json" -d '{"name": "admin", "description": "Administrator role"}' - Example Response:
{ "id": 1, "name": "admin", "description": "Administrator role" }
Get All Roles¶
- URL:
/api/authService/role/all - Method:
GET - Description: Retrieves a list of all roles.
- Response:
[ { "id": 1, "name": "admin" } ] - Example Request:
curl -X GET http://localhost:3000/api/authService/role/all - Example Response:
[ { "id": 1, "name": "admin" } ]
Get Role¶
- URL:
/api/authService/role/:roleid - Method:
GET - Description: Retrieves the details of a specific role by its ID.
- Response:
{ "id": 1, "name": "admin" } - Example Request:
curl -X GET http://localhost:3000/api/authService/role/1 - Example Response:
{ "id": 1, "name": "admin" }
Delete Role¶
- URL:
/api/authService/role/:roleid - Method:
DELETE - Description: Deletes a role by its ID.
- Response:
- 200 OK: Role successfully deleted.
- Example Request:
curl -X DELETE http://localhost:3000/api/authService/role/1 - Example Response:
{ "message": "Role deleted successfully" }
Create Roles Table¶
- URL:
/api/authService/role/createTable - Method:
POST - Description: Creates the roles table in the database.
- Response:
- 200 OK: Roles table successfully created.
- Example Request:
curl -X POST http://localhost:3000/api/authService/role/createTable - Example Response:
{ "message": "Roles table created successfully" }
Example Usage¶
Create Role with Axios¶
import axios from 'axios';
axios.post('/api/authService/role', {
name: 'admin',
description: 'Administrator role'
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
Get Role by ID with Axios¶
axios.get('/api/authService/role/1')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
Error Handling¶
- 422 Unprocessable Entity: Invalid data provided (e.g., missing required fields).
- 400 Bad Request: Invalid request or database error.
- 404 Not Found: Role not found in the database.
- 500 Internal Server Error: Server errors, such as issues with database operations.