Spaces:
Sleeping
Sleeping
Create app/main.py
Browse files- app/main.py +88 -0
app/main.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, status
|
| 2 |
+
from typing import List
|
| 3 |
+
from app.models import Task
|
| 4 |
+
from app import database
|
| 5 |
+
|
| 6 |
+
app = FastAPI(
|
| 7 |
+
title="To-Do List API",
|
| 8 |
+
description="API simples para gerenciar tarefas",
|
| 9 |
+
version="1.0.0"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
@app.get("/", tags=["Root"])
|
| 13 |
+
def read_root():
|
| 14 |
+
"""Endpoint raiz da API"""
|
| 15 |
+
return {
|
| 16 |
+
"message": "Bem-vindo à To-Do List API!",
|
| 17 |
+
"docs": "/docs",
|
| 18 |
+
"redoc": "/redoc"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# CREATE - Criar nova tarefa
|
| 22 |
+
@app.post(
|
| 23 |
+
"/tasks/",
|
| 24 |
+
response_model=Task,
|
| 25 |
+
status_code=status.HTTP_201_CREATED,
|
| 26 |
+
tags=["Tasks"]
|
| 27 |
+
)
|
| 28 |
+
def create_task(task: Task):
|
| 29 |
+
"""Cria uma nova tarefa"""
|
| 30 |
+
return database.create_task(task)
|
| 31 |
+
|
| 32 |
+
# READ - Listar todas as tarefas
|
| 33 |
+
@app.get(
|
| 34 |
+
"/tasks/",
|
| 35 |
+
response_model=List[Task],
|
| 36 |
+
tags=["Tasks"]
|
| 37 |
+
)
|
| 38 |
+
def get_tasks():
|
| 39 |
+
"""Lista todas as tarefas"""
|
| 40 |
+
return database.get_all_tasks()
|
| 41 |
+
|
| 42 |
+
# READ - Buscar tarefa específica por ID
|
| 43 |
+
@app.get(
|
| 44 |
+
"/tasks/{task_id}",
|
| 45 |
+
response_model=Task,
|
| 46 |
+
tags=["Tasks"]
|
| 47 |
+
)
|
| 48 |
+
def get_task(task_id: int):
|
| 49 |
+
"""Busca uma tarefa específica por ID"""
|
| 50 |
+
task = database.get_task_by_id(task_id)
|
| 51 |
+
if task is None:
|
| 52 |
+
raise HTTPException(
|
| 53 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 54 |
+
detail=f"Tarefa com ID {task_id} não encontrada"
|
| 55 |
+
)
|
| 56 |
+
return task
|
| 57 |
+
|
| 58 |
+
# UPDATE - Atualizar tarefa existente
|
| 59 |
+
@app.put(
|
| 60 |
+
"/tasks/{task_id}",
|
| 61 |
+
response_model=Task,
|
| 62 |
+
tags=["Tasks"]
|
| 63 |
+
)
|
| 64 |
+
def update_task(task_id: int, task: Task):
|
| 65 |
+
"""Atualiza uma tarefa existente"""
|
| 66 |
+
updated = database.update_task(task_id, task)
|
| 67 |
+
if updated is None:
|
| 68 |
+
raise HTTPException(
|
| 69 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 70 |
+
detail=f"Tarefa com ID {task_id} não encontrada"
|
| 71 |
+
)
|
| 72 |
+
return updated
|
| 73 |
+
|
| 74 |
+
# DELETE - Remover tarefa
|
| 75 |
+
@app.delete(
|
| 76 |
+
"/tasks/{task_id}",
|
| 77 |
+
status_code=status.HTTP_204_NO_CONTENT,
|
| 78 |
+
tags=["Tasks"]
|
| 79 |
+
)
|
| 80 |
+
def delete_task(task_id: int):
|
| 81 |
+
"""Remove uma tarefa"""
|
| 82 |
+
deleted = database.delete_task(task_id)
|
| 83 |
+
if not deleted:
|
| 84 |
+
raise HTTPException(
|
| 85 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 86 |
+
detail=f"Tarefa com ID {task_id} não encontrada"
|
| 87 |
+
)
|
| 88 |
+
return None
|