-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
82 lines (69 loc) · 2.79 KB
/
routes.py
File metadata and controls
82 lines (69 loc) · 2.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from typing import Optional
from fastapi import HTTPException, Query, Request, status
from app import app
from config import student_collection
from models import *
from pydantic import *
from bson.objectid import ObjectId
# @app.get("/")
# async def read_root():
# return {"Hello": "World"}
@app.head("/")
def read_root_head():
return {"Hello": "World"}
# Create
@app.post("/students", status_code = status.HTTP_201_CREATED)
def create_item(student: Student):
item_dict = student.model_dump()
result = student_collection.insert_one(item_dict)
return {"id": str(result.inserted_id)}
@app.get("/students", status_code = 200)
def get_students(country: Optional[str] = Query(None, description="To apply filter of country."),
age: Optional[int] = Query(None, description="Only records which have age greater than equal to the provided age should be present in the result.")):
# Query to filter students
query = {}
if country:
query['address.country'] = country
# {'address.country':'India'}
if age:
query['age'] = {"$gte": age}
# Retrieve students from MongoDB
print(query)
students = []
# print(student_collection.find(query))
if query != {}:
for student in student_collection.find(query):
students.append({"name": student["name"], "age": student["age"]})
else:
for student in student_collection.find():
students.append({"name": student["name"], "age": student["age"]})
return {"data": students}
# get student by id
@app.get("/students/{id}")
def fetch_student(id: str=None):
# id = request.query_params.get("id")
fetched_student = student_collection.find_one({'_id':ObjectId(id)})
if fetched_student:
fetched_student.pop("_id")
return fetched_student
#summary: Update student
#description: API to update the student's properties based on information provided.
# Not mandatory that all information would be sent in PATCH,
# only what fields are sent should be updated in the Database.
@app.patch("/students/{id}",status_code = status.HTTP_204_NO_CONTENT)
async def update_student(id: str, student_update: dict):
# id = request.query_params.get("id")
student = student_collection.find_one({"_id": ObjectId(id)})
if student:
student_collection.update_one({"_id": ObjectId(id)}, {"$set": student_update})
return None
else:
raise HTTPException(status_code=404, detail="Student not found")
# summary: Delete student
@app.delete("/students/{id}")
async def delete_student(id: str):
result = student_collection.delete_one({"_id": ObjectId(id)})
if result.deleted_count == 1:
return {"message": "Student deleted successfully"}
else:
raise HTTPException(status_code=404, detail="Student not found")