-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (61 loc) · 2.18 KB
/
main.py
File metadata and controls
83 lines (61 loc) · 2.18 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
83
from fastapi import Body, FastAPI, Form, Path, Request, HTTPException
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from pymongo import MongoClient
from pydantic import BaseModel
from typing import Optional, List
from fastapi.responses import RedirectResponse
from bson import ObjectId
app = FastAPI()
temp = Jinja2Templates(directory="view")
app.mount("/static", StaticFiles(directory="static"))
# Database setup
mongo_url = "mongodb://localhost:27017"
client = MongoClient(mongo_url)
db = client.Fast_API
collection = db.my_notes
# Schema for DB
class Db_schema(BaseModel):
title: Optional[str] = None
desc: Optional[str] = None
@app.get("/")
def home_page(request: Request):
# Database Request and Getting the all records
all_records = list(collection.find().sort({"_id": -1}))
# Let's split the IDs of documents separately for the edit and delete button
all_IDs = []
for one in all_records:
all_IDs.append(str(one["_id"]))
return temp.TemplateResponse("home.html", {"request": request,
"all_records": all_records,
"all_IDs": all_IDs
})
@app.post("/submit_notes")
def note_submission(note_title: str = Form(...), note_desc: str = Form(...)):
res = collection.insert_one({
"title": note_title,
"desc": note_desc
})
if res.acknowledged != True:
raise HTTPException(status_code=404, detail=res)
return RedirectResponse(url="/", status_code=303)
# Patch Request
@app.post("/update_note/{note_id}")
def update_single_note(note_id: str, note_title: str = Form(...), note_desc: str = Form(...)):
res = collection.find_one_and_update(
{"_id": ObjectId(note_id)},
{"$set": {
"title": note_title,
"desc": note_desc
}},
# return_document=True
)
return RedirectResponse(url="/", status_code=303)
@app.get("/delete_note/{note_id}")
def delete_one_note(note_id: str):
collection.find_one_and_delete(
{
"_id": ObjectId(note_id)
}
)
return RedirectResponse(url="/", status_code=303)