Skip to content

yan428v/todo-server

Repository files navigation

Todo Server API

A minimal backend API for task management (TODO application) built with NestJS, TypeORM, and SQLite.

Features

  • ✅ Create tasks with text and status
  • ✅ Update task status (in progress → completed)
  • ✅ Delete tasks
  • ✅ Get all tasks with optional status filtering
  • ✅ Get single task by ID
  • ✅ SQLite database storage
  • ✅ Docker containerization
  • ✅ Swagger API documentation
  • ✅ Input validation and error handling

Technologies Used

  • Backend: NestJS (Node.js framework)
  • Database: SQLite with TypeORM
  • Validation: class-validator, class-transformer
  • Documentation: Swagger/OpenAPI
  • Containerization: Docker

Quick Start

Prerequisites

  • Docker installed on your machine

Running with Docker

  1. Clone the repository
  2. Build and run the Docker container:
docker build -t todo-server .
docker run --name todo-server -p 3000:3000 todo-server

The API will be available at http://localhost:3000

Running Locally

  1. Install dependencies:
npm install
  1. Start the development server:
npm run start:dev
  1. For production build:
npm run build
npm run start:prod

API Endpoints

Base URL: http://localhost:3000/api

Method Endpoint Description
GET /tasks Get all tasks (with optional status filter)
GET /tasks/:id Get task by ID
POST /tasks Create a new task
PATCH /tasks/:id/status Update task status
DELETE /tasks/:id Delete task

API Examples

Create a Task

POST /api/tasks
Content-Type: application/json

{
  "text": "Complete the project documentation",
  "status": "in_progress"  // optional, defaults to "in_progress"
}

Response:

{
  "id": 1,
  "text": "Complete the project documentation",
  "status": "in_progress",
  "createdAt": "2024-01-15T10:30:00Z",
  "updatedAt": "2024-01-15T10:30:00Z"
}

Get All Tasks

GET /api/tasks

With status filter:

GET /api/tasks?status=completed

Response:

[
  {
    "id": 1,
    "text": "Complete the project documentation",
    "status": "in_progress",
    "createdAt": "2024-01-15T10:30:00Z",
    "updatedAt": "2024-01-15T10:30:00Z"
  }
]

Get Task by ID

GET /api/tasks/1

Update Task Status

PATCH /api/tasks/1/status
Content-Type: application/json

{
  "status": "completed"
}

Delete Task

DELETE /api/tasks/1

Task Status Values

  • in_progress - Task is currently being worked on
  • completed - Task has been finished

API Documentation

Once the server is running, you can access the interactive Swagger documentation at: http://localhost:3000/api/docs

Database

The application uses SQLite database stored in the db/todo.db file. The database is automatically created when the application starts.

Task Schema

CREATE TABLE tasks (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  text VARCHAR(500) NOT NULL,
  status VARCHAR(20) DEFAULT 'in_progress',
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
);

Error Handling

The API provides comprehensive error handling with appropriate HTTP status codes:

  • 400 Bad Request - Invalid input data or parameters
  • 404 Not Found - Task not found
  • 500 Internal Server Error - Server errors

All error responses follow a consistent format:

{
  "statusCode": 400,
  "message": "Validation failed",
  "error": "Bad Request"
}

Development

Available Scripts

  • npm run start - Start the application
  • npm run start:dev - Start in development mode with hot reload
  • npm run start:prod - Start in production mode
  • npm run build - Build the application
  • npm run test - Run tests
  • npm run lint - Run ESLint
  • npm run format - Format code with Prettier

Project Structure

src/
├── config/           # Configuration files
├── modules/
│   └── tasks/       # Task module
│       ├── dto/     # Data Transfer Objects
│       ├── entities/ # Database entities
│       ├── types/   # TypeScript types
│       ├── tasks.controller.ts
│       ├── tasks.service.ts
│       └── tasks.module.ts
├── app.module.ts    # Root module
└── main.ts         # Application entry point

About

A minimal backend API for task management (TODO application)

Topics

Resources

Stars

Watchers

Forks

Contributors