Skip to content

Commit

Permalink
refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marekzelinka committed Jul 24, 2024
1 parent abea622 commit cd085c5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 63 deletions.
3 changes: 3 additions & 0 deletions api.rest
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ GET http://localhost:3000/api/notes/669fad878eab5fa0dcdfd8d0
### Get single nonexisting note
GET http://localhost:3000/api/notes/669fb91dd2a9ba472da74ee7

### Try to get single note with invalid id
GET http://localhost:3000/api/notes/1

### Delete note
DELETE http://localhost:3000/api/notes/669fab9669330fe1799ecd72

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"express": "^4.19.2",
"express-async-errors": "^3.1.1",
"mongoose": "^8.5.1",
"tiny-invariant": "^1.3.3"
}
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cors from 'cors'
import express from 'express'
import 'express-async-errors'
import { notesRouter } from './routes/notes.js'
import { connectDatabase } from './utils/db.js'
import {
Expand Down
101 changes: 38 additions & 63 deletions src/routes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,52 @@ import { Note } from '../models/note.js'

export const notesRouter = express.Router()

notesRouter.post('', async (req, res, next) => {
try {
const { content, important } = req.body

const note = new Note({
content,
important,
})
const savedNote = await note.save()

res.status(201).send(savedNote)
} catch (error) {
next(error)
}
notesRouter.post('', async (req, res) => {
const { content, important } = req.body
const note = new Note({
content,
important,
})
const savedNote = await note.save()
res.status(201).send(savedNote)
})

notesRouter.get('/', async (_req, res, next) => {
try {
const notes = await Note.find()
res.send(notes)
} catch (error) {
next(error)
}
notesRouter.get('/', async (_req, res) => {
const notes = await Note.find()
res.send(notes)
})

notesRouter.get('/:id', async (req, res, next) => {
try {
const id = req.params.id
const note = await Note.findById(id)

if (!note) {
return res.status(404).end()
}
notesRouter.get('/:id', async (req, res) => {
const id = req.params.id
const note = await Note.findById(id)

res.send(note)
} catch (error) {
next(error)
if (!note) {
return res.status(404).end()
}
})

notesRouter.put('/:id', async (req, res, next) => {
try {
const id = req.params.id
const { content, important } = req.body

const updatedNote = await Note.findByIdAndUpdate(
id,
{
content,
important: Boolean(important) || false,
},
{
new: true,
runValidators: true,
context: 'query',
},
)

res.send(updatedNote)
} catch (error) {
next(error)
}
res.send(note)
})

notesRouter.delete('/:id', async (req, res, next) => {
try {
const id = req.params.id
await Note.findByIdAndDelete(id)
notesRouter.put('/:id', async (req, res) => {
const id = req.params.id
const { content, important } = req.body
const updatedNote = await Note.findByIdAndUpdate(
id,
{
content,
important: Boolean(important) || false,
},
{
new: true,
runValidators: true,
context: 'query',
},
)
res.send(updatedNote)
})

res.status(204).end()
} catch (error) {
next(error)
}
notesRouter.delete('/:id', async (req, res) => {
const id = req.params.id
await Note.findByIdAndDelete(id)
res.status(204).end()
})

0 comments on commit cd085c5

Please sign in to comment.