forked from king04aman/All-In-One-Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastAPI-CURD.py
More file actions
66 lines (53 loc) · 1.95 KB
/
FastAPI-CURD.py
File metadata and controls
66 lines (53 loc) · 1.95 KB
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
from uuid import UUID, uuid4
app = FastAPI()
# Define a Pydantic model for Task
class Task(BaseModel):
id: Optional[UUID] = None
title: str
description: Optional[str] = None
completed: bool = False
# Initialize an empty list to store tasks
tasks = []
# Create a new task
@app.post("/tasks/", response_model=Task)
def create_task(task: Task):
task.id = uuid4() # Assign a UUID to the task
tasks.append(task) # Add the task to the tasks list
return task
# Retrieve all tasks
@app.get("/tasks/", response_model=List[Task])
def read_tasks():
return tasks
# Retrieve a specific task by ID
@app.get("/tasks/{task_id}", response_model=Task)
def read_task(task_id: UUID):
for task in tasks:
if task.id == task_id:
return task
# Raise HTTPException if task not found
raise HTTPException(status_code=404, detail="Task not found")
# Update a specific task by ID
@app.put("/tasks/{task_id}", response_model=Task)
def update_task(task_id: UUID, task_update: Task):
for idx, task in enumerate(tasks):
if task.id == task_id:
updated_task = task.copy(update=task_update.dict(exclude_unset=True))
tasks[idx] = updated_task
return updated_task
# Raise HTTPException if task not found
raise HTTPException(status_code=404, detail="Task not found")
# Delete a specific task by ID
@app.delete("/tasks/{task_id}", response_model=Task)
def delete_task(task_id: UUID):
for idx, task in enumerate(tasks):
if task.id == task_id:
return tasks.pop(idx)
# Raise HTTPException if task not found
raise HTTPException(status_code=404, detail="Task not found")
# Run the FastAPI app
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)