-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
69 lines (59 loc) · 1.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
# FastAPI instance
app = FastAPI()
# In-memory "database"
todos = []
# Pydantic model for Todo
class Todo(BaseModel):
id: int
title: str
description: Optional[str] = None
completed: bool = False
# Helper function to find a todo by id
def find_todo(todo_id: int):
for todo in todos:
if todo.id == todo_id:
return todo
return None
# Root endpoint
@app.get("/")
def read_root():
return {"message": "Welcome to the Todo API!"}
# Get all todos
@app.get("/todos", response_model=List[Todo])
def get_todos():
return todos
# Get a todo by ID
@app.get("/todos/{todo_id}", response_model=Todo)
def get_todo(todo_id: int):
todo = find_todo(todo_id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
return todo
# Create a new todo
@app.post("/todos", response_model=Todo)
def create_todo(todo: Todo):
if find_todo(todo.id):
raise HTTPException(status_code=400, detail="Todo with this ID already exists")
todos.append(todo)
return todo
# Update an existing todo
@app.put("/todos/{todo_id}", response_model=Todo)
def update_todo(todo_id: int, updated_todo: Todo):
todo = find_todo(todo_id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
todo.title = updated_todo.title
todo.description = updated_todo.description
todo.completed = updated_todo.completed
return todo
# Delete a todo
@app.delete("/todos/{todo_id}", response_model=dict)
def delete_todo(todo_id: int):
todo = find_todo(todo_id)
if not todo:
raise HTTPException(status_code=404, detail="Todo not found")
todos.remove(todo)
return {"message": "Todo deleted successfully"}