Skip to content

Commit 6f8c608

Browse files
committed
add initial crud endpoints
0 parents  commit 6f8c608

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run:
2+
fastapi dev main.py

__pycache__/main.cpython-312.pyc

906 Bytes
Binary file not shown.

main.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from fastapi import FastAPI, HTTPException
2+
from typing import List
3+
4+
from uuid import UUID, uuid4
5+
from models.task_models import Task
6+
7+
8+
app = FastAPI()
9+
10+
tasks = []
11+
12+
13+
@app.post("/tasks", response_model=Task)
14+
def add_task(task: Task):
15+
task.id = uuid4()
16+
tasks.append(task)
17+
return task
18+
19+
20+
@app.get("/tasks", response_model=List[Task])
21+
async def get_tasks():
22+
return tasks
23+
24+
25+
@app.get("/tasks/{task_id}", response_model=Task)
26+
def get_task_by_id(task_id: UUID):
27+
for task in tasks:
28+
if task.id == task_id:
29+
return task
30+
31+
raise HTTPException(status_code=404, detail="Task not found")
32+
33+
34+
@app.put("/tasks/{task_id}")
35+
def update_task(task_id: UUID, task_update: Task):
36+
for i, task in enumerate(tasks):
37+
if task.id == task_id:
38+
updated_task = task.copy(update=task_update.dict(exclude_unset=True))
39+
tasks[i] = updated_task
40+
return updated_task
41+
42+
raise HTTPException(status_code=404, detail="Task not found")
43+
44+
45+
@app.delete("/tasks/{task_id}")
46+
def delete_task(task_id: UUID):
47+
for i, task in enumerate(tasks):
48+
if task.id == task_id:
49+
tasks.pop(i)
50+
return {"ok": "task deleted successfully"}
51+
raise HTTPException(status_code=404, detail="Task not found")
52+
53+
54+
if __name__ == "__main__":
55+
import uvicorn
56+
57+
uvicorn.run(app, host="0.0.0.0", port=8000)
640 Bytes
Binary file not shown.

models/task_models.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pydantic import BaseModel
2+
from typing import List, Optional
3+
from uuid import UUID
4+
5+
6+
class Task(BaseModel):
7+
id: Optional[UUID] = None
8+
title: str
9+
description: Optional[str]
10+
completed: bool = False

requirements.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
annotated-types==0.7.0
2+
anyio==4.4.0
3+
click==8.1.7
4+
fastapi==0.114.0
5+
h11==0.14.0
6+
idna==3.8
7+
pydantic==2.9.0
8+
pydantic_core==2.23.2
9+
sniffio==1.3.1
10+
starlette==0.38.4
11+
typing_extensions==4.12.2
12+
tzdata==2024.1
13+
uvicorn==0.30.6

0 commit comments

Comments
 (0)