mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
feat: implement fluent api
This commit is contained in:
75
packages/core/models/topic.js
Normal file
75
packages/core/models/topic.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import Topics from "../collections/topics";
|
||||
|
||||
export default class Topic {
|
||||
/**
|
||||
*
|
||||
* @param {Topics} topics
|
||||
* @param {Object} topic
|
||||
*/
|
||||
constructor(topics, topic) {
|
||||
this.topic = topic;
|
||||
this.topics = topics;
|
||||
this.transactionOpen = false;
|
||||
}
|
||||
|
||||
transaction(ops, saveAfter = true) {
|
||||
this.transactionOpen = true;
|
||||
ops().then(() => {
|
||||
this.transactionOpen = false;
|
||||
});
|
||||
if (!saveAfter) return this;
|
||||
return this.save();
|
||||
}
|
||||
|
||||
has(noteId) {
|
||||
return this.topic.notes.findIndex(n => n === noteId) > -1;
|
||||
}
|
||||
|
||||
async add(...noteIds) {
|
||||
for (let noteId of noteIds) {
|
||||
let note = this.topics.notes.note(noteId);
|
||||
if (this.has(noteId) || !note) return this;
|
||||
|
||||
this.topic.notes.push(noteId);
|
||||
|
||||
if (note.notebook && note.notebook.id && note.notebook.topic) {
|
||||
if (
|
||||
note.notebook.id === this.topics.notebookId &&
|
||||
note.notebook.topic === this.topic.title
|
||||
)
|
||||
return this;
|
||||
await this.topics.notebooks
|
||||
.topics(note.notebook.id)
|
||||
.topic(note.notebook.topic)
|
||||
.delete(note.id);
|
||||
}
|
||||
await this.topics.notes.add({
|
||||
id: noteId,
|
||||
notebook: { id: this.topics.notebookId, topic: this.topic.title }
|
||||
});
|
||||
}
|
||||
return await this.save();
|
||||
}
|
||||
|
||||
async delete(...noteIds) {
|
||||
for (let noteId of noteIds) {
|
||||
if (!this.has(noteId)) return this;
|
||||
let index = this.topic.notes.findIndex(n => n === noteId);
|
||||
this.topic.notes.splice(index, 1);
|
||||
await this.topics.notes.add({
|
||||
id: noteId,
|
||||
notebook: {}
|
||||
});
|
||||
}
|
||||
return await this.save();
|
||||
}
|
||||
|
||||
save() {
|
||||
if (this.transactionOpen) return this;
|
||||
return this.topics.add(this.topic);
|
||||
}
|
||||
|
||||
get all() {
|
||||
return this.topic.notes.map(note => this.topics.notes.note(note).note);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user