{"size": 1189, "ext": "js", "max_stars_repo_path": "api/plants/plants-router.js", "max_stars_repo_name": "Build-Week-Water-My-Plants-Greenthumbs/Back-end-Matt", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/plants/plants-router.js", "max_issues_repo_name": "Build-Week-Water-My-Plants-Greenthumbs/Back-end-Matt", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/plants/plants-router.js", "max_forks_repo_name": "Build-Week-Water-My-Plants-Greenthumbs/Back-end-Matt", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.3095238095, "max_line_length": 96, "alphanum_fraction": 0.5988225399}
const express = require('express')
const Plants = require('./plants-model')
const router = express.Router()

const { validateId, validatePlant, checkNameUnique } = require('./plants-middleware')
const { restricted } = require('../auth/auth-middleware')

router.get('/', async(req, res) => {
    const plants = await Plants.find()
    res.json(plants)
})

router.get('/:id', validateId, async(req, res) => {
    const plant = await Plants.findById(req.params.id)
    res.json(plant)
})

router.delete('/:id', restricted, validateId, (req, res, next) => {
    Plants.remove(req.params.id)
        .then(deleted => {
            res.json({
                deleted
            })
        })
        .catch(err => next(err))
})

router.put('/:id', restricted, validateId, validatePlant, checkNameUnique, (req, res, next) => {
    Plants.update(req.params.id, req.body)
        .then(updated => {
            res.json(updated)
        })
        .catch(err => next(err))
})

router.post('/', restricted, validatePlant, checkNameUnique, (req, res, next) => {
    Plants.add(req.body)
        .then(plant => res.status(201).json(plant))
        .catch(err => next(err))
})

module.exports = router