aboutsummaryrefslogtreecommitdiffstats
path: root/models/NoteList.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/NoteList.js')
-rw-r--r--models/NoteList.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/models/NoteList.js b/models/NoteList.js
index f07ea50..1cac218 100644
--- a/models/NoteList.js
+++ b/models/NoteList.js
@@ -1,5 +1,8 @@
+const {encrypt, decrypt} = require('lib/crypt')
const mongoose = require("mongoose")
+const decryptTitles = (l) => ({notes: l.notes.map(n => ({ ...n, title: decrypt(n.title)}))})
+
const noteListSchema = new mongoose.Schema({
notes: [{
title: {
@@ -16,6 +19,45 @@ const noteListSchema = new mongoose.Schema({
}]
})
+
+noteListSchema.statics.getList = async (id) => {
+ const newList = await NoteList.findById(id).lean()
+
+ return {notes: newList.notes.map(n => ({ ...n, title: decrypt(n.title)}))}
+}
+
+noteListSchema.statics.getNoteId = async (_id, id) => {
+ const noteList = await NoteList.findById(_id).lean()
+ return noteList.notes
+ .find(n => n._id.toString() === id.toString()).noteId
+}
+
+noteListSchema.statics.addNote = async (_id, noteId, title) => {
+ const noteList = await NoteList.findOneAndUpdate(
+ {_id}, {$push: {notes: {title: encrypt(title ? title : 'no title'), noteId}}}, {new: true}
+ ).lean()
+
+ return decryptTitles(noteList)
+}
+
+noteListSchema.statics.removeNote = async (_id, id) => {
+ const noteList = await NoteList.findOneAndUpdate(
+ {_id}, {$pull: {notes: {_id: id}}}
+ ).lean()
+
+ return decryptTitles(noteList)
+}
+
+noteListSchema.statics.updateList = async (_id, noteId, title) => {
+ const noteList = await NoteList.findOneAndUpdate(
+ { _id, "notes.noteId": noteId },
+ { $set: { "notes.$.title": encrypt(title ? title : 'No title') } },
+ { new: true }
+ ).lean()
+
+ return decryptTitles(noteList)
+}
+
const NoteList = mongoose.models.NoteList || mongoose.model('NoteList', noteListSchema)
export default NoteList