161 lines
3.7 KiB
Markdown
161 lines
3.7 KiB
Markdown
# Todo List API
|
|
|
|
A simple Todo List API built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- Create, read, update, and delete todo items
|
|
- Filter todo items by completion status
|
|
- Pagination support
|
|
- SQLite database with SQLAlchemy ORM
|
|
- Database migrations with Alembic
|
|
- Health check endpoint
|
|
- OpenAPI documentation
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
todo-list-api/
|
|
├── alembic.ini # Alembic configuration
|
|
├── app/ # Main application package
|
|
│ ├── api/ # API endpoints
|
|
│ │ └── routes/ # API route handlers
|
|
│ │ ├── health.py # Health check endpoint
|
|
│ │ └── todo.py # Todo endpoints
|
|
│ ├── core/ # Core application code
|
|
│ │ └── config.py # Application configuration
|
|
│ ├── crud/ # CRUD operations
|
|
│ │ └── todo.py # Todo CRUD operations
|
|
│ ├── db/ # Database related code
|
|
│ │ └── session.py # Database session setup
|
|
│ ├── models/ # SQLAlchemy models
|
|
│ │ └── todo.py # Todo model
|
|
│ └── schemas/ # Pydantic schemas
|
|
│ └── todo.py # Todo schemas
|
|
├── main.py # Application entry point
|
|
├── migrations/ # Alembic migrations
|
|
│ ├── env.py # Alembic environment configuration
|
|
│ ├── script.py.mako # Migration script template
|
|
│ └── versions/ # Migration script versions
|
|
│ └── 001_create_todos_table.py # Initial migration
|
|
└── requirements.txt # Python dependencies
|
|
```
|
|
|
|
## Requirements
|
|
|
|
- Python 3.9+
|
|
- FastAPI
|
|
- SQLAlchemy
|
|
- Pydantic
|
|
- Alembic
|
|
- Uvicorn
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone https://github.com/yourusername/todo-list-api.git
|
|
cd todo-list-api
|
|
```
|
|
|
|
2. Install the required dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Run database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the application with:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at `http://localhost:8000`.
|
|
|
|
## API Documentation
|
|
|
|
After starting the application, you can access the interactive API documentation:
|
|
|
|
- Swagger UI: `http://localhost:8000/docs`
|
|
- ReDoc: `http://localhost:8000/redoc`
|
|
|
|
## API Endpoints
|
|
|
|
### Health Check
|
|
|
|
- `GET /api/health`: Check if the API is healthy
|
|
|
|
### Todo Operations
|
|
|
|
- `GET /api/todos`: Get a list of todo items
|
|
- Query parameters:
|
|
- `skip`: Number of items to skip (pagination)
|
|
- `limit`: Maximum number of items to return (pagination)
|
|
- `completed`: Filter by completion status (optional)
|
|
|
|
- `POST /api/todos`: Create a new todo item
|
|
- Request body: `TodoCreate` schema
|
|
|
|
- `GET /api/todos/{todo_id}`: Get a specific todo item by ID
|
|
|
|
- `PUT /api/todos/{todo_id}`: Update a specific todo item by ID
|
|
- Request body: `TodoUpdate` schema
|
|
|
|
- `DELETE /api/todos/{todo_id}`: Delete a specific todo item by ID
|
|
|
|
## Examples
|
|
|
|
### Create a Todo
|
|
|
|
```bash
|
|
curl -X 'POST' \
|
|
'http://localhost:8000/api/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/todos'
|
|
```
|
|
|
|
### Get Completed Todos
|
|
|
|
```bash
|
|
curl -X 'GET' 'http://localhost:8000/api/todos?completed=true'
|
|
```
|
|
|
|
### Update a Todo
|
|
|
|
```bash
|
|
curl -X 'PUT' \
|
|
'http://localhost:8000/api/todos/1' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{
|
|
"completed": true
|
|
}'
|
|
```
|
|
|
|
### Delete a Todo
|
|
|
|
```bash
|
|
curl -X 'DELETE' 'http://localhost:8000/api/todos/1'
|
|
```
|
|
|
|
## License
|
|
|
|
MIT |