feat: impl add & get notebooks

This commit is contained in:
thecodrr
2019-12-04 15:08:01 +05:00
parent fe349bbfdb
commit 8f64294274

View File

@@ -4,13 +4,15 @@ import ff from "fast-filter";
import { extractValues } from "../utils"; import { extractValues } from "../utils";
const KEYS = { const KEYS = {
notes: "notes" notes: "notes",
notebooks: "notebooks"
}; };
class Database { class Database {
constructor(storage) { constructor(storage) {
this.storage = new Storage(storage); this.storage = new Storage(storage);
this.notes = {}; this.notes = {};
this.notebooks = {};
} }
/** /**
@@ -88,7 +90,7 @@ class Database {
if (!query) return []; if (!query) return [];
//TODO benchmark this and make it faster if necessary //TODO benchmark this and make it faster if necessary
let notes = await this.getNotes(); let notes = await this.getNotes();
if (!notes) return; if (!notes) return [];
return ff( return ff(
notes, notes,
v => fuzzysearch(query, v.title + " " + v.content.text), v => fuzzysearch(query, v.title + " " + v.content.text),
@@ -97,9 +99,33 @@ class Database {
} }
//Notebooks //Notebooks
getNotebooks() {} async getNotebooks() {
getNotebook() {} //update our cache
addNotebook() {} this.notebooks = (await this.storage.read(KEYS.notebooks)) || {};
return extractValues(this.notebooks);
}
async addNotebook(notebook) {
if (!notebook || !notebook.title) return;
const id = notebook.dateCreated || Date.now();
let topics = {};
for (let topic of notebook.topics) {
topics[topic] = [];
}
this.notebooks[id] = {
title: notebook.title,
description: notebook.description,
dateCreated: notebook.dateCreated,
pinned: notebook.pinned || false,
favorite: notebook.favorite || false,
topics,
totalNotes: 0,
tags: [],
colors: []
};
await this.storage.write(KEYS.notebooks, this.notebooks);
return id;
}
getNotebook(id) {}
// Lists // Lists
getLists() {} getLists() {}