diff options
author | 2021-09-04 10:56:37 +0200 | |
---|---|---|
committer | 2021-09-04 10:56:37 +0200 | |
commit | a28b952eafc83ac6f6fc1a3d99805866bc41fde9 (patch) | |
tree | 5e8e99ef5ac2b11e8a8ab027a976caa9b1af480f /pages/api/notes/index.js | |
parent | 467233f771d745aeb9f4e69b93d2fb24a1a95d3e (diff) | |
download | my_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.tar.gz my_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.tar.bz2 my_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.zip |
routes refactor
Diffstat (limited to 'pages/api/notes/index.js')
-rw-r--r-- | pages/api/notes/index.js | 48 |
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 + } +}) + + |