This API provides CRUD (Create, Read, Update, Delete) operations for managing tasks.
- Node.js: v19.8.1
- MongoDB: v6.0.4
Run the following command to install the dependencies:
npm install
- Copy the
.env.example
file as.env
and configure the necessary environment variables. - If you want to run tests, also copy the
.env.example
file as.env.test
and configure the necessary environment variables for testing.
To run the API, use the following command:
npm run start
Unit tests and e2e tests are included to ensure the proper functioning of the API.
- To run unit tests, use the following command:
npm run test
- To run e2e tests, use the following command:
npm run test:e2e
This documentation provides instructions on how to use Docker to build and run the Tasks API application. It covers building the Docker image, running the image with Docker, and using Docker Compose for more complex setups.
Docker should be installed on your system. If you don't have Docker installed, please follow the Docker installation guide.
To build the Docker image for the Tasks API application, run the following command:
docker build -t tasks-api .
To run the Docker container with the Tasks API image, use the following command:
docker run -p 3000:3000 --network host tasks-api
Note: Using the --network host flag allows the container to consume the database from the host machine. If this is not available, consider using Docker Compose as described below.
If you prefer to use Docker Compose, follow these steps:
Grant necessary permissions to the data directory for persistent storage:
sudo chmod -R 777 /data/db
Uncomment line 6 and comment line 4 in the .env file:
4. MONGODB_CONNECTION_STRING=mongodb://localhost/tasksdb
5. # For docker compose
6. # MONGODB_CONNECTION_STRING=mongodb://mongodb/tasksdb
(Optional) If you encounter permission issues or need to customize the UID and GID, uncomment lines 7 and 8 in the .env file:
7. # UID=1000
8. # GID=1000
Ensure that port '27017:27017' is available on your system or modify the port in line 6 of the docker-compose.yml file.
To run the Tasks API with Docker Compose, execute the following command:
docker compose up
The data model used in the API is as follows:
Property | Type | Required | Generated |
---|---|---|---|
_id | string | Yes | Yes |
title | string | Yes | No |
description | string | No | No |
done | boolean | No | No |
createdAt | string | No | Yes |
updatedAt | string | No | Yes |
__v | number | No | Yes |
Property | Type | Required |
---|---|---|
title | string | Yes |
description | string | No |
done | boolean | No |
Property | Type | Required |
---|---|---|
title | string | No |
description | string | No |
done | boolean | No |
Endpoint | Method | Description | Responses |
---|---|---|---|
/tasks |
GET | Get all tasks | 200 (OK), 404 (Not Found) |
/tasks |
POST | Create a new task | 201 (Created), 409 (Conflict) |
/tasks/{id} |
GET | Get a task by ID | 200 (OK), 404 (Not Found) |
/tasks/{id} |
DELETE | Delete a task by ID | 204 (No Content), 404 (Not Found) |
/tasks/{id} |
PUT | Update a task by ID | 200 (OK), 404 (Not Found) |
-
POST /tasks
-
Description: Creates a new task.
-
Content:
application/json
:- Schema: CreateTaskDto
Example Request Body (POST /tasks):
{ "title": "Task Title", "description": "Task Description", "done": true }
-
-
PUT /tasks:
-
Description: Updates an existing task.
-
Content:
application/json
:- Schema: UpdateTaskDto
Example Request Body (PUT /tasks):
{ "title": "Task Title", "description": "Task Description", "done": true }
-
-
200 (OK)
-
Description: The request was successful.
-
Content:
application/json
:- Schema: Task
Example Response (GET /tasks):
[ { "_id": "60eef305d1e513001e477f20", "title": "Task Title", "description": "Task Description", "done": false, "createdAt": "2021-07-14T10:30:00Z", "updatedAt": "2021-07-14T10:45:00Z", "__v": "0" } ]
Example Response (GET /tasks/{id):
{ "_id": "60eef305d1e513001e477f20", "title": "Task Title", "description": "Task Description", "done": false, "createdAt": "2021-07-14T10:30:00Z", "updatedAt": "2021-07-14T10:45:00Z", "__v": "0" }
-
-
201 (Created)
-
Description: The task was created successfully.
-
Content:
application/json
:- Schema: Task
Example Response (POST /tasks):
{ "_id": "60eef305d1e513001e477f20" "title": "Task Title", "description": "Task Description", "done": true, "createdAt": "2021-07-14T10:30:00Z", "updatedAt": "2021-07-14T10:45:00Z", "__v": "0" }
-
-
204 (No Content)
- Description: The task was deleted successfully.
-
404 (Not Found)
- Description: The requested task was not found.
-
409 (Conflict)
- Description: The task already exists