-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (49 loc) · 1.46 KB
/
main.py
File metadata and controls
74 lines (49 loc) · 1.46 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
from pymongo.mongo_client import MongoClient
from dotenv import load_dotenv
from bson.objectid import ObjectId
from fastapi import FastAPI
app = FastAPI()
load_dotenv()
from os import getenv
uri = getenv("MONGO_URI")
# Create a new client and connect to the server
client = MongoClient(uri)
db = client["students"]
coll = db["all_students"]
def add_data(name: dict, roll: int, marks: int) -> str:
"""db te student data add kore"""
res = coll.insert_one({"name": name, "roll": roll, "marks": marks})
return str(res.inserted_id)
def show_data() -> list:
"""student data show kore"""
res = coll.find({})
return list(res)
def edit_data(id: str, name: str):
res = coll.update_one({"_id": ObjectId(id)}, {"$set": {"name": name}})
return res.modified_count
def delete_data(id: str):
res = coll.delete_one({"_id": ObjectId(id)})
return int(res.deleted_count)
@app.get("/")
def home():
return "Hello!!"
@app.get("/show")
def show():
alldata = show_data()
res: list = []
for data in alldata:
data["_id"] = str(data["_id"])
res.append(data)
return {"data": res}
@app.post("/add")
def add(name: str, roll: int, marks: int):
res = add_data(name=name, roll=roll, marks=marks)
return {"status": res}
@app.post("/edit")
def edit(id: str, name: str):
res = edit_data(id, name)
return {"status": res}
@app.get("/remove")
def remove(id: str):
res = delete_data(id)
return {"status": res}