diff --git a/index.js b/index.js index 759ece5..05a1673 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,23 @@ if(!fs.existsSync(filePath)){ fs.writeFileSync(filePath, JSON.stringify([])) } +function loadNotes(){ + return JSON.parse(fs.readFileSync(filePath, "utf-8")); +} + +function saveNotes(notes){ + fs.writeFileSync(filePath, JSON.stringify(notes, null, 2)); +} + +function generateId(notes){ + if(notes.length === 0){ + return 1; + } + + //find the max id and add 1 with it + return Math.max(...notes.map(note => note.id)) + 1; +} + const readline = rl.createInterface({ input : process.stdin, output : process.stdout @@ -16,23 +33,74 @@ const readline = rl.createInterface({ function addNote (note){ const notes = JSON.parse(fs.readFileSync(filePath, "utf-8")); - let id; + // let id; - if(notes.length === 0){ - id="1"; - }else{ - id = `${notes.length + 1}`; - } + // if(notes.length === 0){ + // id="1"; + // }else{ + // id = `${notes.length + 1}`; + // } const newNote = { - id : id, + id : generateId(notes), note, createdAt : new Date().toISOString(), updatedAt : null } notes.push(newNote) - fs.writeFileSync(filePath, JSON.stringify(notes, null, 2)) - console.log("Note added successfully"); + saveNotes(notes) + console.log("✅ Note added successfully"); +} + +function listNotes() { + const notes = loadNotes(); + if (notes.length === 0) { + console.log("No notes found.\n"); + return; + } + notes.forEach((note) => { + console.log( + `ID No: ${note.id} : ${note.note} (Created: ${note.createdAt}${ + note.updatedAt ? ` | Updated: ${note.updatedAt}` : "" + })` + ); + }); + console.log(""); +} + +function getNote(id) { + const notes = loadNotes(); + const note = notes.find((n) => n.id === Number(id)); + if (!note) { + console.log("❌ Note not found\n"); + return; + } + console.log(note, "\n"); +} + +function updateNote(id, newNoteMessage) { + const notes = loadNotes(); + const note = notes.find((n) => n.id === id); + if (!note) { + console.log("❌ Note not found\n"); + return; + } + note.note = newNoteMessage; + note.updatedAt = new Date().toISOString(); + saveNotes(notes); + console.log("✏️ Note updated successfully!\n"); +} + +function deleteNote(id) { + let notes = loadNotes(); + let initialLength = notes.length; + notes = notes.filter((n) => n.id !== id); + if (notes.length === initialLength) { + console.log("❌ Note not found\n"); + return; + } + saveNotes(notes); + console.log("🗑️ Note deleted successfully!\n"); } function showMenu(){ @@ -46,26 +114,44 @@ function showMenu(){ `) readline.question("Select an option:", (choice)=> { - switch (choice){ - case "1" : - readline.question("Please write your note: ", (note)=>{ - addNote(note) - showMenu(); - }); - break; - - case "2" : - const notes = JSON.parse( - fs.readFileSync(filePath, "utf-8") - ); - console.log(notes); - break; - case "6": - readline.close(); - break; - default: - console.log("Invalid Option. Try Again..."); - showMenu() + switch (choice.trim()) { + case "1": + readline.question("Please write your note: ", (note) => { + addNote(note); + showMenu(); + }); + break; + + case "2": + listNotes(); + showMenu(); + break; + case "3": + readline.question("Enter note ID: ", (id) => { + getNote(id.trim()); + showMenu(); + }); + break; + case "4": + readline.question("Enter note ID to update: ", (id) => { + readline.question("Enter new text: ", (newNoteMessage) => { + updateNote(id.trim(), newNoteMessage); + showMenu(); + }); + }); + break; + case "5": + readline.question("Enter note ID to delete: ", (id) => { + deleteNote(id.trim()); + showMenu(); + }); + break; + case "6": + readline.close(); + break; + default: + console.log("Invalid Option. Try Again..."); + showMenu(); } } ) } diff --git a/notes.json b/notes.json index b43dfc6..cfdd2e6 100644 --- a/notes.json +++ b/notes.json @@ -23,22 +23,28 @@ "createdAt": "2025-10-03T17:21:33.645Z", "updatedAt": null }, - { - "id": "5", - "note": "nextjs", - "createdAt": "2025-10-03T17:22:40.605Z", - "updatedAt": null - }, { "id": "6", - "note": "golang", + "note": "golang gopher", "createdAt": "2025-10-03T17:24:37.729Z", - "updatedAt": null + "updatedAt": "2025-10-04T09:23:09.200Z" }, { "id": "7", "note": "postgres", "createdAt": "2025-10-03T17:25:41.635Z", "updatedAt": null + }, + { + "id": 8, + "note": "hello", + "createdAt": "2025-10-04T09:19:15.749Z", + "updatedAt": null + }, + { + "id": 9, + "note": "efawefa", + "createdAt": "2025-10-04T09:22:38.564Z", + "updatedAt": null } ] \ No newline at end of file