aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/notes/index.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-09-09 23:23:53 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-09-09 23:23:53 +0200
commita863c77ea12a4c7060b9c496e5df81bbdcca2374 (patch)
tree8d38ebc21928fa973235d8829bf1c6f7d65028df /pages/api/notes/index.js
parent569bdb8c5d7538fa0ea8a99ff2f8376f7cbfa51a (diff)
downloadmy_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.gz
my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.tar.bz2
my_apps-a863c77ea12a4c7060b9c496e5df81bbdcca2374.zip
add db transactions, move db models
Diffstat (limited to 'pages/api/notes/index.js')
-rw-r--r--pages/api/notes/index.js16
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([])
}