aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/notes/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'pages/api/notes/index.js')
-rw-r--r--pages/api/notes/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/pages/api/notes/index.js b/pages/api/notes/index.js
new file mode 100644
index 0000000..f62f849
--- /dev/null
+++ b/pages/api/notes/index.js
@@ -0,0 +1,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
+ }
+})
+
+