Finish up the Todo application
- Fixed database connection path to match BackendIM requirements - Implemented filter by completion status in the todos endpoint - Fixed updated_at timestamp to automatically update - Updated alembic.ini with the correct database URL generated with BackendIM... (backend.im)
This commit is contained in:
parent
4f8e9f0f6e
commit
980c575291
@ -57,7 +57,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = sqlite:///projects/simpletodoapplication-3rp4k9/storage/db/db.sqlite
|
||||
sqlalchemy.url = sqlite:///app/storage/db/db.sqlite
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
|
@ -23,7 +23,7 @@ def upgrade() -> None:
|
||||
sa.Column('description', sa.String(), nullable=True),
|
||||
sa.Column('completed', sa.Boolean(), nullable=False, default=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)')),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True, server_default=sa.text('(CURRENT_TIMESTAMP)'), server_onupdate=sa.text('(CURRENT_TIMESTAMP)')),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_todos_id'), 'todos', ['id'], unique=False)
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@ -9,8 +9,13 @@ from app.schemas.todo import TodoCreate, TodoResponse, TodoUpdate
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=List[TodoResponse])
|
||||
def get_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
return todo_crud.get_todos(db, skip=skip, limit=limit)
|
||||
def get_todos(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
completed: Optional[bool] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return todo_crud.get_todos(db, skip=skip, limit=limit, completed=completed)
|
||||
|
||||
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
||||
|
@ -4,8 +4,13 @@ from sqlalchemy.orm import Session
|
||||
from app.models.todo import Todo
|
||||
from app.schemas.todo import TodoCreate, TodoUpdate
|
||||
|
||||
def get_todos(db: Session, skip: int = 0, limit: int = 100) -> List[Todo]:
|
||||
return db.query(Todo).offset(skip).limit(limit).all()
|
||||
def get_todos(db: Session, skip: int = 0, limit: int = 100, completed: Optional[bool] = None) -> List[Todo]:
|
||||
query = db.query(Todo)
|
||||
|
||||
if completed is not None:
|
||||
query = query.filter(Todo.completed == completed)
|
||||
|
||||
return query.offset(skip).limit(limit).all()
|
||||
|
||||
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
|
||||
return db.query(Todo).filter(Todo.id == todo_id).first()
|
||||
|
@ -3,7 +3,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from pathlib import Path
|
||||
|
||||
DB_DIR = Path("/projects/simpletodoapplication-3rp4k9/storage/db")
|
||||
DB_DIR = Path("/app") / "storage" / "db"
|
||||
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||
|
Loading…
x
Reference in New Issue
Block a user