Spaces:
Runtime error
Runtime error
Update src/main.py
Browse files- src/main.py +54 -8
src/main.py
CHANGED
|
@@ -5,7 +5,7 @@ from fastapi import FastAPI, Depends, HTTPException, Query
|
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from sqlalchemy.orm import Session
|
| 7 |
|
| 8 |
-
# These imports now work because the
|
| 9 |
from database import get_db, engine, Base
|
| 10 |
from models.todo import Todo
|
| 11 |
from models.user import User
|
|
@@ -18,30 +18,76 @@ from middleware.auth import get_current_user
|
|
| 18 |
|
| 19 |
app = FastAPI(
|
| 20 |
title="Todo API",
|
|
|
|
| 21 |
version="2.0.0"
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
@app.on_event("startup")
|
| 26 |
def startup_event():
|
|
|
|
| 27 |
Base.metadata.create_all(bind=engine)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
| 32 |
-
allow_origins=["*"],
|
| 33 |
allow_credentials=True,
|
| 34 |
allow_methods=["*"],
|
| 35 |
allow_headers=["*"],
|
| 36 |
)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
app.include_router(auth_router, prefix="/api/v1")
|
| 40 |
app.include_router(chat_router)
|
| 41 |
app.include_router(task_router, prefix="/api/v1")
|
| 42 |
|
| 43 |
@app.get("/")
|
| 44 |
-
def
|
| 45 |
-
return {"status": "
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from sqlalchemy.orm import Session
|
| 7 |
|
| 8 |
+
# These imports now work because of the path fix in the root main.py
|
| 9 |
from database import get_db, engine, Base
|
| 10 |
from models.todo import Todo
|
| 11 |
from models.user import User
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI(
|
| 20 |
title="Todo API",
|
| 21 |
+
description="REST API for managing todo items",
|
| 22 |
version="2.0.0"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# FIX: Use on_event to avoid 'generator object is not an async iterator' error
|
| 26 |
@app.on_event("startup")
|
| 27 |
def startup_event():
|
| 28 |
+
print("Backend starting... Creating database tables.")
|
| 29 |
Base.metadata.create_all(bind=engine)
|
| 30 |
|
| 31 |
+
# FIX: Allow GitHub Pages to connect
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
| 34 |
+
allow_origins=["*"],
|
| 35 |
allow_credentials=True,
|
| 36 |
allow_methods=["*"],
|
| 37 |
allow_headers=["*"],
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Include All Routers
|
| 41 |
app.include_router(auth_router, prefix="/api/v1")
|
| 42 |
app.include_router(chat_router)
|
| 43 |
app.include_router(task_router, prefix="/api/v1")
|
| 44 |
|
| 45 |
@app.get("/")
|
| 46 |
+
def health():
|
| 47 |
+
return {"status": "online", "server": "Hugging Face Spaces"}
|
| 48 |
|
| 49 |
+
# --- COMPLETE TODO ROUTES ---
|
| 50 |
+
|
| 51 |
+
@app.get("/api/v1/todos", response_model=TodoListResponse)
|
| 52 |
+
def get_todos(
|
| 53 |
+
db: Session = Depends(get_db),
|
| 54 |
+
current_user: User = Depends(get_current_user),
|
| 55 |
+
search: Optional[str] = Query(None),
|
| 56 |
+
status: Optional[str] = Query(None),
|
| 57 |
+
priority: Optional[str] = Query(None),
|
| 58 |
+
sort_by: Optional[str] = Query("created_at"),
|
| 59 |
+
sort_order: Optional[str] = Query("desc"),
|
| 60 |
+
):
|
| 61 |
+
service = TodoService(db, user_id=current_user.id)
|
| 62 |
+
todos = service.get_all(search=search, status=status, priority=priority, sort_by=sort_by, sort_order=sort_order)
|
| 63 |
+
|
| 64 |
+
# Helper to convert models to response
|
| 65 |
+
def _todo_to_response(todo):
|
| 66 |
+
return TodoResponse(
|
| 67 |
+
id=todo.id, user_id=todo.user_id, title=todo.title,
|
| 68 |
+
description=todo.description, completed=todo.completed,
|
| 69 |
+
priority=todo.priority, tags=todo.tags, due_date=todo.due_date,
|
| 70 |
+
recurrence=todo.recurrence, created_at=todo.created_at, updated_at=todo.updated_at,
|
| 71 |
+
overdue=False if todo.due_date is None or todo.completed else todo.due_date < datetime.utcnow()
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
todos_response = [_todo_to_response(t) for t in todos]
|
| 75 |
+
return {"todos": todos_response, "count": len(todos_response), "has_more": False}
|
| 76 |
+
|
| 77 |
+
@app.post("/api/v1/todos", response_model=TodoResponse, status_code=201)
|
| 78 |
+
def create_todo(todo_data: TodoCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
| 79 |
+
service = TodoService(db, user_id=current_user.id)
|
| 80 |
+
todo = service.create(todo_data)
|
| 81 |
+
# Re-using logic to return response
|
| 82 |
+
return TodoResponse(
|
| 83 |
+
id=todo.id, user_id=todo.user_id, title=todo.title,
|
| 84 |
+
description=todo.description, completed=todo.completed,
|
| 85 |
+
priority=todo.priority, created_at=todo.created_at, updated_at=todo.updated_at
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
@app.delete("/api/v1/todos/{todo_id}")
|
| 89 |
+
def delete_todo(todo_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
| 90 |
+
service = TodoService(db, user_id=current_user.id)
|
| 91 |
+
if not service.delete(todo_id):
|
| 92 |
+
raise HTTPException(status_code=404, detail="Todo not found")
|
| 93 |
+
return {"message": "Todo deleted successfully"}
|