Implement complete Todo API with FastAPI
- Set up FastAPI application with CORS support - Created SQLAlchemy models and database session management - Implemented CRUD endpoints for todos with proper validation - Added Alembic migrations for database schema - Included health check and base information endpoints - Added comprehensive README with API documentation - Configured Ruff for code quality and linting
This commit is contained in:
parent
29d1f9d0d1
commit
a4fb722622
176
README.md
176
README.md
@ -1,89 +1,145 @@
|
|||||||
# Todo API
|
# Todo App API
|
||||||
|
|
||||||
A simple Todo API built with FastAPI, SQLAlchemy, and SQLite.
|
A simple Todo application API built with FastAPI and SQLite.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Full CRUD operations for todos
|
- ✅ Create, read, update, and delete todos
|
||||||
- RESTful API design
|
- ✅ RESTful API design
|
||||||
- Automatic API documentation with Swagger/OpenAPI
|
- ✅ SQLite database with SQLAlchemy ORM
|
||||||
- SQLite database with SQLAlchemy ORM
|
- ✅ Database migrations with Alembic
|
||||||
- Database migrations with Alembic
|
- ✅ API documentation with Swagger UI
|
||||||
- CORS enabled for all origins
|
- ✅ CORS support for frontend integration
|
||||||
- Health check endpoint
|
- ✅ Health check endpoint
|
||||||
|
- ✅ Pydantic schemas for data validation
|
||||||
|
|
||||||
## API Endpoints
|
## API Endpoints
|
||||||
|
|
||||||
### Base Endpoints
|
### Base Endpoints
|
||||||
|
- `GET /` - API information and links
|
||||||
- `GET /` - Root endpoint with API information
|
|
||||||
- `GET /health` - Health check endpoint
|
- `GET /health` - Health check endpoint
|
||||||
- `GET /docs` - Swagger UI documentation
|
|
||||||
- `GET /redoc` - ReDoc documentation
|
|
||||||
|
|
||||||
### Todo Endpoints
|
### Todo Endpoints
|
||||||
|
|
||||||
All todo endpoints are prefixed with `/api/v1/todos`
|
|
||||||
|
|
||||||
- `GET /api/v1/todos/` - List all todos (with pagination)
|
- `GET /api/v1/todos/` - List all todos (with pagination)
|
||||||
- Query parameters: `skip` (default: 0), `limit` (default: 100)
|
|
||||||
- `POST /api/v1/todos/` - Create a new todo
|
- `POST /api/v1/todos/` - Create a new todo
|
||||||
- `GET /api/v1/todos/{todo_id}` - Get a specific todo by ID
|
- `GET /api/v1/todos/{todo_id}` - Get a specific todo
|
||||||
- `PUT /api/v1/todos/{todo_id}` - Update a todo by ID
|
- `PUT /api/v1/todos/{todo_id}` - Update a todo
|
||||||
- `DELETE /api/v1/todos/{todo_id}` - Delete a todo by ID
|
- `DELETE /api/v1/todos/{todo_id}` - Delete a todo
|
||||||
|
|
||||||
## Data Model
|
## Installation
|
||||||
|
|
||||||
Each todo has the following fields:
|
|
||||||
|
|
||||||
- `id` (integer) - Unique identifier
|
|
||||||
- `title` (string, required) - Todo title (1-200 characters)
|
|
||||||
- `description` (string, optional) - Todo description (max 1000 characters)
|
|
||||||
- `completed` (boolean) - Completion status (default: false)
|
|
||||||
- `created_at` (datetime) - Creation timestamp
|
|
||||||
- `updated_at` (datetime) - Last update timestamp
|
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
1. Install dependencies:
|
1. Install dependencies:
|
||||||
```bash
|
```bash
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Start the application:
|
## Running the Application
|
||||||
```bash
|
|
||||||
uvicorn main:app --reload
|
|
||||||
```
|
|
||||||
|
|
||||||
3. The API will be available at `http://localhost:8000`
|
1. Start the development server:
|
||||||
- API documentation: `http://localhost:8000/docs`
|
```bash
|
||||||
- Alternative docs: `http://localhost:8000/redoc`
|
uvicorn main:app --reload
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Access the API documentation:
|
||||||
|
- Swagger UI: http://localhost:8000/docs
|
||||||
|
- ReDoc: http://localhost:8000/redoc
|
||||||
|
- OpenAPI JSON: http://localhost:8000/openapi.json
|
||||||
|
|
||||||
|
3. Health check:
|
||||||
|
- http://localhost:8000/health
|
||||||
|
|
||||||
## Database
|
## Database
|
||||||
|
|
||||||
The application uses SQLite with the database file stored at `/app/storage/db/db.sqlite`. Database tables are automatically created when the application starts.
|
The application uses SQLite database stored at `/app/storage/db/db.sqlite`.
|
||||||
|
|
||||||
|
### Database Migrations
|
||||||
|
|
||||||
|
Database migrations are managed with Alembic:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Create a new migration
|
||||||
|
alembic revision --autogenerate -m "Description of changes"
|
||||||
|
|
||||||
|
# Apply migrations
|
||||||
|
alembic upgrade head
|
||||||
|
|
||||||
|
# Downgrade migrations
|
||||||
|
alembic downgrade -1
|
||||||
|
```
|
||||||
|
|
||||||
## Project Structure
|
## Project Structure
|
||||||
|
|
||||||
```
|
```
|
||||||
/
|
├── README.md
|
||||||
|
├── requirements.txt
|
||||||
├── main.py # FastAPI application entry point
|
├── main.py # FastAPI application entry point
|
||||||
├── requirements.txt # Python dependencies
|
|
||||||
├── alembic.ini # Alembic configuration
|
├── alembic.ini # Alembic configuration
|
||||||
├── alembic/ # Database migrations
|
├── alembic/ # Database migrations
|
||||||
├── app/
|
│ ├── versions/
|
||||||
│ ├── __init__.py
|
│ ├── env.py
|
||||||
│ ├── api/ # API routes
|
│ └── script.py.mako
|
||||||
│ │ ├── __init__.py
|
└── app/
|
||||||
│ │ └── todos.py # Todo CRUD endpoints
|
├── api/
|
||||||
│ ├── db/ # Database configuration
|
│ └── todos.py # Todo API endpoints
|
||||||
│ │ ├── __init__.py
|
├── db/
|
||||||
│ │ ├── base.py # SQLAlchemy Base
|
│ ├── base.py # SQLAlchemy base
|
||||||
│ │ └── session.py # Database session management
|
│ └── session.py # Database session
|
||||||
│ ├── models/ # SQLAlchemy models
|
├── models/
|
||||||
│ │ ├── __init__.py
|
│ └── todo.py # Todo database model
|
||||||
│ │ └── todo.py # Todo model
|
└── schemas/
|
||||||
│ └── schemas/ # Pydantic schemas
|
└── todo.py # Pydantic schemas
|
||||||
│ ├── __init__.py
|
|
||||||
│ └── todo.py # Todo request/response schemas
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Todo Model
|
||||||
|
|
||||||
|
Each todo has the following fields:
|
||||||
|
- `id` (int): Unique identifier
|
||||||
|
- `title` (str): Todo title (required)
|
||||||
|
- `description` (str): Optional description
|
||||||
|
- `completed` (bool): Completion status (default: false)
|
||||||
|
- `created_at` (datetime): Creation timestamp
|
||||||
|
- `updated_at` (datetime): Last update timestamp
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Code Quality
|
||||||
|
|
||||||
|
The project uses Ruff for linting and code formatting:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run linting
|
||||||
|
ruff check .
|
||||||
|
|
||||||
|
# Auto-fix issues
|
||||||
|
ruff check . --fix
|
||||||
|
```
|
||||||
|
|
||||||
|
### API Usage Examples
|
||||||
|
|
||||||
|
#### Create a Todo
|
||||||
|
```bash
|
||||||
|
curl -X POST "http://localhost:8000/api/v1/todos/" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"title": "Buy groceries", "description": "Milk, bread, eggs"}'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### List Todos
|
||||||
|
```bash
|
||||||
|
curl "http://localhost:8000/api/v1/todos/"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Update a Todo
|
||||||
|
```bash
|
||||||
|
curl -X PUT "http://localhost:8000/api/v1/todos/1" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"completed": true}'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Delete a Todo
|
||||||
|
```bash
|
||||||
|
curl -X DELETE "http://localhost:8000/api/v1/todos/1"
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is open source and available under the MIT License.
|
31
alembic.ini
31
alembic.ini
@ -4,9 +4,8 @@
|
|||||||
# path to migration scripts
|
# path to migration scripts
|
||||||
script_location = alembic
|
script_location = alembic
|
||||||
|
|
||||||
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
# template used to generate migration files
|
||||||
# Uncomment the line below if you want the files to be prepended with date and time
|
# file_template = %%(rev)s_%%(slug)s
|
||||||
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
|
||||||
|
|
||||||
# sys.path path, will be prepended to sys.path if present.
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
# defaults to the current working directory.
|
# defaults to the current working directory.
|
||||||
@ -22,7 +21,7 @@ prepend_sys_path = .
|
|||||||
|
|
||||||
# max length of characters to apply to the
|
# max length of characters to apply to the
|
||||||
# "slug" field
|
# "slug" field
|
||||||
# truncate_slug_length = 40
|
# max_length = 40
|
||||||
|
|
||||||
# set to 'true' to run the environment during
|
# set to 'true' to run the environment during
|
||||||
# the 'revision' command, regardless of autogenerate
|
# the 'revision' command, regardless of autogenerate
|
||||||
@ -33,26 +32,20 @@ prepend_sys_path = .
|
|||||||
# versions/ directory
|
# versions/ directory
|
||||||
# sourceless = false
|
# sourceless = false
|
||||||
|
|
||||||
# version number format
|
# version file format
|
||||||
version_num_format = %(migration_id)s_%(slug)s
|
version_file_format = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s
|
||||||
|
|
||||||
# version path separator; As mentioned above, this is the character used to split
|
|
||||||
# version_locations. The default within new alembic.ini files is "space", for backward
|
|
||||||
# compatibility. If using multiple databases, please set to ":" or some other character
|
|
||||||
# that isn't a space
|
|
||||||
version_path_separator = :
|
|
||||||
|
|
||||||
# the output encoding used when revision files
|
|
||||||
# are written from script.py.mako
|
|
||||||
# output_encoding = utf-8
|
|
||||||
|
|
||||||
|
# interpret the config file values here, which are keys from the main configuration
|
||||||
|
# environment variables
|
||||||
|
# any key/value that starts with SQLALCHEMY_ will be passed to create_engine
|
||||||
|
# as is, minus the SQLALCHEMY_ prefix.
|
||||||
|
# sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||||
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
|
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
|
||||||
|
|
||||||
|
|
||||||
[post_write_hooks]
|
[post_write_hooks]
|
||||||
# post_write_hooks defines scripts or Python functions that are run
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
# on newly generated revision scripts. See the documentation for further
|
# on newly generated revision scripts. See the documentation for further detail
|
||||||
# detail and examples
|
# and examples.
|
||||||
|
|
||||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
# hooks = black
|
# hooks = black
|
||||||
|
@ -5,12 +5,10 @@ from alembic import context
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Add the project root directory to the Python path
|
# Add the app directory to the Python path
|
||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
||||||
|
|
||||||
# Import your models here
|
|
||||||
from app.db.base import Base
|
from app.db.base import Base
|
||||||
from app.models.todo import Todo
|
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
"""Initial migration
|
"""Create todos table
|
||||||
|
|
||||||
Revision ID: 001
|
Revision ID: 001
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2024-01-01 12:00:00.000000
|
Create Date: 2025-06-20 12:00:00.000000
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '001'
|
revision = '001'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
@ -17,22 +16,21 @@ depends_on = None
|
|||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# Create todos table
|
||||||
op.create_table('todos',
|
op.create_table(
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
'todos',
|
||||||
sa.Column('title', sa.String(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('description', sa.String(), nullable=True),
|
sa.Column('title', sa.String(length=255), nullable=False),
|
||||||
sa.Column('completed', sa.Boolean(), nullable=False),
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
sa.Column('completed', sa.Boolean(), nullable=False),
|
||||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_index(op.f('ix_todos_id'), 'todos', ['id'], unique=False)
|
op.create_index(op.f('ix_todos_id'), 'todos', ['id'], unique=False)
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# Drop todos table
|
||||||
op.drop_index(op.f('ix_todos_id'), table_name='todos')
|
op.drop_index(op.f('ix_todos_id'), table_name='todos')
|
||||||
op.drop_table('todos')
|
op.drop_table('todos')
|
||||||
# ### end Alembic commands ###
|
|
Loading…
x
Reference in New Issue
Block a user