-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (57 loc) · 1.91 KB
/
main.py
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
from typing import Optional
from decouple import config
from fastapi import FastAPI, Request
from pydantic import BaseModel
import time
from pymongo import MongoClient
mongo_url = config('MONGO')
print(mongo_url)
def get_database():
# Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
client = MongoClient(mongo_url,tls=True, tlsAllowInvalidCertificates=True)
# Create the database for our example (we will use the same database throughout the tutorial
return client['student']
dbname = get_database()
collection_name= dbname["student"]
app = FastAPI()
starttime = time.time()
class Student(BaseModel):
name: str
registration_number: str
division: str
fa_name: str
class StudentDelete(BaseModel):
name: str
@app.get("/")
def healthCheck():
return {"message": "OK", "uptime": time.time()-starttime}
@app.get("/student")
async def readItem(name: str):
result = collection_name.find_one({"name": name})
if result is None:
return{"result": "failed","status": 404}
return {"name": result["name"],"registration number": result["registration_number"], "division": result["division"], "FA name": result["fa_name"]}
@app.post("/student")
async def create_item(info : Student):
contojson = info.json()
collection_name.insert_one(eval(contojson))
return {
"status" : "SUCCESS",
"data" : info
}
@app.put("/student")
async def updateInformation(info : Student):
collection_name.update_one(
{"name": info.name},
{"$set": {"fa_name": info.fa_name, "division": info.division, "registration_number": info.registration_number}})
return {
"status" : "SUCCESS",
"data" : info
}
@app.delete("/student")
async def deleteInformation(info : StudentDelete):
collection_name.delete_one({"name": info.name})
return {
"status" : "SUCCESS",
"data" : info
}