Automated Action d20c3af0bc Build a simple Todo app with FastAPI
- Setup project structure with FastAPI, SQLAlchemy, and SQLite
- Create Todo model and database migrations
- Implement CRUD API endpoints
- Add error handling and validation
- Update README with documentation and examples

generated with BackendIM... (backend.im)
2025-05-15 22:01:40 +00:00

143 lines
3.1 KiB
Markdown

# Simple Todo Application
A simple RESTful API for managing todos built with FastAPI, SQLAlchemy, and SQLite.
## Features
- Create, Read, Update, Delete todo items
- Filter todos by completion status
- Proper error handling and validation
- API documentation via Swagger UI
- SQLite database for persistence
- Database migrations using Alembic
## Project Structure
```
simpletodoapplication/
├── alembic/ # Database migration scripts
├── app/ # Application package
│ ├── api/ # API endpoints
│ │ └── routers/ # API routers
│ ├── core/ # Core functionality
│ ├── db/ # Database session and connection
│ ├── models/ # SQLAlchemy models
│ └── schemas/ # Pydantic schemas
├── alembic.ini # Alembic configuration
├── main.py # Application entry point
└── requirements.txt # Project dependencies
```
## Setup
1. Clone the repository:
```bash
git clone <repository-url>
cd simpletodoapplication
```
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. Apply database migrations:
```bash
alembic upgrade head
```
4. Run the application:
```bash
uvicorn main:app --reload
```
The API server will be running at http://localhost:8000.
## API Documentation
Once the server is running, you can access the auto-generated API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
## API Endpoints
- `GET /api/v1/todos`: Get all todos
- Query Parameters:
- `skip`: Number of items to skip (default: 0)
- `limit`: Maximum number of items to return (default: 100)
- `completed`: Filter by completion status (boolean, optional)
- `POST /api/v1/todos`: Create a new todo
- Request Body:
- `title`: String (required)
- `description`: String (optional)
- `completed`: Boolean (default: false)
- `GET /api/v1/todos/{todo_id}`: Get a specific todo by ID
- `PUT /api/v1/todos/{todo_id}`: Update a todo
- Request Body (all fields optional):
- `title`: String
- `description`: String
- `completed`: Boolean
- `DELETE /api/v1/todos/{todo_id}`: Delete a todo
- `GET /health`: Health check endpoint
## Example Requests
### Create a Todo
```bash
curl -X 'POST' \
'http://localhost:8000/api/v1/todos/' \
-H 'accept: application/json' \
-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/' \
-H 'accept: application/json'
```
### Get Only Completed Todos
```bash
curl -X 'GET' \
'http://localhost:8000/api/v1/todos/?completed=true' \
-H 'accept: application/json'
```
### Update a Todo
```bash
curl -X 'PUT' \
'http://localhost:8000/api/v1/todos/1' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"completed": true
}'
```
### Delete a Todo
```bash
curl -X 'DELETE' \
'http://localhost:8000/api/v1/todos/1' \
-H 'accept: application/json'
```