diff --git a/src/db/_utils.js b/src/db/_utils.js index 89686f4..2c81f47 100644 --- a/src/db/_utils.js +++ b/src/db/_utils.js @@ -2,7 +2,7 @@ export function calculateNextShowTime(startTime, showedAmount) { const minutes = startTime + 1000 * 60 const hours = startTime + 1000 * 3600 const days = startTime + 1000 * 3600 * 24 - switch (showedAmount){ + switch (showedAmount) { case 0: return minutes * 10 // 10 minutes case 1: @@ -18,4 +18,27 @@ export function calculateNextShowTime(startTime, showedAmount) { case 6: return days * 30 * 4 // 4 month } +} + +export function compareWithTagsAndFolder(memos = [], tags, folder) { + const resultMemos = [] + + memos.forEach(memo => { + var folderOkey = false, tagsOkey = false + if (!folder) folderOkey = true + if (tags.length === 0) tagsOkey = true + + if (folder && memo.folder === folder) + folderOkey = true + + for (let index = 0; index < tags.length; index++) { + const tag = tags[index] + if (memo.tags.includes(tag)) { + tagsOkey = true; break + } + } + if (folderOkey && tagsOkey) resultMemos.push(memo) + }) + + return resultMemos } \ No newline at end of file diff --git a/src/db/api.js b/src/db/api.js index fdac966..2d06521 100644 --- a/src/db/api.js +++ b/src/db/api.js @@ -65,4 +65,23 @@ export function markMemoRepeated(memoId, callback = (err, res) => { }) { .then(res => callback(false, res)) .catch(err => callback(err, false)) } +} + +/** + * search: all memos {from, to} AND folder AND (memo tags contain any of given tags) + * @param {*} from date.getTime() if not provided will use minimum + * @param {*} to date.getTime() if not provided will use maximum + * @param {*} tags Search by OR + * @param {*} folder if no matter send undefined + * @param {*} callback (err, memo_list) + */ +export function searchMemo(from, to, tags = [], folder, callback) { + let openRequest = indexedDB.open("memos", 1) + + openRequest.onsuccess = function () { + let db = openRequest.result + controllers.searchMemo(db, from, to, tags, folder) + .then(res => callback(false, res)) + .catch(err => callback(err, false)) + } } \ No newline at end of file diff --git a/src/db/controllers.js b/src/db/controllers.js index 1a8f110..902fcaf 100644 --- a/src/db/controllers.js +++ b/src/db/controllers.js @@ -1,4 +1,4 @@ -import { calculateNextShowTime } from "./_utils" +import { calculateNextShowTime, compareWithTagsAndFolder } from "./_utils" export async function addNewMemo(db, memoText, startShowAt, tags, folder) { return await new Promise((resolve, reject) => { @@ -52,7 +52,7 @@ export async function searchUnresolvedMemos(db) { resolve(request.result) } request.onerror = function (e) { - reject('error searching book in db', e) + reject('error searching unresolved memos in db', e) } }) } @@ -72,7 +72,28 @@ export async function handleMemoRepeated(db, memoId) { resolve(true) } request.onerror = function (e) { - reject('error searching book in db', e) + reject('error memo handle repeated', e) + } + }) +} + +export async function searchMemo(db, from, to, tags, folder) { + return await new Promise((resolve, reject) => { + if (!from) from = 0 + if (!to) to = 3691082609569 + let transaction = db.transaction("memos"); + let memos = transaction.objectStore('memos') + let timeShowIndex = memos.index('time_index') + + let request = timeShowIndex.getAll(IDBKeyRange.bound(from, to)) + + request.onsuccess = function () { + console.log('From to result: ',request.result); + const memos = compareWithTagsAndFolder(request.result, tags, folder) + resolve(memos) + } + request.onerror = function (e) { + reject('error searching memo in db', e) } }) } \ No newline at end of file diff --git a/src/db/index.js b/src/db/index.js index 91b3933..05aa6ed 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -9,12 +9,10 @@ export function initdb() { if (!db.objectStoreNames.contains("memos")) { let memos = db.createObjectStore("memos", { keyPath: "id" }) memos.createIndex('time_index', 'timeNextShow') - memos.createIndex('folder_index', 'folder') } else { let memos = txn.objectStore('memos') memos.createIndex('time_index', 'timeNextShow') - memos.createIndex('folder_index', 'folder') } }