diff --git a/Code With Harry/.gitignore b/Code With Harry/.gitignore new file mode 100644 index 0000000..f072b81 --- /dev/null +++ b/Code With Harry/.gitignore @@ -0,0 +1,3 @@ +.venv +.mypy_cache +__pycache__ diff --git a/Code With Harry/README.md b/Code With Harry/README.md new file mode 100644 index 0000000..ccabab9 --- /dev/null +++ b/Code With Harry/README.md @@ -0,0 +1,3 @@ +# Warninig Plase Don't see this video west of time + +[YouTube Video Link](https://youtu.be/52c7Kxp_14E?si=uli6CtFBPcJinPk1) diff --git a/Code With Harry/config/db.py b/Code With Harry/config/db.py new file mode 100644 index 0000000..35093b7 --- /dev/null +++ b/Code With Harry/config/db.py @@ -0,0 +1,5 @@ +from pymongo import MongoClient + +MONGO_URI = "mongodb://localhost:27017/" + +conn = MongoClient(MONGO_URI) \ No newline at end of file diff --git a/Code With Harry/index.py b/Code With Harry/index.py new file mode 100644 index 0000000..12fbe57 --- /dev/null +++ b/Code With Harry/index.py @@ -0,0 +1,8 @@ +from fastapi import FastAPI +from routes.note import note +from fastapi.staticfiles import StaticFiles + + +app = FastAPI() +app.mount("/static", StaticFiles(directory="static"), name="static") +app.include_router(note) diff --git a/Code With Harry/models/note.py b/Code With Harry/models/note.py new file mode 100644 index 0000000..d65c77b --- /dev/null +++ b/Code With Harry/models/note.py @@ -0,0 +1,7 @@ +from pydantic import BaseModel + + +class Note(BaseModel): + title: str + desc: str + important: bool = None \ No newline at end of file diff --git a/Code With Harry/requirements.txt b/Code With Harry/requirements.txt new file mode 100644 index 0000000..feb750a --- /dev/null +++ b/Code With Harry/requirements.txt @@ -0,0 +1,5 @@ +fastapi==0.110.0 +uvicorn==0.27.1 +Jinja2==3.1.3 +pymongo==4.6.2 +python-multipart==0.0.9 diff --git a/Code With Harry/routes/note.py b/Code With Harry/routes/note.py new file mode 100644 index 0000000..4509e18 --- /dev/null +++ b/Code With Harry/routes/note.py @@ -0,0 +1,34 @@ +from fastapi import APIRouter, Request +from fastapi.responses import HTMLResponse +from models.note import Note +from config.db import conn +from schemas.note import noteEntity, notesEntity +from jinja2 import Template +from fastapi.templating import Jinja2Templates + + +templates = Jinja2Templates(directory="templates") + +note = APIRouter() + +@note.get("/", response_class=HTMLResponse) +async def read_item(request: Request): + docs = conn.notes.notes.find({}) + newDocs=[] + + for doc in docs: + newDocs.append({ + "id": doc["_id"], + "title": doc["title"], + "desc": doc["desc"] + }) + + return templates.TemplateResponse("index.html", {"request": request, "newDocs": newDocs}) + +@note.post('/') +async def create_item(request: Request): + form = await request.form() + formDict = dict(form) + formDict["important"] = True if formDict.get("important") == "on" else False + note = conn.notes.notes.insert_one(formDict) + return {"Success": True} \ No newline at end of file diff --git a/Code With Harry/schemas/note.py b/Code With Harry/schemas/note.py new file mode 100644 index 0000000..0004238 --- /dev/null +++ b/Code With Harry/schemas/note.py @@ -0,0 +1,10 @@ +def noteEntity(item) -> dict: + return { + "id": str(item["_id"]), + "title": item["title"], + "desc": item["desc"], + "important": item["important"], + } + +def notesEntity(items) -> list: + return [noteEntity(item) for item in items] \ No newline at end of file diff --git a/Code With Harry/templates/index.html b/Code With Harry/templates/index.html new file mode 100644 index 0000000..b4fa8d5 --- /dev/null +++ b/Code With Harry/templates/index.html @@ -0,0 +1,69 @@ + + + + + + iNotes - Take your notes easy-peasy! + + + + + +
+

Start adding your note to the database

+ +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ +
+

Your Notes

+ + {% if newDocs is not defined %} +

No notes to display

+ {% endif %} +
+ {% for item in newDocs %} +

{{item.title}}

+

{{item.desc}}

+ {% endfor %} +
+
+ + + + \ No newline at end of file