aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/notes.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-09-04 10:56:37 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-09-04 10:56:37 +0200
commita28b952eafc83ac6f6fc1a3d99805866bc41fde9 (patch)
tree5e8e99ef5ac2b11e8a8ab027a976caa9b1af480f /pages/api/notes.js
parent467233f771d745aeb9f4e69b93d2fb24a1a95d3e (diff)
downloadmy_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.tar.gz
my_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.tar.bz2
my_apps-a28b952eafc83ac6f6fc1a3d99805866bc41fde9.zip
routes refactor
Diffstat (limited to 'pages/api/notes.js')
-rw-r--r--pages/api/notes.js86
1 files changed, 0 insertions, 86 deletions
diff --git a/pages/api/notes.js b/pages/api/notes.js
deleted file mode 100644
index 0439a7a..0000000
--- a/pages/api/notes.js
+++ /dev/null
@@ -1,86 +0,0 @@
-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
- }
-})
-
-