Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search memo #2

Open
wants to merge 1 commit into
base: v2.0-develop
Choose a base branch
from
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
25 changes: 24 additions & 1 deletion src/db/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions src/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
27 changes: 24 additions & 3 deletions src/db/controllers.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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)
}
})
}
Expand All @@ -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)
}
})
}
2 changes: 0 additions & 2 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}

Expand Down