Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const FILE = path.join(__dirname, "..", "notes.json");
function load() {
try {
return JSON.parse(fs.readFileSync(FILE, "utf8"));
} catch {
return { nextId: 1, notes: [] };
} catch (e) {
// Only treat ENOENT (file not found) as OK
if (e.code === 'ENOENT') {
return { nextId: 1, notes: [] };
}
// Re-throw other errors (corruption, permissions, etc)
throw e;
}
}

Expand Down Expand Up @@ -45,4 +50,15 @@ function search(term) {
return matches(load().notes, term);
}

module.exports = { all, add, remove, search, matches };
function edit(id, text) {
const data = load();
const note = data.notes.find((n) => n.id === id);
if (!note){
return false;
}
note.text = text;
save(data);
return true;
}

module.exports = { all, add, remove, search, matches, edit };
14 changes: 13 additions & 1 deletion notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,26 @@ function main() {
}
break;
}
case "edit": {
const id = Number(rest[0]);
const text = rest.slice(1).join(" ").trim();
const ok = store.edit(id, text);
if (ok){
console.log(`Updated note #${id}`);
}
else{
console.log("Note #${id} not found");
}
break;
}
case "delete": {
const id = Number(rest[0]);
const ok = store.remove(id);
console.log(ok ? `Deleted note #${id}` : `No note #${id} found`);
break;
}
default:
console.log("Commands: add <text> | list | search <term> | delete <id>");
console.log("Commands: add <text> | list | search <term> | edit <id> <text> | delete <id>");
console.log(`(Session locks after ${config.SESSION_TIMEOUT_MINUTES} minutes of inactivity.)`);
}
}
Expand Down