Skip to content

Commit 2e51a73

Browse files
committed
first
0 parents  commit 2e51a73

File tree

20 files changed

+258
-0
lines changed

20 files changed

+258
-0
lines changed

Diff for: .idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/FastAPI.iml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: __pycache__/index.cpython-311.pyc

665 Bytes
Binary file not shown.

Diff for: __pycache__/main.cpython-311.pyc

850 Bytes
Binary file not shown.

Diff for: config/__pycache__/db.cpython-311.pyc

337 Bytes
Binary file not shown.

Diff for: config/db.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pymongo import MongoClient
2+
MONGO_URI = "mongodb+srv://<user>:<password>@cluster0.afakb9j.mongodb.net/notes"
3+
4+
conn = MongoClient(MONGO_URI)

Diff for: index.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import FastAPI
2+
from routes.note import note
3+
from fastapi.staticfiles import StaticFiles
4+
from fastapi.templating import Jinja2Templates
5+
6+
app = FastAPI()
7+
app.mount("/static", StaticFiles(directory="static"), name="static")
8+
app.include_router(note)

Diff for: main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# from typing import Union
2+
3+
from fastapi import FastAPI, Request
4+
from fastapi.responses import HTMLResponse
5+
from fastapi.staticfiles import StaticFiles
6+
from fastapi.templating import Jinja2Templates
7+
from pymongo import MongoClient
8+
9+
app = FastAPI()
10+
app.mount("/static", StaticFiles(directory="static"), name="static")
11+
templates = Jinja2Templates(directory="templates")
12+
13+
conn = MongoClient("mongodb+srv://rahul:[email protected]/notes")
14+
15+

Diff for: models/__pycache__/note.cpython-311.pyc

564 Bytes
Binary file not shown.

Diff for: models/note.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pydantic import BaseModel
2+
3+
4+
class Note(BaseModel):
5+
title: str
6+
desc: str
7+
important: bool = None

Diff for: requirements

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
annotated-types==0.6.0
2+
anyio==4.2.0
3+
click==8.1.7
4+
colorama==0.4.6
5+
dnspython==2.4.2
6+
fastapi==0.108.0
7+
h11==0.14.0
8+
httptools==0.6.1
9+
idna==3.6
10+
Jinja2==3.1.2
11+
MarkupSafe==2.1.3
12+
pydantic==2.5.3
13+
pydantic_core==2.14.6
14+
pymongo==4.6.1
15+
python-dotenv==1.0.0
16+
python-multipart==0.0.6
17+
PyYAML==6.0.1
18+
sniffio==1.3.0
19+
starlette==0.32.0.post1
20+
typing_extensions==4.9.0
21+
uvicorn==0.25.0
22+
watchfiles==0.21.0
23+
websockets==12.0

Diff for: routes/__pycache__/note.cpython-311.pyc

2.12 KB
Binary file not shown.

Diff for: routes/note.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from fastapi import APIRouter
2+
from fastapi import FastAPI, Request
3+
from fastapi.responses import HTMLResponse
4+
from fastapi.templating import Jinja2Templates
5+
from models.note import Note
6+
from config.db import conn
7+
from schemas.note import noteEntity, notesEntity
8+
9+
note = APIRouter()
10+
templates = Jinja2Templates(directory="templates")
11+
12+
13+
@note.get("/", response_class=HTMLResponse)
14+
async def read_item(request: Request):
15+
docs = conn.notes.notes.find({})
16+
newDocs = []
17+
for doc in docs:
18+
newDocs.append({
19+
"id": doc["_id"],
20+
"title": doc["title"],
21+
"desc": doc["desc"],
22+
"important": doc["important"],
23+
})
24+
return templates.TemplateResponse("index.html", {"request": request, "newDocs": newDocs})
25+
26+
27+
@note.post("/")
28+
async def create_item(request: Request):
29+
form = await request.form()
30+
formDict = dict(form)
31+
formDict["important"] = True if formDict.get("important") == "on" else False
32+
note = conn.notes.notes.insert_one(formDict)
33+
return {"success": True}

Diff for: schemas/__pycache__/note.cpython-311.pyc

870 Bytes
Binary file not shown.

Diff for: schemas/note.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def noteEntity(item) -> dict:
2+
return {
3+
"_id": str(item["_id"]),
4+
"title": item["title"],
5+
"desc": item["desc"],
6+
"important": item["important"]
7+
}
8+
9+
10+
def notesEntity(items) -> list:
11+
return [noteEntity(item) for item in items]

Diff for: static/style.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
css

Diff for: templates/index.html

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
9+
<title>index</title>
10+
<style>
11+
.nav{
12+
background-color:#222;
13+
overflow:hidden;
14+
}
15+
.nav a{
16+
float:left;
17+
display: block;
18+
color: #fff;
19+
text-align: center;
20+
padding: 15px 17px;
21+
text-decoration: none;
22+
font-size: 18px;
23+
}
24+
.nav a:hover{
25+
background-color: #ddd;
26+
color: white;
27+
}
28+
.nav a.active{
29+
background-color: blue;
30+
color: white;
31+
}
32+
.nav .icon{
33+
display: none;
34+
}
35+
@media screen and (max-width: 600px) {
36+
.nav a:not(:first-child) {display: none;}
37+
.nav a.icon {
38+
float: right;
39+
display: block;
40+
}
41+
}
42+
@media screen and (max-width: 600px) {
43+
.nav.responsive {position: relative;}
44+
.nav.responsive a.icon {
45+
position: absolute;
46+
right: 0;
47+
top: 0;
48+
}
49+
.nav.responsive a {
50+
float: none;
51+
display: block;
52+
text-align: left;
53+
}
54+
}
55+
.container{
56+
height: 500px;
57+
background-color: red;
58+
}
59+
.container h1{
60+
text-align: center;
61+
}
62+
.container form{
63+
height: 200px;
64+
margin: 0;
65+
padding: 0;
66+
display: flex;
67+
justify-content: center;
68+
69+
}
70+
</style>
71+
</head>
72+
<body>
73+
<nav>
74+
<div class="nav" id="myNav">
75+
<a class="active">Home</a>
76+
<a>About</a>
77+
<a>Contact</a>
78+
<a href="javascript:void(0);" class="icon" onclick="myFun()">
79+
<i class="fa fa-bars"></i>
80+
</a>
81+
</div>
82+
</nav>
83+
<div class="container">
84+
<h1>Adding your notes in Database</h1>
85+
<form action="/" method="post">
86+
<pre>
87+
<label for="text">Name</label>
88+
<input id="text" type="text" name="title" placeholder="Enter Name" required><br>
89+
<label for="des">Desc</label>
90+
<textarea id="des" name="desc" placeholder="Enter Description" cols="30" rows="5" required></textarea><br>
91+
<input type="submit" name="submit">
92+
</pre>
93+
</form>
94+
</div>
95+
<div class="container">
96+
<h1>Your Notes</h1>
97+
{% if newDocs is defined %}
98+
No Notes to display
99+
{% endif %}
100+
{% for item in newDocs %}
101+
<div>{{item.note}}</div>
102+
{% endfor %}
103+
</div>
104+
<script>
105+
function myFun() {
106+
var x = document.getElementById("myNav");
107+
if (x.className === "nav") {
108+
x.className += " responsive";
109+
} else {
110+
x.className = "nav";
111+
}
112+
}
113+
</script>
114+
</body>
115+
</html>

0 commit comments

Comments
 (0)