summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLeo Goetz <dev@leogtz.de>2025-11-09 14:19:26 +0100
committerLeo Goetz <dev@leogtz.de>2025-11-09 14:19:26 +0100
commit789d28b83af165258dc7e8b3a44bbd2d48fcc370 (patch)
treee98ef728fb42a7bc03aef6d6a601e0533a4961b3 /src
parent16a0e9a9d2c24af28142ed3420ef6cb373c454f7 (diff)
added crud functionality with json file as a db
Diffstat (limited to 'src')
-rw-r--r--src/command.js34
-rw-r--r--src/db.js3
-rw-r--r--src/notes.js37
-rw-r--r--src/utils.js8
4 files changed, 76 insertions, 6 deletions
diff --git a/src/command.js b/src/command.js
index 20a23b2..c022c81 100644
--- a/src/command.js
+++ b/src/command.js
@@ -1,5 +1,13 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
+import {
+ findNotes,
+ getAllNotes,
+ newNote,
+ removeAllNotes,
+ removeNote,
+} from "./notes.js";
+import { listNotes } from "./utils.js";
yargs(hideBin(process.argv))
.command(
@@ -11,8 +19,10 @@ yargs(hideBin(process.argv))
description: "The content of the note to create",
});
},
- (argv) => {
- console.log(argv.note);
+ async (argv) => {
+ const tags = argv.tags ? argv.tags.split(",") : [];
+ const note = await newNote(argv.note, tags);
+ console.log("New Note: ", note);
},
)
.option("tags", {
@@ -24,7 +34,10 @@ yargs(hideBin(process.argv))
"all",
"get all notes",
() => {},
- async (argv) => {},
+ async () => {
+ const notes = await getAllNotes();
+ listNotes(notes);
+ },
)
.command(
"find <filter>",
@@ -36,7 +49,10 @@ yargs(hideBin(process.argv))
type: "string",
});
},
- async (argv) => {},
+ async (argv) => {
+ const matches = await findNotes(argv.filter);
+ listNotes(matches);
+ },
)
.command(
"remove <id>",
@@ -47,7 +63,10 @@ yargs(hideBin(process.argv))
description: "The id of the note you want to remove",
});
},
- async (argv) => {},
+ async (argv) => {
+ const id = await removeNote(argv.id);
+ console.log(id);
+ },
)
.command(
"web [port]",
@@ -65,7 +84,10 @@ yargs(hideBin(process.argv))
"clean",
"remove all notes",
() => {},
- async (argv) => {},
+ async (argv) => {
+ await removeAllNotes();
+ console.log("db reseted");
+ },
)
.demandCommand(1)
.parse();
diff --git a/src/db.js b/src/db.js
index 39fdc29..087686a 100644
--- a/src/db.js
+++ b/src/db.js
@@ -14,4 +14,7 @@ export const saveDB = async (db) => {
export const insertDB = async (note) => {
const db = await getDB();
+ db.notes.push(note);
+ await saveDB(db);
+ return note;
};
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: [] });
diff --git a/src/utils.js b/src/utils.js
new file mode 100644
index 0000000..4153ada
--- /dev/null
+++ b/src/utils.js
@@ -0,0 +1,8 @@
+export const listNotes = (notes) => {
+ notes.forEach(({ id, content, tags }) => {
+ console.log("id: ", id);
+ console.log("tags: ", tags);
+ console.log("content: ", content);
+ console.log("\n");
+ });
+};