2020-03-02 12:36:10 +05:00
|
|
|
import { qclone } from "qclone";
|
2020-02-05 20:57:43 +05:00
|
|
|
export default class Topic {
|
|
|
|
|
/**
|
|
|
|
|
*
|
2020-04-12 11:04:30 +05:00
|
|
|
* @param {import('../collections/topics').default} topics
|
2020-02-05 20:57:43 +05:00
|
|
|
* @param {Object} topic
|
|
|
|
|
*/
|
|
|
|
|
constructor(topics, topic) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._topic = topic;
|
|
|
|
|
this._topics = topics;
|
|
|
|
|
this._transactionOpen = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get totalNotes() {
|
|
|
|
|
return this._topic.totalNotes;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction(ops, saveAfter = true) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._transactionOpen = true;
|
2020-02-05 20:57:43 +05:00
|
|
|
ops().then(() => {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._transactionOpen = false;
|
2020-02-05 20:57:43 +05:00
|
|
|
});
|
|
|
|
|
if (!saveAfter) return this;
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._save();
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has(noteId) {
|
2020-04-12 11:04:30 +05:00
|
|
|
return this._topic.notes.findIndex((n) => n === noteId) > -1;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(...noteIds) {
|
2020-03-02 12:36:10 +05:00
|
|
|
const topic = qclone(this._topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
for (let noteId of noteIds) {
|
2020-02-22 21:53:56 +05:00
|
|
|
let note = this._topics._notebooks._notes.note(noteId);
|
2020-03-23 13:22:28 +05:00
|
|
|
if (this.has(noteId) || !note || note.data.deleted) continue;
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.notes.push(noteId);
|
2020-02-05 20:57:43 +05:00
|
|
|
if (note.notebook && note.notebook.id && note.notebook.topic) {
|
|
|
|
|
if (
|
2020-02-22 21:53:56 +05:00
|
|
|
note.notebook.id === this._topics.notebookId &&
|
|
|
|
|
note.notebook.topic === topic.title
|
2020-02-05 20:57:43 +05:00
|
|
|
)
|
|
|
|
|
return this;
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._topics._notebooks
|
2020-03-02 10:29:29 +05:00
|
|
|
.notebook(note.notebook.id)
|
|
|
|
|
.topics.topic(note.notebook.topic)
|
2020-02-05 20:57:43 +05:00
|
|
|
.delete(note.id);
|
|
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._topics._notebooks._notes.add({
|
2020-02-05 20:57:43 +05:00
|
|
|
id: noteId,
|
2020-04-12 11:04:30 +05:00
|
|
|
notebook: { id: this._topics._notebookId, topic: topic.title },
|
2020-02-05 20:57:43 +05:00
|
|
|
});
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.totalNotes++;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
return await this._save(topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(...noteIds) {
|
2020-03-03 22:10:13 +05:00
|
|
|
const topic = qclone(this._topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
for (let noteId of noteIds) {
|
|
|
|
|
if (!this.has(noteId)) return this;
|
2020-04-12 11:04:30 +05:00
|
|
|
let index = topic.notes.findIndex((n) => n === noteId);
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.notes.splice(index, 1);
|
|
|
|
|
await this._topics._notebooks._notes.add({
|
2020-02-05 20:57:43 +05:00
|
|
|
id: noteId,
|
2020-04-12 11:04:30 +05:00
|
|
|
notebook: {},
|
2020-02-05 20:57:43 +05:00
|
|
|
});
|
2020-02-22 21:53:56 +05:00
|
|
|
topic.totalNotes--;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
return await this._save(topic);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
async _save(topic) {
|
|
|
|
|
if (this._transactionOpen) return this;
|
|
|
|
|
await this._topics.add(topic);
|
2020-02-06 18:47:42 +05:00
|
|
|
return this;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._topic.notes
|
2020-04-12 11:04:30 +05:00
|
|
|
.map((note) => {
|
2020-02-22 21:53:56 +05:00
|
|
|
let fullNote = this._topics._notebooks._notes.note(note);
|
|
|
|
|
if (fullNote) return fullNote.data;
|
|
|
|
|
})
|
2020-04-12 11:04:30 +05:00
|
|
|
.filter((v) => v);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
}
|