37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from schemas.todo import TodoSchema, TodoCreate, TodoUpdate
|
|
from helpers.todo_helpers import create_todo, update_todo, delete_todo
|
|
import uuid
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/todo", status_code=status.HTTP_201_CREATED, response_model=TodoSchema)
|
|
async def create_new_todo(
|
|
todo_data: TodoCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
new_todo = create_todo(db=db, todo_data=todo_data)
|
|
return new_todo
|
|
|
|
@router.post("/todo/{todo_id}", status_code=status.HTTP_200_OK, response_model=TodoSchema)
|
|
async def update_existing_todo(
|
|
todo_id: uuid.UUID,
|
|
todo_data: TodoUpdate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
updated_todo = update_todo(db=db, todo_id=todo_id, todo_data=todo_data)
|
|
if not updated_todo:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return updated_todo
|
|
|
|
@router.delete("/todo/{todo_id}", status_code=status.HTTP_200_OK)
|
|
async def delete_existing_todo(
|
|
todo_id: uuid.UUID,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
deleted = delete_todo(db=db, todo_id=todo_id)
|
|
if not deleted:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return {"message": "Todo deleted successfully"} |