blob: 0439a7a5ac1ff80d0839d46117a9223fbf7278ab (
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
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
case 'DELETE':
try {
const user = req.session.get('user')
const {_id} = req.body
if (!user || !user?.isVerified || !_id) {
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)
res.status(200).json(notes)
} catch (error) {
res.status(400).json([])
}
break
case 'PUT':
try {
const user = req.session.get('user')
const {_id, title, noteId, content} = req.body
if (!user || !user?.isVerified || !_id || !content) {
throw new Error('Something went wrong')
}
await Note.updateNote(noteId, content)
const {notes} = await NoteList.updateList(user.noteList, noteId, title)
res.status(200).json(notes)
} catch (error) {
console.log(error)
res.status(400).json([])
}
break
default:
res.status(400).send()
break
}
})
|