Skip to content

devrapture/todo-list-api

Repository files navigation

Todo List API

A robust and scalable REST API for managing todo tasks, built with NestJS, PostgreSQL, and Prisma. Features user authentication, JWT authorization, and comprehensive CRUD operations for todo management.

πŸš€ Features

  • User Authentication: Secure signup and signin with JWT tokens
  • Todo Management: Full CRUD operations for todo tasks
  • User-specific Todos: Each user can only access their own todos
  • Task Filtering: Filter todos by completion status
  • Due Date Support: Optional due dates for tasks
  • Database: PostgreSQL with Prisma ORM
  • API Documentation: Swagger/OpenAPI documentation
  • Testing: Comprehensive unit and e2e tests
  • Docker Support: Easy development setup with Docker Compose

πŸ› οΈ Tech Stack

  • Framework: NestJS - Progressive Node.js framework
  • Database: PostgreSQL with Prisma ORM
  • Authentication: JWT with Passport.js
  • Password Hashing: Argon2
  • Validation: Class-validator and class-transformer
  • Documentation: Swagger/OpenAPI
  • Testing: Jest
  • Package Manager: pnpm

πŸ“‹ Prerequisites

  • Node.js (v18 or higher)
  • pnpm
  • Docker and Docker Compose
  • PostgreSQL (or use Docker)

πŸš€ Quick Start

1. Clone the repository

git clone https://github.com/devrapture/todo-list-api
cd todo-list-api

2. Install dependencies

pnpm install

3. Set up environment variables

Create a .env file in the root directory:

DATABASE_URL="postgresql://postgres:postgres@localhost:5432/todo_db?schema=public"
JWT_SECRET="your-super-secret-jwt-key"
PORT=3000

4. Start the database

# Start PostgreSQL with Docker
pnpm db:dev:up

# Apply database migrations
pnpm prisma:dev:deploy

5. Run the application

# Development mode
pnpm start:dev

# Production mode
pnpm start:prod

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

πŸ“š API Documentation

Once the application is running, you can access the interactive API documentation at:

Swagger UI: http://localhost:3000/api/v1/docs

Authentication Endpoints

Method Endpoint Description
POST /api/v1/auth/signup Register a new user
POST /api/v1/auth/signin Login user

Todo Endpoints

All todo endpoints require JWT authentication. Include the token in the Authorization header: Bearer <your-jwt-token>

Method Endpoint Description
GET /api/v1/todos Get all todos (with optional filtering)
GET /api/v1/todos/:id Get a specific todo
POST /api/v1/todos Create a new todo
PATCH /api/v1/todos/:id Update a todo
DELETE /api/v1/todos/:id Delete a specific todo
DELETE /api/v1/todos Delete all todos

Example Usage

1. Register a new user

curl -X POST http://localhost:3000/api/v1/auth/signup \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

2. Login

curl -X POST http://localhost:3000/api/v1/auth/signin \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

3. Create a todo (with JWT token)

curl -X POST http://localhost:3000/api/v1/todos \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-jwt-token>" \
  -d '{
    "todo": "Buy groceries",
    "dueDate": "2024-01-15T10:00:00Z"
  }'

πŸ—„οΈ Database Schema

User Model

  • id: UUID (Primary Key)
  • email: String (Unique)
  • password: String (Hashed with Argon2)
  • createdAt: DateTime
  • updatedAt: DateTime

Todos Model

  • id: UUID (Primary Key)
  • todo: String (Task description)
  • completed: Boolean (Default: false)
  • dueDate: DateTime (Optional)
  • userId: String (Foreign Key to User)
  • createdAt: DateTime
  • updatedAt: DateTime

πŸ§ͺ Testing

Run all tests

# Unit tests
pnpm test

# E2E tests
pnpm test:e2e

# Test coverage
pnpm test:cov

Test database setup

The project includes separate test database configuration:

  • Test database runs on port 5433
  • Automatically managed by test scripts
  • Isolated from development database

🐳 Docker

Development with Docker

# Start PostgreSQL
docker-compose up postgres -d

# Start the application
pnpm start:dev

Reset development database

pnpm db:dev:restart

πŸ“ Available Scripts

Script Description
pnpm start Start the application
pnpm start:dev Start in development mode with hot reload
pnpm start:debug Start in debug mode
pnpm start:prod Start in production mode
pnpm build Build the application
pnpm test Run unit tests
pnpm test:e2e Run e2e tests
pnpm test:cov Run tests with coverage
pnpm lint Run ESLint
pnpm format Format code with Prettier
pnpm db:dev:up Start development database
pnpm db:dev:restart Restart development database
pnpm prisma:generate Generate Prisma client

πŸ”§ Environment Variables

Variable Description Default
DATABASE_URL PostgreSQL connection string Required
JWT_SECRET Secret key for JWT tokens Required
PORT Application port 3000

πŸ“ Project Structure

src/
β”œβ”€β”€ auth/                 # Authentication module
β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”œβ”€β”€ dto/
β”‚   β”œβ”€β”€ guard/
β”‚   └── strategy/
β”œβ”€β”€ todos/               # Todo management module
β”‚   β”œβ”€β”€ todos.controller.ts
β”‚   β”œβ”€β”€ todos.service.ts
β”‚   └── dto/
β”œβ”€β”€ prisma/             # Database configuration
β”‚   β”œβ”€β”€ prisma.service.ts
β”‚   └── prisma.module.ts
β”œβ”€β”€ app.module.ts       # Root module
└── main.ts            # Application entry point

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ†˜ Support

If you encounter any issues or have questions:

  1. Check the API documentation when the server is running
  2. Review the test files for usage examples
  3. Open an issue on GitHub

Built with ❀️ using NestJS

About

Todo List API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors