From f6557f602f5124d5c90019cd90cf5257dbc00ef5 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Fri, 13 Aug 2021 21:36:45 +0200 Subject: crypt notes title --- pages/api/notes.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) (limited to 'pages/api') 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) { -- cgit v1.2.3