An open API service indexing awesome lists of open source software.

https://github.com/devrapture/todo-list-api

Todo List API
https://github.com/devrapture/todo-list-api

Last synced: 9 months ago
JSON representation

Todo List API

Awesome Lists containing this project

README

          

# 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](https://nestjs.com/) - Progressive Node.js framework
- **Database**: PostgreSQL with [Prisma](https://www.prisma.io/) 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

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

### 2. Install dependencies

```bash
pnpm install
```

### 3. Set up environment variables

Create a `.env` file in the root directory:

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

### 4. Start the database

```bash
# Start PostgreSQL with Docker
pnpm db:dev:up

# Apply database migrations
pnpm prisma:dev:deploy
```

### 5. Run the application

```bash
# 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 `

| 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
```bash
curl -X POST http://localhost:3000/api/v1/auth/signup \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123"
}'
```

#### 2. Login
```bash
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)
```bash
curl -X POST http://localhost:3000/api/v1/todos \
-H "Content-Type: application/json" \
-H "Authorization: Bearer " \
-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
```bash
# 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
```bash
# Start PostgreSQL
docker-compose up postgres -d

# Start the application
pnpm start:dev
```

### Reset development database
```bash
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](http://localhost:3000/api/v1/docs) when the server is running
2. Review the test files for usage examples
3. Open an issue on GitHub

---

Built with โค๏ธ using NestJS