diff options
Diffstat (limited to 'pages/api/notes/index.js')
-rw-r--r-- | pages/api/notes/index.js | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/pages/api/notes/index.js b/pages/api/notes/index.js index 16cb88c..86369ed 100644 --- a/pages/api/notes/index.js +++ b/pages/api/notes/index.js @@ -1,10 +1,10 @@ import dbConnect from 'configs/dbConnect' import withSession from 'hocs/withSession' -import NoteList from 'models/NoteList' -import Note from 'models/Note' +import NoteList from 'apps/Notes/models/NoteList' +import Note from 'apps/Notes/models/Note' export default withSession(async (req, res) => { - await dbConnect() + const conn = await dbConnect() switch (req.method) { case 'GET': @@ -24,6 +24,7 @@ export default withSession(async (req, res) => { break case 'POST': try { + const session = await conn.startSession() const user = req.session.get('user') const { title, content } = req.body @@ -31,10 +32,13 @@ export default withSession(async (req, res) => { throw new Error('Something went wrong') } - const note = await Note.create({ content }) - const { notes } = await NoteList.addNote(user.noteList, note._id, title) + await session.withTransaction(async () => { + const note = await Note.create({ content }) + const { notes } = await NoteList.addNote(user.noteList, note._id, title) - res.status(200).json(notes) + session.endSession() + res.status(200).json(notes) + }) } catch (error) { res.status(400).json([]) } |