aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/notes.js
blob: 73a7217145c9f8355547a9f4cedc52895e6809e9 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import dbConnect from 'lib/dbConnect'
import withSession from 'lib/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.findById(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 noteList = await NoteList.findById(user.noteList)
        noteList.notes.push({title: title ? title : 'No title', noteId: note})
        await noteList.save()

        res.status(200).json(noteList.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 noteList = await NoteList.findById(user.noteList)
        const noteId = noteList.notes
          .find(n => n._id.toString() === _id.toString()).noteId

        if (!noteList || !noteId) {
          throw new Error('Something went wrong')
        }

        await Note.findByIdAndDelete(noteId)

        noteList.notes.pull(_id)
        await noteList.save()

        res.status(200).json(noteList.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.findByIdAndUpdate(noteId, {content}, {new: true})

        const notes = await NoteList.findOneAndUpdate(
          { _id: user.noteList, "notes.noteId": noteId },
          { $set: { "notes.$.title": title ? title : 'No title' } },
          { new: true }
        )

        res.status(200).json(notes)
      } catch (error) {
        console.log(error)
        res.status(400).json([])
      }
      break
    default:
      res.status(400).send()
      break
  }
})