128 lines
3.7 KiB
Markdown
128 lines
3.7 KiB
Markdown
# FastAPI Todo App
|
|
|
|
This is a simple Todo API application built with FastAPI and SQLite. The application allows you to create, read, update, and delete Todo items.
|
|
|
|
## Features
|
|
|
|
- Create new todo items
|
|
- Retrieve all todo items
|
|
- Retrieve a specific todo item by ID
|
|
- Update todo items
|
|
- Delete todo items
|
|
- Health check endpoint
|
|
|
|
## Tech Stack
|
|
|
|
- **FastAPI**: High-performance web framework
|
|
- **SQLAlchemy**: SQL toolkit and ORM
|
|
- **Alembic**: Database migration tool
|
|
- **SQLite**: Lightweight disk-based database
|
|
- **Pydantic**: Data validation and settings management
|
|
- **Uvicorn**: ASGI server
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── main.py # FastAPI application entry point
|
|
├── migrations/ # Database migrations
|
|
│ ├── env.py # Alembic environment
|
|
│ ├── README # Alembic readme
|
|
│ ├── script.py.mako # Alembic script template
|
|
│ └── versions/ # Migration scripts
|
|
│ └── 001_create_todo_table.py # Initial migration
|
|
├── app/ # Application package
|
|
│ ├── api/ # API endpoints
|
|
│ │ ├── api.py # API router
|
|
│ │ ├── deps.py # Dependencies
|
|
│ │ └── endpoints/ # Endpoint modules
|
|
│ │ └── todos.py # Todo endpoints
|
|
│ ├── core/ # Core modules
|
|
│ │ ├── config.py # Configuration
|
|
│ │ └── exceptions.py # Custom exceptions
|
|
│ ├── crud/ # CRUD operations
|
|
│ │ └── crud_todo.py # Todo CRUD operations
|
|
│ ├── db/ # Database
|
|
│ │ ├── base.py # Base class
|
|
│ │ ├── base_class.py # Import all models
|
|
│ │ └── session.py # Database session
|
|
│ ├── models/ # SQLAlchemy models
|
|
│ │ └── todo.py # Todo model
|
|
│ └── schemas/ # Pydantic schemas
|
|
│ └── todo.py # Todo schemas
|
|
└── requirements.txt # Project dependencies
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository
|
|
2. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Run the application:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
## API Documentation
|
|
|
|
Once the application is running, you can access the API documentation at:
|
|
|
|
- Swagger UI: http://localhost:8000/docs
|
|
- ReDoc: http://localhost:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
- `GET /api/v1/todos/`: Get all todos
|
|
- `POST /api/v1/todos/`: Create a new todo
|
|
- `GET /api/v1/todos/{todo_id}`: Get a specific todo
|
|
- `PUT /api/v1/todos/{todo_id}`: Update a todo
|
|
- `DELETE /api/v1/todos/{todo_id}`: Delete a todo
|
|
- `GET /health`: Health check endpoint
|
|
|
|
## Example Usage
|
|
|
|
### Create a Todo
|
|
|
|
```bash
|
|
curl -X POST "http://localhost:8000/api/v1/todos/" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"title": "Buy groceries", "description": "Milk, eggs, bread", "completed": false}'
|
|
```
|
|
|
|
### Get All Todos
|
|
|
|
```bash
|
|
curl -X GET "http://localhost:8000/api/v1/todos/"
|
|
```
|
|
|
|
### Get a Specific Todo
|
|
|
|
```bash
|
|
curl -X GET "http://localhost:8000/api/v1/todos/1"
|
|
```
|
|
|
|
### Update a Todo
|
|
|
|
```bash
|
|
curl -X PUT "http://localhost:8000/api/v1/todos/1" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"title": "Buy groceries", "description": "Milk, eggs, bread, cheese", "completed": true}'
|
|
```
|
|
|
|
### Delete a Todo
|
|
|
|
```bash
|
|
curl -X DELETE "http://localhost:8000/api/v1/todos/1"
|
|
```
|
|
|
|
### Health Check
|
|
|
|
```bash
|
|
curl -X GET "http://localhost:8000/health"
|
|
``` |