aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-08-13 21:36:45 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-08-13 21:36:45 +0200
commitf6557f602f5124d5c90019cd90cf5257dbc00ef5 (patch)
tree2375986f75128a4412056b53e5eaabe94246155d /pages/api
parent74abb7052d3af3c305cfe3c525aea311ccc446e1 (diff)
downloadmy_apps-f6557f602f5124d5c90019cd90cf5257dbc00ef5.tar.gz
my_apps-f6557f602f5124d5c90019cd90cf5257dbc00ef5.tar.bz2
my_apps-f6557f602f5124d5c90019cd90cf5257dbc00ef5.zip
crypt notes title
Diffstat (limited to 'pages/api')
-rw-r--r--pages/api/notes.js31
1 files changed, 8 insertions, 23 deletions
diff --git a/pages/api/notes.js b/pages/api/notes.js
index dd31c6f..79ab281 100644
--- a/pages/api/notes.js
+++ b/pages/api/notes.js
@@ -15,7 +15,7 @@ export default withSession(async (req, res) => {
throw new Error('Something went wrong')
}
- const {notes} = await NoteList.findById(user.noteList)
+ const {notes} = await NoteList.getList(user.noteList)
res.status(200).json(notes)
} catch (error) {
@@ -32,12 +32,9 @@ export default withSession(async (req, res) => {
}
const note = await Note.create({content})
+ const {notes} = await NoteList.addNote(user.noteList, note._id, title)
- const noteList = await NoteList.findById(user.noteList)
- noteList.notes.push({title: title ? title : 'No title', noteId: note})
- await noteList.save()
-
- res.status(200).json(noteList.notes)
+ res.status(200).json(notes)
} catch (error) {
res.status(400).json([])
}
@@ -51,20 +48,13 @@ export default withSession(async (req, res) => {
throw new Error('Something went wrong')
}
- const noteList = await NoteList.findById(user.noteList)
- const noteId = noteList.notes
- .find(n => n._id.toString() === _id.toString()).noteId
-
- if (!noteList || !noteId) {
- throw new Error('Something went wrong')
- }
+ const noteId = await NoteList.getNoteId(user.noteList, _id)
+ if ( !noteId) throw new Error('Something went wrong')
await Note.findByIdAndDelete(noteId)
+ const {notes} = await NoteList.removeNote(user.noteList, _id)
- noteList.notes.pull(_id)
- await noteList.save()
-
- res.status(200).json(noteList.notes)
+ res.status(200).json(notes)
} catch (error) {
res.status(400).json([])
}
@@ -79,12 +69,7 @@ export default withSession(async (req, res) => {
}
await Note.updateNote(noteId, content)
-
- const notes = await NoteList.findOneAndUpdate(
- { _id: user.noteList, "notes.noteId": noteId },
- { $set: { "notes.$.title": title ? title : 'No title' } },
- { new: true }
- )
+ const {notes} = await NoteList.updateList(user.noteList, noteId, title)
res.status(200).json(notes)
} catch (error) {