-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (32 loc) · 1.15 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
from fastapi import FastAPI, Request
from bson.objectid import ObjectId
from model import Todo
from db import collection
app = FastAPI()
@app.post("/todos")
async def create_todo(todo: Todo):
todo_dict = todo.dict()
result = await collection.insert_one(todo_dict)
todo_dict["_id"] = str(result.inserted_id)
return todo_dict
@app.get("/todos")
async def read_todos():
todos = []
async for todo in collection.find():
todo["_id"] = str(todo["_id"])
todos.append(todo)
return todos
@app.put("/todos/{todo_id}")
async def update_todo(todo_id: str, completed: bool):
result = await collection.update_one({"_id": ObjectId(todo_id)}, {"$set": {"completed": completed}})
if result.modified_count == 1:
return {"message": "Todo item updated successfully"}
else:
return {"error": "Failed to update todo item"}
@app.delete("/todos/{todo_id}")
async def delete_todo(todo_id: str):
result = await collection.delete_one({"_id": ObjectId(todo_id)})
if result.deleted_count == 1:
return {"message": "Todo item deleted successfully"}
else:
return {"error": "Failed to delete todo item"}