const router = require('express').Router()
const crypto = require('crypto')
const Model = require('../models/robot')

router.get('/robots', (req, res, next) => {
  Model.find({}, (err, docs) => {
    if (err) next(err)
    return res.status(200).json(docs)
  })
})

router.post('/robots', (req, res, next) => {
  if (!req.body) {
    return res.status(400).json({ error: 'The request body is empty.' })
  }
  if (!req.body.name) {
    return res.status(400).json({ error: 'The name must not be empty.' })
  }
  Model.create(req.body, (err, doc) => {
    if (err) next(err)
    return res.status(200).json(doc)
  })
})

router.get('/robots/:id', (req, res, next) => {
  Model.findById(req.params.id, (err, doc) => {
    if (err) next(err)
    return res.status(200).json(doc)
  })
})

router.patch('/robots/:id', (req, res, next) => {
  const tokenRegex = /[a-f0-9]{32}/
  if (!req.body) {
    return res.status(400).json({ error: 'The request body is empty.' })
  }
  if (req.body.token && !tokenRegex.test(req.body.token)) {
    crypto.randomBytes(16, (err, buffer) => {
      req.body.token = buffer.toString('hex')
      Model.findByIdAndUpdate(req.params.id, { $set: req.body }, (err, doc) => {
        if (err) return next(err)
        return res.status(200).json(doc)
      })
    })
  } else {
    Model.findByIdAndUpdate(req.params.id, { $set: req.body }, (err, doc) => {
      if (err) return next(err)
      return res.status(200).json(doc)
    })
  }
})

router.delete('/robots/:id', (req, res, next) => {
  Model.findByIdAndRemove(req.params.id, err => {
    if (err) return next(err)
    return res
      .status(200)
      .json({ message: 'The entity was successfully removed.' })
  })
})

module.exports = exports = router

@@ -955,24 +955,180 @@
 ty.' %7D)%0A  %7D%0A
+  if (req.body._id && req.body._id !== req.params.id) %7B%0A    return res%0A      .status(400)%0A      .json(%7B error: 'Payload ID does not match API path.' %7D)%0A  %7D%0A
   if (req.bo
@@ -1171,24 +1171,24 @@
 y.token)) %7B%0A
-
     crypto.r
@@ -1378,24 +1378,165 @@
 n next(err)%0A
+        if (!doc) %7B%0A          return res%0A            .status(400)%0A            .json(%7B error: 'There is no entity with this ID.' %7D)%0A        %7D%0A
         retu
@@ -1666,24 +1666,24 @@
 , doc) =%3E %7B%0A
-
       if (er
@@ -1698,24 +1698,155 @@
 n next(err)%0A
+      if (!doc) %7B%0A        return res%0A          .status(400)%0A          .json(%7B error: 'There is no entity with this ID.' %7D)%0A      %7D%0A
       return

Add case for id mismatch