blob: f62f8496c582ba084882f5dd3cc44e6f54bd5afa (
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
|
import dbConnect from 'configs/dbConnect'
import withSession from 'hocs/withSession'
import NoteList from 'models/NoteList'
import Note from 'models/Note'
export default withSession(async (req, res) => {
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 user = req.session.get('user')
const {title, content} = req.body
if (!user || !user?.isVerified || !content) {
throw new Error('Something went wrong')
}
const note = await Note.create({content})
const {notes} = await NoteList.addNote(user.noteList, note._id, title)
res.status(200).json(notes)
} catch (error) {
res.status(400).json([])
}
break
default:
res.status(400).send()
break
}
})
|