blob: 8530c53de372d284a0e1ac618e6edde5f0ba8389 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import dbConnect from 'configs/dbConnect'
import withSession from 'hocs/withSession'
import NoteList from 'apps/Notes/models/NoteList'
import Note from 'apps/Notes/models/Note'
export default withSession(async (req, res) => {
const conn = await dbConnect()
switch (req.method) {
case 'GET':
try {
const user = req.session.get('user')
if (!user || !user.isVerified) {
throw new Error('Something went wrong')
}
const { notes } = await NoteList.getList(user.noteList)
res.status(200).json(notes)
} catch (error) {
res.status(400).json([])
}
break
case 'POST':
try {
const session = await conn.startSession()
const user = req.session.get('user')
const { title, content } = JSON.parse(req.body)
if (!user || !user?.isVerified || !content) {
throw new Error('Something went wrong')
}
await session.withTransaction(async () => {
const note = await Note.create({ content })
const { notes } = await NoteList.addNote(user.noteList, note._id, title)
session.endSession()
res.status(200).json(notes)
})
} catch (error) {
console.log(error)
res.status(400).json([])
}
break
default:
res.status(400).send()
break
}
})
|