From 789d28b83af165258dc7e8b3a44bbd2d48fcc370 Mon Sep 17 00:00:00 2001 From: Leo Goetz Date: Sun, 9 Nov 2025 14:19:26 +0100 Subject: added crud functionality with json file as a db --- src/notes.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/notes.js (limited to 'src/notes.js') diff --git a/src/notes.js b/src/notes.js new file mode 100644 index 0000000..7a34950 --- /dev/null +++ b/src/notes.js @@ -0,0 +1,37 @@ +import { getDB, saveDB, insertDB } from "./db.js"; + +export const newNote = async (note, tags) => { + const newNote = { + tags, + id: Date.now(), + content: note, + }; + + await insertDB(newNote); + return newNote; +}; + +export const getAllNotes = async () => { + const { notes } = await getDB(); + return notes; +}; + +export const findNotes = async (filter) => { + const { notes } = await getDB(); + return notes.filter((note) => + note.content.toLowerCase().includes(filter.toLowerCase()), + ); +}; + +export const removeNote = async (id) => { + const { notes } = await getDB(); + const match = notes.find((note) => note.id === id); + + if (match) { + const newNotes = notes.filter((note) => note.id !== id); + await saveDB({ notes: newNotes }); + return id; + } +}; + +export const removeAllNotes = () => saveDB({ notes: [] }); -- cgit v1.3