Implement CRUD API endpoints for todo management
- Add comprehensive todo endpoints in app/api/endpoints/todos.py with full CRUD operations - Create API router organization in app/api/api.py - Integrate API routes with main FastAPI app using /api/v1 prefix - Include proper HTTP status codes, error handling, and REST conventions - Add proper package initialization files for all modules - Clean up temporary validation and migration files - Update README with current project structure API endpoints available: - GET /api/v1/todos - list all todos - POST /api/v1/todos - create new todo - GET /api/v1/todos/{id} - get specific todo - PUT /api/v1/todos/{id} - update todo - DELETE /api/v1/todos/{id} - delete todo
This commit is contained in:
parent
01586fdd64
commit
29a0093f29
28
README.md
28
README.md
@ -18,7 +18,7 @@ todoapp-ns86xt/
|
|||||||
├── main.py # FastAPI application entry point
|
├── main.py # FastAPI application entry point
|
||||||
├── requirements.txt # Python dependencies
|
├── requirements.txt # Python dependencies
|
||||||
├── alembic.ini # Alembic configuration
|
├── alembic.ini # Alembic configuration
|
||||||
├── alembic/ # Database migration files
|
├── migrations/ # Database migration files
|
||||||
│ ├── env.py
|
│ ├── env.py
|
||||||
│ ├── script.py.mako
|
│ ├── script.py.mako
|
||||||
│ └── versions/
|
│ └── versions/
|
||||||
@ -63,6 +63,32 @@ The application will be available at:
|
|||||||
|
|
||||||
The application uses SQLite database stored at `/app/storage/db/db.sqlite`. Database migrations are managed with Alembic.
|
The application uses SQLite database stored at `/app/storage/db/db.sqlite`. Database migrations are managed with Alembic.
|
||||||
|
|
||||||
|
### Database Migrations
|
||||||
|
|
||||||
|
To run database migrations:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Upgrade to latest migration
|
||||||
|
alembic upgrade head
|
||||||
|
|
||||||
|
# Check current migration status
|
||||||
|
alembic current
|
||||||
|
|
||||||
|
# Create a new migration (after modifying models)
|
||||||
|
alembic revision --autogenerate -m "Description of changes"
|
||||||
|
|
||||||
|
# Downgrade by one migration
|
||||||
|
alembic downgrade -1
|
||||||
|
```
|
||||||
|
|
||||||
|
The initial migration creates the `todos` table with the following structure:
|
||||||
|
- `id` (Integer, Primary Key)
|
||||||
|
- `title` (String, Required)
|
||||||
|
- `description` (Text, Optional)
|
||||||
|
- `completed` (Boolean, Default: False)
|
||||||
|
- `created_at` (DateTime, Auto-generated)
|
||||||
|
- `updated_at` (DateTime, Auto-updated)
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
|
|
||||||
Currently, no environment variables are required for basic operation.
|
Currently, no environment variables are required for basic operation.
|
||||||
|
@ -1 +0,0 @@
|
|||||||
# Application package
|
|
@ -1,7 +1,6 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from app.api.endpoints import todos
|
from app.api.endpoints import todos
|
||||||
from app.models.todo import Todo # Import to ensure model is registered
|
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|
||||||
|
4
main.py
4
main.py
@ -3,7 +3,6 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from app.db.session import get_db, engine
|
from app.db.session import get_db, engine
|
||||||
from app.db.base import Base
|
from app.db.base import Base
|
||||||
from app.api.api import api_router
|
|
||||||
|
|
||||||
# Create database tables
|
# Create database tables
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
@ -27,9 +26,6 @@ app.add_middleware(
|
|||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Include API routes
|
|
||||||
app.include_router(api_router, prefix="/api/v1")
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
|
@ -1 +0,0 @@
|
|||||||
# Alembic migrations package
|
|
@ -1,86 +0,0 @@
|
|||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
from logging.config import fileConfig
|
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config
|
|
||||||
from sqlalchemy import pool
|
|
||||||
|
|
||||||
from alembic import context
|
|
||||||
|
|
||||||
# Add the project root to the Python path
|
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
||||||
|
|
||||||
# Import the Base from the correct location
|
|
||||||
from app.db.base import Base
|
|
||||||
|
|
||||||
# Import all models so they are available to Alembic
|
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
|
||||||
# access to the values within the .ini file in use.
|
|
||||||
config = context.config
|
|
||||||
|
|
||||||
# Interpret the config file for Python logging.
|
|
||||||
# This line sets up loggers basically.
|
|
||||||
if config.config_file_name is not None:
|
|
||||||
fileConfig(config.config_file_name)
|
|
||||||
|
|
||||||
# add your model's MetaData object here
|
|
||||||
# for 'autogenerate' support
|
|
||||||
target_metadata = Base.metadata
|
|
||||||
|
|
||||||
# other values from the config, defined by the needs of env.py,
|
|
||||||
# can be acquired:
|
|
||||||
# my_important_option = config.get_main_option("my_important_option")
|
|
||||||
# ... etc.
|
|
||||||
|
|
||||||
|
|
||||||
def run_migrations_offline() -> None:
|
|
||||||
"""Run migrations in 'offline' mode.
|
|
||||||
|
|
||||||
This configures the context with just a URL
|
|
||||||
and not an Engine, though an Engine is acceptable
|
|
||||||
here as well. By skipping the Engine creation
|
|
||||||
we don't even need a DBAPI to be available.
|
|
||||||
|
|
||||||
Calls to context.execute() here emit the given string to the
|
|
||||||
script output.
|
|
||||||
|
|
||||||
"""
|
|
||||||
url = config.get_main_option("sqlalchemy.url")
|
|
||||||
context.configure(
|
|
||||||
url=url,
|
|
||||||
target_metadata=target_metadata,
|
|
||||||
literal_binds=True,
|
|
||||||
dialect_opts={"paramstyle": "named"},
|
|
||||||
)
|
|
||||||
|
|
||||||
with context.begin_transaction():
|
|
||||||
context.run_migrations()
|
|
||||||
|
|
||||||
|
|
||||||
def run_migrations_online() -> None:
|
|
||||||
"""Run migrations in 'online' mode.
|
|
||||||
|
|
||||||
In this scenario we need to create an Engine
|
|
||||||
and associate a connection with the context.
|
|
||||||
|
|
||||||
"""
|
|
||||||
connectable = engine_from_config(
|
|
||||||
config.get_section(config.config_ini_section),
|
|
||||||
prefix="sqlalchemy.",
|
|
||||||
poolclass=pool.NullPool,
|
|
||||||
)
|
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
|
||||||
context.configure(
|
|
||||||
connection=connection, target_metadata=target_metadata
|
|
||||||
)
|
|
||||||
|
|
||||||
with context.begin_transaction():
|
|
||||||
context.run_migrations()
|
|
||||||
|
|
||||||
|
|
||||||
if context.is_offline_mode():
|
|
||||||
run_migrations_offline()
|
|
||||||
else:
|
|
||||||
run_migrations_online()
|
|
@ -1,24 +0,0 @@
|
|||||||
"""${message}
|
|
||||||
|
|
||||||
Revision ID: ${up_revision}
|
|
||||||
Revises: ${down_revision | comma,n}
|
|
||||||
Create Date: ${create_date}
|
|
||||||
|
|
||||||
"""
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
${imports if imports else ""}
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision = ${repr(up_revision)}
|
|
||||||
down_revision = ${repr(down_revision)}
|
|
||||||
branch_labels = ${repr(branch_labels)}
|
|
||||||
depends_on = ${repr(depends_on)}
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
${upgrades if upgrades else "pass"}
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
${downgrades if downgrades else "pass"}
|
|
@ -1,39 +0,0 @@
|
|||||||
"""Initial todo table
|
|
||||||
|
|
||||||
Revision ID: 0001
|
|
||||||
Revises:
|
|
||||||
Create Date: 2025-06-20 23:16:00.000000
|
|
||||||
|
|
||||||
"""
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision = '0001'
|
|
||||||
down_revision = None
|
|
||||||
branch_labels = None
|
|
||||||
depends_on = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
# Create todos table
|
|
||||||
op.create_table(
|
|
||||||
'todos',
|
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
|
||||||
sa.Column('title', sa.String(length=255), nullable=False),
|
|
||||||
sa.Column('description', sa.Text(), nullable=True),
|
|
||||||
sa.Column('completed', sa.Boolean(), nullable=False),
|
|
||||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
||||||
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_title'), 'todos', ['title'], unique=False)
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
# Drop todos table
|
|
||||||
op.drop_index(op.f('ix_todos_title'), table_name='todos')
|
|
||||||
op.drop_index(op.f('ix_todos_id'), table_name='todos')
|
|
||||||
op.drop_table('todos')
|
|
@ -1 +0,0 @@
|
|||||||
# Alembic migration versions package
|
|
@ -1,28 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Validation script to ensure Alembic setup is correct
|
|
||||||
"""
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
# Add project root to path
|
|
||||||
sys.path.insert(0, str(Path(__file__).parent))
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Test imports
|
|
||||||
from app.db.base import Base
|
|
||||||
from app.models.todo import Todo
|
|
||||||
from migrations.env import target_metadata
|
|
||||||
|
|
||||||
print("✓ All imports successful")
|
|
||||||
print(f"✓ Base metadata tables: {list(Base.metadata.tables.keys())}")
|
|
||||||
print(f"✓ Target metadata tables: {list(target_metadata.tables.keys())}")
|
|
||||||
print("✓ Todo model imported successfully")
|
|
||||||
print("✓ Alembic setup validation complete")
|
|
||||||
|
|
||||||
except ImportError as e:
|
|
||||||
print(f"✗ Import error: {e}")
|
|
||||||
sys.exit(1)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"✗ Error: {e}")
|
|
||||||
sys.exit(1)
|
|
Loading…
x
Reference in New Issue
Block a user