docker / app.py
test090309a's picture
ohne gradio
f6c211e verified
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
app = FastAPI()
class Task(BaseModel):
id: int
title: str
description: Optional[str] = None
done: bool = False
tasks: List[Task] = []
@app.get("/")
def root():
return {"message": "Willkommen bei der ToDo-API! Siehe /tasks"}
@app.get("/tasks", response_model=List[Task])
def get_tasks():
return tasks
@app.post("/tasks", response_model=Task)
def create_task(task: Task):
if any(t.id == task.id for t in tasks):
raise HTTPException(status_code=400, detail="ID existiert bereits.")
tasks.append(task)
return task
@app.get("/tasks/{task_id}", response_model=Task)
def get_task(task_id: int):
for task in tasks:
if task.id == task_id:
return task
raise HTTPException(status_code=404, detail="Aufgabe nicht gefunden.")
@app.put("/tasks/{task_id}/done", response_model=Task)
def mark_task_done(task_id: int):
for task in tasks:
if task.id == task_id:
task.done = True
return task
raise HTTPException(status_code=404, detail="Aufgabe nicht gefunden.")
@app.delete("/tasks/{task_id}")
def delete_task(task_id: int):
for i, task in enumerate(tasks):
if task.id == task_id:
del tasks[i]
return {"detail": "Aufgabe gelöscht."}
raise HTTPException(status_code=404, detail="Aufgabe nicht gefunden.")