From 980c57529158173edac4a616779d0b44aa6420eb Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 13 May 2025 06:05:28 +0000 Subject: [PATCH] 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) --- alembic.ini | 2 +- alembic/versions/1_create_todos_table.py | 2 +- app/api/todos.py | 11 ++++++++--- app/crud/todo.py | 9 +++++++-- app/db/session.py | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/alembic.ini b/alembic.ini index cd14d41..6eef928 100644 --- a/alembic.ini +++ b/alembic.ini @@ -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] diff --git a/alembic/versions/1_create_todos_table.py b/alembic/versions/1_create_todos_table.py index 42acb14..86ff101 100644 --- a/alembic/versions/1_create_todos_table.py +++ b/alembic/versions/1_create_todos_table.py @@ -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) diff --git a/app/api/todos.py b/app/api/todos.py index 1bae88e..83ec456 100644 --- a/app/api/todos.py +++ b/app/api/todos.py @@ -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)): diff --git a/app/crud/todo.py b/app/crud/todo.py index 8abe4dd..b11fef2 100644 --- a/app/crud/todo.py +++ b/app/crud/todo.py @@ -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() diff --git a/app/db/session.py b/app/db/session.py index 635c60a..f005c4b 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -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"