1+ import os
2+ from dotenv import load_dotenv
3+ import pymongo
4+ from bson import ObjectId
5+
6+ load_dotenv ()
7+ DB_URL = os .getenv ("DB_URL" )
8+
9+ client = pymongo .MongoClient (DB_URL )
10+
11+ db = client ["noteapp" ]
12+ notes_col = db ["notes" ]
13+ # note = {
14+ # "title":"This is the title of note",
15+ # "desc":"This is descriptiong of note"
16+ # }
17+ # res = notes_col.insert_one(note)
18+ # print(res)
19+ def show_notes ():
20+ notes = notes_col .find ()
21+ if notes :
22+ for note in notes :
23+ print (f"id : { note ["_id" ]} " )
24+ print (f"title : { note ["title" ]} " )
25+ print (f"desc : { note ["desc" ]} " )
26+ else :
27+ print ("No notes found." )
28+
29+ def add_note ():
30+ title = input ("Enter title of note: " )
31+ desc = input ("Enter description of note: " )
32+ notes_col .insert_one ({"title" : title ,"desc" : desc })
33+ print ("Note added successfully." )
34+
35+ def update_note ():
36+ id = input ("Enter the id of note: " )
37+ new_title = input ("Enter new title of note: " )
38+ new_desc = input ("Enter new description of note: " )
39+ notes_col .update_one ({"_id" :ObjectId (id )},{"$set" : {"title" : new_title , "desc" : new_desc }})
40+ print ("Note updated successfully." )
41+
42+ def delete_note ():
43+ id = input ("Enter the id of note: " )
44+ notes_col .delete_one ({"_id" :ObjectId (id )})
45+ print ("Note deleted successfully." )
46+
47+ def main ():
48+ while True :
49+ print ("\n Note Taking App with Database\n " )
50+ print ("1. Show Notes" )
51+ print ("2. Add Note" )
52+ print ("3. Update Note" )
53+ print ("4. Delete Note" )
54+ print ("5. Exit" )
55+
56+ choice = input ("select options (1/2/3/4/5): " )
57+ if choice == "1" :
58+ show_notes ()
59+ elif choice == "2" :
60+ add_note ()
61+ elif choice == "3" :
62+ update_note ()
63+ elif choice == "4" :
64+ delete_note ()
65+ elif choice == "5" :
66+ print ("Thanks for using this app" )
67+ break
68+ else :
69+ print ("Please select valid option." )
70+ if __name__ == "__main__" :
71+ main ()
0 commit comments