DocGenie API Documentation
Introduction
Docgenie API contains several end-points that allows users to create, edit, retrieve and delete documents. It also sets access privileges so only authorized users can perform certain operations. That is, only users in a particular role can view role-access documents, while only the creator of a document has the right to delete or edit it.
Development
The application was developed with NodeJs and Express is used for routing. The Postgres database was used with sequelize as the ORM.
Installation
Follow the steps below to setup a local development environment. First ensure you have Postgresql installed, and a version of Node.js equal or greater than v6.8.0.
- Clone the repository to your local machine:
git clone https://github.com/andela-mharuna/DocGenie.git. - Cd into the project directory:
cd DocGenie - Rename
.env_exampleto.envand add the required environment variables. - Install project dependencies
npm install - Start the express server in development mode:
npm run start:dev.
API Summary
Users
| EndPoint | Functionality |
|---|---|
| POST /auth/api/v1/users/login | Logs in a user. |
| POST /api/v1/users/logout | Logs out a user. |
| POST /auth/api/v1/users | Creates a new user(Sign up). |
| GET /api/v1/users | Gets all registered users (available only to the Admin). |
| GET /api/v1/users/:id | Finds a particular user by his/her id. |
| PUT /api/v1/users/:id | Updates a user's attributes based on the id specified (available only to the profile owner or admin). |
| DELETE /api/v1/users/:id | Deletes a user (available only to the profile owner). |
| GET /api/v1/users/:id/documents | Gets all documents belonging to a particular user. |
Documents
| EndPoint | Functionality |
|---|---|
| POST /api/v1/documents | Creates a new document. |
| GET /api/v1/documents | Gets all documents. |
| GET /api/v1/documents/:id | Find a particular document by it's id. |
| PUT /api/v1/documents/:id | Updates a document attributes. (available only to the owner) |
| DELETE /api/v1/documents/:id | Delete a particular document. (available only to the document creator) |
| GET /api/v1/search/documents/?searchKey=${query} | Get all documents with title or content containing the search query |
Roles (available only to the Admin)
| EndPoint | Functionality |
|---|---|
| GET /api/v1/roles | Get all created Roles. |
| POST /api/v1/roles | Create a new Role. |
| DELETE /api/v1/roles/:id | Delete a Role. |
Users
List All Users
Sample JSON structured response:
{
"users": [
{
"id": 12,
"username": "Tolu",
"fullname": "Tolu Afobs",
"roleId": 2,
"email": "tolu@gmail.com"
},
{
"id": 11,
"username": "biodun",
"fullname": "biodun",
"roleId": 2,
"email": "biodun@gmail.com"
},
{
"id": 8,
"username": "Rob",
"fullname": "Robert Ludlum",
"roleId": 2,
"email": "robert@robert.com"
},
{
"id": 7,
"username": "Johnie",
"fullname": "John Grisham",
"roleId": 2,
"email": "john@grisham.com"
},
{
"id": 6,
"username": "Dorian",
"fullname": "Dorian Gray",
"roleId": 2,
"email": "dorian@gray.com"
},
{
"id": 5,
"username": "Max",
"fullname": "Gruocho",
"roleId": 2,
"email": "max@max.com"
}
],
"pagination": {
"totalCount": 9,
"pages": 2,
"currentPage": 1,
"pageSize": 6
}
}
Request
- Endpoint: GET:
/api/v1/users - Requires: Authentication
Query Parameters
| Parameter | Default | Description |
|---|---|---|
| limit | 6 | Sets the limit. |
| offset | 0 | Sets the offset. |
Response
- Status:
200: OK - Body:
(application/json)
Create User
Request:
{
"username": "doro",
"fullname": "doro bucci",
"email": "doro@bucci.com",
"password": "dorobucci",
"confirmPassword": "dorobucci",
}
Sample JSON structured response:
{
"message": "Signed up successfully",
"user": {
"id": 5,
"username": "doro",
"fullname": "doro bucci",
"roleId": 2,
"email": "doro@bucci.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInJvbGVJZCI6MSwiaWF0IjoxNTAyMTAwMzA5LCJleHAiOjE1MDIxODY3MDl9.vzMK6Mr7cLr7rk5mXF5KeRXlJn913XBJXQjaOGJEiXo"
}
Request
- Endpoint: POST:
/auth/api/v1/users - Body:
(application/json)
Response
- Status:
201: Created - Body:
(application/json)
Get User
Sample JSON structured response:
{
"id": 4,
"username": "Mikail",
"fullname": "Mikail Stanislaski",
"roleId": 2,
"email": "mikahil.stanislaski@gmail.com",
}
Request
- Endpoint: GET:
/api/v1/users/:id
Response
- Status:
200: OK - Body:
(application/json)
Edit User
Request:
{
"username": "mikhy",
}
Sample JSON structured response:
{
"id": 4,
"username": "mikhy",
"fullname": "Mikail Stanislaski",
"email": "mikahil.stanislaski@gmail.com",
"roleId": 2,
}
Request
- Endpoint: PUT:
/api/v1/users/:id - Body:
(application/json)
Response
- Status:
200: OK - Body:
(application/json)
Delete User
Sample JSON structured response:
{
"message": "User deleted successfully"
}
Request
- Endpoint: DELETE:
/api/v1/users/:id - Requires: Authentication
Response
- Status:
200: OK - Body:
(application/json)
Login User
Request:
{
"email": "tolu@gmail.com",
"password": "afolabi"
}
Sample JSON structured response:
{
"message": "Signed in successfully",
"user": {
"id": 1,
"username": "tolu",
"fullname": "tolu afobs",
"roleId": 1,
"email": "tolu@gmail.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInJvbGVJZCI6MSwiaWF0IjoxNTAyMTAwMzA5LCJleHAiOjE1MDIxODY3MDl9.vzMK6Mr7cLr7rk5mXF5KeRXlJn913XBJXQjaOGJEiXo"
}
Request
- Endpoint: POST:
/auth/api/v1/users/login
Response
- Status:
200: OK - Body:
(application/json)
Logout User
Sample JSON structured response:
{
"message": "Successfully logged out"
}
Request
- Endpoint: POST:
/api/v1/users/logout
Response
- Status:
200: OK - Body:
(application/json)
List User Documents
Sample JSON structured response:
[
{
"id": 1,
"title": "Hello",
"content": "It's me!",
"access": 0,
"userId": 1,
"createdAt": "2017-05-28T22:19:08.870Z",
"updatedAt": "2017-05-28T22:58:17.249Z",
"user": {
"username": "admin",
"roleId": 1,
"email": "admin@gmail.com",
"fullname": "admin"
}
}
]
Request
- Endpoint: GET:
/api/v1/users/:id/documents - Requires: Authentication
Response
- Status:
200: OK - Body:
(application/json)
Search Users
Sample JSON structured response:
{
"users": [
{
"id": 12,
"username": "Tolu",
"fullname": "Tolu Afobs",
"roleId": 2,
"email": "tolu@gmail.com"
},
{
"id": 11,
"username": "biodun",
"fullname": "biodun",
"roleId": 2,
"email": "biodun@gmail.com"
},
{
"id": 8,
"username": "Rob",
"fullname": "Robert Ludlum",
"roleId": 2,
"email": "robert@robert.com"
},
],
"pagination": {
"totalCount": 9,
"pages": 2,
"currentPage": 1,
"pageSize": 6
}
}
Request
- Endpoint: GET:
/api/v1/search/users/ - Requires: Authentication
Query Parameters
| Parameter | Default | Description |
|---|---|---|
| searchKey | "" | Search query. |
Response
- Status:
200: OK - Body:
(application/json)
Documents
List All Documents
Sample JSON structured response:
{
"documents": [
{
"id": 15,
"userId": 1,
"title": "Biodun's Dilemma",
"access": 0,
"content": "So, what will I eat for breakfast now that there's an election going on????",
"createdAt": "2017-07-22T06:53:06.520Z",
"updatedAt": "2017-07-22T06:53:06.520Z",
"user": {
"id": 1,
"username": "admin",
"roleId": 1,
"fullname": "admin",
"email": "admin@gmail.com"
}
},
{
"id": 14,
"userId": 1,
"title": "Sunday",
"access": 1,
"content": "First document edited",
"createdAt": "2017-07-22T06:50:11.150Z",
"updatedAt": "2017-07-23T15:03:56.047Z",
"user": {
"id": 1,
"username": "admin",
"roleId": 1,
"fullname": "admin",
"email": "admin@gmail.com"
}
},
{
"id": 12,
"userId": 4,
"title": "Nath's role document",
"access": 2,
"content": "Role document by nathan!!",
"createdAt": "2017-07-21T12:58:33.691Z",
"updatedAt": "2017-07-21T12:58:33.691Z",
"user": {
"id": 4,
"username": "nathan",
"roleId": 2,
"fullname": "nathan",
"email": "nathan@gmail.com"
}
},
],
"pagination": {
"totalCount": 7,
"pages": 2,
"currentPage": 1,
"pageSize": 6
}
}
Request
- Endpoint: GET:
/api/v1/documents
Query Parameters
| Parameter | Default | Description |
|---|---|---|
| limit | 6 | Sets the limit. |
| offset | 0 | Sets the offset. |
Response
- Status:
200: OK - Body:
(application/json)
Create Document
Request:
{
"title": "Chester",
"content": "I'm so high, I can hear heaven. Oh, but Heaven can't hear me",
"access": 2
}
Sample JSON structured response:
{
"id": 3,
"title": "Hello",
"content": "Hello, It's me",
"access": 0,
"userId": 1,
"createdAt": "2017-05-28T22:43:37.097Z",
"updatedAt": "2017-05-28T22:43:37.097Z",
"user": {
"id": 2,
"username": "admin",
"fullname": "admin",
"email": "admin",
"roleId": 1
},
}
Request
- Endpoint: POST:
/api/v1/documents - Requires: Authentication
- Body:
(application/json)
Response
- Status:
201: Created - Body:
(application/json)
Get Document
Sample JSON structured response:
{
"id": 6,
"title": "Hello",
"content": `Hello, It's me`,
"access": 2,
"userId": 3,
"createdAt": "2017-05-28T22:19:08.870Z",
"updatedAt": "2017-05-28T22:19:08.871Z",
"user": {
"id": 2,
"username": "funms",
"fullname": "funmi",
"email": "funms@gmail.com",
"roleId": 3
}
}
Request
- Endpoint: GET:
/api/v1/documents/:id
Response
- Status:
200: OK - Body:
(application/json)
Edit Document
Request:
{
"content": "What's new?"
}
Sample JSON structured response:
{
"id": 3,
"title": "Hello",
"content": "What's new?",
"access": 0,
"userId": 1,
"createdAt": "2017-05-28T22:43:37.097Z",
"updatedAt": "2017-05-28T22:43:37.097Z",
"user": {
"id": 2,
"username": "admin",
"fullname": "admin",
"email": "admin@gmail.com",
"roleId": 1
},
}
Request
- Endpoint: PUT:
/api/v1/documents/:id - Requires: Authentication
- Body:
(application/json)
Response
- Status:
200: OK - Body:
(application/json)
Delete Document
Sample JSON structured response:
{
"message": "Document deleted successfully"
}
Request
- Endpoint: DELETE:
/api/v1/documents/:id - Requires: Authentication
Response
- Status:
200: OK - Body:
(application/json)
Search Documents
Sample JSON structured response:
{
"documents": [
{
"id": 2,
"title": "Saruman's Words",
"content": `The hour is late and Gandalf the Grey comes to
Isengard seeking my counsel...`,
"access": -1,
"userId": 2,
"createdAt": "2017-05-28T22:19:08.871Z",
"updatedAt": "2017-05-28T22:19:08.871Z",
"user": {
"id": 2,
"username": "elf",
"fullname": "arwen",
"email": "arwen@aragon.com",
"roleId": 2
}
},
{
"id": 1,
"title": "Glory",
"content": `When the glory comes, it will be ours, oh it will be ours,
One day, when the war is won, we will be sure...`,
"access": 0,
"userId": 1,
"createdAt": "2017-05-28T22:19:08.870Z",
"updatedAt": "2017-05-28T22:19:08.871Z",
"user": {
"id": 3,
"username": "sara",
"fullname": "sara",
"email": "sara@gmail.com",
"roleId": 1
}
}
],
"pagination": {
"totalCount": 9,
"pages": 2,
"currentPage": 1,
"pageSize": 6
}
}
Request
- Endpoint: GET:
/api/v1/search/documents/ - Requires: Authentication
Query Parameters
| Parameter | Default | Description |
|---|---|---|
| searchKey | "" | Search query. |
Response
- Status:
200: OK - Body:
(application/json)
Roles
List All Roles
Sample JSON structured response:
[
{
"id": 1,
"title": "admin"
},
{
"id": 2,
"title": "user"
}
]
Request
- Endpoint: GET:
/api/v1/roles - Requires: Authentication
Response
- Status:
200: OK - Body:
(application/json)
Create Role
Request:
{
"title": "guest",
}
Sample JSON structured response:
{
"id": 3,
"title": "guest",
}
Request
- Endpoint: POST:
/api/v1/roles - Requires: Authentication
- Body:
(application/json)
Response
- Status:
201: Created - Body:
(application/json)
Delete Role
Sample JSON structured response:
{
"message": "Role deleted successfully"
}
Request
- Endpoint: DELETE:
/api/v1/roles/:id - Requires: Authentication
Response
- Status:
200: OK - Body:
(application/json)