Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["manushamadubhashini"]
}
196 changes: 196 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# Build a Simple Task Management API

A simple RESTful API for managing tasks built with Node.js, Express.js, and MongoDB.

## Features

- Create, read, update and delete tasks
- Input validation with express-validator
- MongoDB database integration
- Proper error handling and status codes
- Postman collection included

## Tech Stack

- **Backend**: Node.js, Express.js
- **Database**: MongoDB, Mongoose
- **Validation**: Express Validator
- **Environment**: dotenv

## Prerequisites

- Node.js (v14+)
- MongoDB
- Postman

## Installation

1. **Clone the repository**
```bash
git clone git@github.com:manushamadubhashini/task-api.git
cd task-api
```

2. **Install dependencies**
```bash
npm install express mongoose express-validator dotenv
```

3. **Create `.env` file**
```env
PORT=3000
MONGODB_URI=mongodb://localhost:27017/taskmanagement
NODE_ENV=development
```


5. **Run the application**
```bash
npm run dev
```

Server will run at `http://localhost:3000`

## Project Structure

```
task-api/
├── postman/
│ └── Task Management.postman_collection.json
├── src/
│ ├── config/
│ │ └── dbConnection.js
│ ├── controllers/
│ │ └── taskController.js
│ ├── dto/
│ │ └── taskDTO.js
│ ├── middleware/
│ │ ├── errorHandler.js
│ │ └── taskValidator.js
│ ├── models/
│ │ └── taskModel.js
│ ├── routes/
│ │ └── taskRoute.js
│ ├── services/
│ │ └── taskService.js
│ ├── utils/
│ │ └── customError.js
│ ├── app.js
│ └── index.js
├── .env
├── .gitignore
├── package.json
└── README.md
```

## API Endpoints

Base URL: `http://localhost:3000/api/v1/tasks`

### 1. Create Task
**POST** `/create`

Request:
```json
{
"title" : "Task 5",
"description" : "This is task 5",
"status" : "pending"
}
```

Response (201):
```json
{
"message": "Task created successfully",
"data": {
"id": "T005",
"title": "Task 5",
"description": "This is task 5",
"status": "pending",
"_id": "6909c995b7a10bcb07015c3f",
"createdAt": "2025-11-04T09:38:29.632Z",
"updatedAt": "2025-11-04T09:38:29.632Z",
"__v": 0
}
}
```

### 2. Get All Tasks
**GET** `/getAll`

Response (200):
```json
{
"message": "Tasks fetched successfully",
"data": [
{
"_id": "6909c995b7a10bcb07015c3f",
"id": "T005",
"title": "Task 5",
"description": "This is task 5",
"status": "pending",
"createdAt": "2025-11-04T09:38:29.632Z",
"updatedAt": "2025-11-04T09:38:29.632Z",
"__v": 0
}
]
}
```

### 3. Get Task by ID
**GET** `/get/:id`

Response (200):
```json
{
"message": "Task fetched successfully",
"data": {
"_id": "6909c995b7a10bcb07015c3f",
"id": "T005",
"title": "Task 5",
"description": "This is task 5",
"status": "pending",
"createdAt": "2025-11-04T09:38:29.632Z",
"updatedAt": "2025-11-04T09:38:29.632Z",
"__v": 0
}
}
```

### 4. Delete Task
**DELETE** `/delete/:id`

Response (200):
```json
{
"message": "Task deleted successfully",
"data": {
"_id": "69098178f4cdd31945052ef5",
"id": "T002",
"title": "task3",
"description": "this is a task 3",
"status": "pending",
"createdAt": "2025-11-04T04:30:48.107Z",
"updatedAt": "2025-11-04T06:45:04.058Z",
"__v": 0
}
}
```

## Testing with Postman

1. Open Postman
2. Click **Import**
3. Select `postman/Task Management.postman_collection.json`
4. Run the requests

## Troubleshooting

**MongoDB Connection Error**
- Ensure MongoDB is running: `mongod`
- Check `MONGODB_URI` in `.env`

## Author

Manusha Madubhashini
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "task-api",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.js"
},
"keywords": [],
"author": "",
Expand Down
119 changes: 119 additions & 0 deletions postman/Task Management.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"info": {
"_postman_id": "4560299f-17ae-44f4-9a79-3f0975ee760e",
"name": "Task Management",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "40188628"
},
"item": [
{
"name": "New Request",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"title\" : \"task3\",\r\n \"description\" : \"this is task 3\",\r\n \"status\" : \"pending\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3000/api/v1/tasks/create",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"api",
"v1",
"tasks",
"create"
]
}
},
"response": []
},
{
"name": "New Request",
"request": {
"auth": {
"type": "bearer"
},
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:3000/api/v1/tasks/delete/T001",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"api",
"v1",
"tasks",
"delete",
"T001"
]
}
},
"response": []
},
{
"name": "New Request",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "New Request",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"title\" : \"task3\",\r\n \"description\" : \"this is a task 3\",\r\n \"status\" : \"pending\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3000/api/v1/tasks/update/T002",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"api",
"v1",
"tasks",
"update",
"T002"
]
}
},
"response": []
},
{
"name": "New Request",
"request": {
"method": "GET",
"header": []
},
"response": []
}
]
}
17 changes: 17 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from "express";
import { taskRouter } from "./routes/taskRoute.js";
import { ErrorHandler } from "./middleware/errorhandler.js";

const app = express();

// middleware
app.use(express.json());

// routes
app.use("/api/v1/tasks" , taskRouter)

// Error handling middleware
app.use(ErrorHandler);


export default app;
5 changes: 0 additions & 5 deletions src/controller.js

This file was deleted.

Loading