Compare commits
No commits in common. "0ec5bd6053e21c2cc96c03a54019997db29f5e9e" and "29a0093f29894ff96eabe57d00b89d2b73f05463" have entirely different histories.
0ec5bd6053
...
29a0093f29
@ -0,0 +1 @@
|
|||||||
|
# API package
|
8
app/api/api.py
Normal file
8
app/api/api.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
from app.api.endpoints import todos
|
||||||
|
|
||||||
|
api_router = APIRouter()
|
||||||
|
|
||||||
|
# Include todos router
|
||||||
|
api_router.include_router(todos.router, prefix="/todos", tags=["todos"])
|
1
app/api/endpoints/__init__.py
Normal file
1
app/api/endpoints/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# API endpoints package
|
83
app/api/endpoints/todos.py
Normal file
83
app/api/endpoints/todos.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
from typing import List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.models.todo import Todo
|
||||||
|
from app.schemas.todo import TodoCreate, TodoUpdate, TodoResponse
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[TodoResponse])
|
||||||
|
def get_todos(db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Get all todos
|
||||||
|
"""
|
||||||
|
todos = db.query(Todo).all()
|
||||||
|
return todos
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
||||||
|
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Create a new todo
|
||||||
|
"""
|
||||||
|
db_todo = Todo(**todo.dict())
|
||||||
|
db.add(db_todo)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_todo)
|
||||||
|
return db_todo
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{todo_id}", response_model=TodoResponse)
|
||||||
|
def get_todo(todo_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Get a specific todo by ID
|
||||||
|
"""
|
||||||
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
||||||
|
if not todo:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=f"Todo with id {todo_id} not found"
|
||||||
|
)
|
||||||
|
return todo
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/{todo_id}", response_model=TodoResponse)
|
||||||
|
def update_todo(todo_id: int, todo_update: TodoUpdate, db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Update a specific todo by ID
|
||||||
|
"""
|
||||||
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
||||||
|
if not todo:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=f"Todo with id {todo_id} not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update only the fields that are provided
|
||||||
|
update_data = todo_update.dict(exclude_unset=True)
|
||||||
|
for field, value in update_data.items():
|
||||||
|
setattr(todo, field, value)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(todo)
|
||||||
|
return todo
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""
|
||||||
|
Delete a specific todo by ID
|
||||||
|
"""
|
||||||
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
||||||
|
if not todo:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=f"Todo with id {todo_id} not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
db.delete(todo)
|
||||||
|
db.commit()
|
||||||
|
return None
|
@ -0,0 +1 @@
|
|||||||
|
# Database package
|
@ -0,0 +1,4 @@
|
|||||||
|
# Models package
|
||||||
|
from .todo import Todo
|
||||||
|
|
||||||
|
__all__ = ["Todo"]
|
14
app/models/todo.py
Normal file
14
app/models/todo.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Todo(Base):
|
||||||
|
__tablename__ = "todos"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
title = Column(String(255), nullable=False, index=True)
|
||||||
|
description = Column(Text, nullable=True)
|
||||||
|
completed = Column(Boolean, default=False, nullable=False)
|
||||||
|
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||||
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
1
app/schemas/__init__.py
Normal file
1
app/schemas/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Schemas package
|
28
app/schemas/todo.py
Normal file
28
app/schemas/todo.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class TodoBase(BaseModel):
|
||||||
|
title: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
completed: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class TodoCreate(TodoBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TodoUpdate(BaseModel):
|
||||||
|
title: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
completed: Optional[bool] = None
|
||||||
|
|
||||||
|
|
||||||
|
class TodoResponse(TodoBase):
|
||||||
|
id: int
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
Loading…
x
Reference in New Issue
Block a user