Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.postman/
postman/
Binary file added ujjwal/Assignments/__pycache__/main3.cpython-314.pyc
Binary file not shown.
Binary file not shown.
File renamed without changes.
13 changes: 13 additions & 0 deletions ujjwal/Assignments/main2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import FastAPI

app = FastAPI()


@app.get("/greet")
async def greet(name: str = "Guest"):
return {"message": f"Hello, {name}!"}

@app.get("/add")
async def add(a: int, b: int):
result = a + b
return {"result": result, "operation": f"{a} + {b} = {result}"}
19 changes: 19 additions & 0 deletions ujjwal/Assignments/main3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from fastapi import FastAPI
from fastapi.params import Body
app = FastAPI()

@app.get("/")
async def first():
return {"message":"Hi, you can create post here"}

@app.post("/createpost")
def create_post(payload:dict=Body(...)):
print(payload)
name = payload.get("name")
age = payload.get("age")

return {
"message": "User created successfully",
"data": f"Name: {name}, Age: {age}"
}

43 changes: 43 additions & 0 deletions ujjwal/Assignments/main5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from fastapi import FastAPI, Body

app = FastAPI()

tasks = []

@app.post("/tasks")
def create_task(payload: dict = Body(...)):
task = {
"id": len(tasks) + 1,
"title": payload.get("title"),
"completed": False
}
tasks.append(task)
return task

@app.get("/tasks")
def get_tasks():
return tasks

@app.get("/tasks/{id}")
def get_task(id: int):
for task in tasks:
if task["id"] == id:
return task
return {"error": "Task not found"}

@app.put("/tasks/{id}")
def update_task(id: int, payload: dict = Body(...)):
for task in tasks:
if task["id"] == id:
task["title"] = payload.get("title", task["title"])
task["completed"] = payload.get("completed", task["completed"])
return task
return {"error": "Task not found"}

@app.delete("/tasks/{id}")
def delete_task(id: int):
for index, task in enumerate(tasks):
if task["id"] == id:
deleted = tasks.pop(index)
return {"message": "Deleted", "task": deleted}
return {"error": "Task not found"}