Desktop: review comments for commit 0383dcc

pull/2288/head
lightray22 2020-01-11 09:21:32 -05:00
parent 0383dcc865
commit 1fe4685d8a
2 changed files with 31 additions and 20 deletions

View File

@ -128,7 +128,29 @@ describe('models_Folder', function() {
}));
it('should add node counts', asyncTest(async () => {
let folders, foldersById;
let f1 = await Folder.save({ title: 'folder1' });
let f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
let f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
let f4 = await Folder.save({ title: 'folder4' });
let n1 = await Note.save({ title: 'note1', parent_id: f3.id });
let n2 = await Note.save({ title: 'note1', parent_id: f3.id });
let n3 = await Note.save({ title: 'note1', parent_id: f1.id });
const folders = await Folder.all();
await Folder.addNoteCounts(folders);
const foldersById = {};
folders.forEach((f) => { foldersById[f.id] = f; });
expect(folders.length).toBe(4);
expect(foldersById[f1.id].note_count).toBe(3);
expect(foldersById[f2.id].note_count).toBe(2);
expect(foldersById[f3.id].note_count).toBe(2);
expect(foldersById[f4.id].note_count).toBe(0);
}));
it('should not count completed to-dos', asyncTest(async () => {
let f1 = await Folder.save({ title: 'folder1' });
let f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
@ -142,22 +164,10 @@ describe('models_Folder', function() {
let n5 = await Note.save({ title: 'note5', parent_id: f3.id, is_todo: true, todo_completed: 999 });
let n6 = await Note.save({ title: 'note6', parent_id: f3.id, is_todo: true, todo_completed: 999 });
folders = await Folder.all();
await Folder.addNoteCounts(folders, true); // count completed
const folders = await Folder.all();
await Folder.addNoteCounts(folders, false);
foldersById = {};
folders.forEach((f) => { foldersById[f.id] = f; });
expect(folders.length).toBe(4);
expect(foldersById[f1.id].note_count).toBe(6);
expect(foldersById[f2.id].note_count).toBe(5);
expect(foldersById[f3.id].note_count).toBe(5);
expect(foldersById[f4.id].note_count).toBe(0);
folders = await Folder.all();
await Folder.addNoteCounts(folders, false); // don't count completed
foldersById = {};
const foldersById = {};
folders.forEach((f) => { foldersById[f.id] = f; });
expect(folders.length).toBe(4);

View File

@ -107,17 +107,18 @@ class Folder extends BaseItem {
// Calculates note counts for all folders and adds the note_count attribute to each folder
// Note: this only calculates the overall number of nodes for this folder and all its descendants
static async addNoteCounts(folders, showDone) {
static async addNoteCounts(folders, includeCompletedTodos = true) {
const foldersById = {};
folders.forEach((f) => {
foldersById[f.id] = f;
f.note_count = 0;
});
const where = !includeCompletedTodos ? 'WHERE (notes.is_todo = 0 OR notes.todo_completed = 0)' : '';
const sql = `SELECT folders.id as folder_id, count(notes.parent_id) as note_count
FROM folders LEFT JOIN notes ON notes.parent_id = folders.id`
+ (!showDone ? ' WHERE (notes.is_todo = 0 OR notes.todo_completed = 0)' : '')
+ ` GROUP BY folders.id`;
FROM folders LEFT JOIN notes ON notes.parent_id = folders.id
${where} GROUP BY folders.id`;
const noteCounts = await this.db().selectAll(sql);
noteCounts.forEach((noteCount) => {