关系图

main.py
| |
| |
| |
| from fastapi import FastAPI, HTTPException |
| |
| from model import Todo |
| |
| from database import ( |
| fetch_one_todo, |
| fetch_all_todos, |
| create_todo, |
| update_todo, |
| remove_todo, |
| ) |
| |
| |
| |
| from fastapi.middleware.cors import CORSMiddleware |
| app = FastAPI() |
| |
| origins = [ |
| "http://localhost:3000", |
| ] |
| |
| |
| |
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=origins, |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
| |
| @app.get("/") |
| async def read_root(): |
| return {"Hello": "World"} |
| |
| @app.get("/api/todo") |
| async def get_todo(): |
| response = await fetch_all_todos() |
| return response |
| |
| @app.get("/api/todo/{title}", response_model=Todo) |
| async def get_todo_by_title(title): |
| response = await fetch_one_todo(title) |
| if response: |
| return response |
| raise HTTPException(404, f"There is no todo with the title {title}") |
| |
| @app.post("/api/todo/", response_model=Todo) |
| async def post_todo(todo: Todo): |
| response = await create_todo(todo.dict()) |
| if response: |
| return response |
| raise HTTPException(400, "Something went wrong") |
| |
| @app.put("/api/todo/{title}/", response_model=Todo) |
| async def put_todo(title: str, desc: str): |
| response = await update_todo(title, desc) |
| if response: |
| return response |
| raise HTTPException(404, f"There is no todo with the title {title}") |
| |
| @app.delete("/api/todo/{title}") |
| async def delete_todo(title): |
| response = await remove_todo(title) |
| if response: |
| return "Successfully deleted todo" |
| raise HTTPException(404, f"There is no todo with the title {title}") |
model.py
| |
| |
| |
| |
| from pydantic import BaseModel |
| |
| class Todo(BaseModel): |
| title: str |
| description: str |
database.py
| |
| |
| |
| import motor.motor_asyncio |
| from model import Todo |
| |
| client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost:27017/') |
| database = client.TodoList |
| collection = database.todo |
| |
| async def fetch_one_todo(title): |
| document = await collection.find_one({"title": title}) |
| return document |
| |
| async def fetch_all_todos(): |
| todos = [] |
| cursor = collection.find({}) |
| async for document in cursor: |
| todos.append(Todo(**document)) |
| return todos |
| |
| async def create_todo(todo): |
| document = todo |
| result = await collection.insert_one(document) |
| return document |
| |
| |
| async def update_todo(title, desc): |
| await collection.update_one({"title": title}, {"$set": {"description": desc}}) |
| document = await collection.find_one({"title": title}) |
| return document |
| |
| async def remove_todo(title): |
| await collection.delete_one({"title": title}) |
| return True |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验