Automated Action 69f6a404bd Add user authentication to Todo application
- Create User model and schema
- Implement password hashing with bcrypt
- Add JWT token-based authentication
- Create user and auth endpoints
- Update todo endpoints with user authentication
- Add alembic migration for user model
- Update README with new features
2025-05-16 02:07:51 +00:00

59 lines
1.7 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.todo import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
return db.query(Todo).filter(Todo.id == todo_id).first()
def get_todo_by_owner(db: Session, todo_id: int, owner_id: int) -> Optional[Todo]:
return db.query(Todo).filter(Todo.id == todo_id, Todo.owner_id == owner_id).first()
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_by_owner(db: Session, owner_id: int, skip: int = 0, limit: int = 100) -> List[Todo]:
return db.query(Todo).filter(Todo.owner_id == owner_id).offset(skip).limit(limit).all()
def create_todo(db: Session, todo: TodoCreate, owner_id: int) -> Todo:
db_todo = Todo(
title=todo.title,
description=todo.description,
completed=todo.completed,
owner_id=owner_id,
)
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
def update_todo(db: Session, todo_id: int, todo: TodoUpdate, owner_id: int) -> Optional[Todo]:
db_todo = get_todo_by_owner(db, todo_id, owner_id)
if db_todo is None:
return None
todo_data = todo.model_dump(exclude_unset=True)
for key, value in todo_data.items():
setattr(db_todo, key, value)
db.commit()
db.refresh(db_todo)
return db_todo
def delete_todo(db: Session, todo_id: int, owner_id: int) -> bool:
db_todo = get_todo_by_owner(db, todo_id, owner_id)
if db_todo is None:
return False
db.delete(db_todo)
db.commit()
return True