{"size": 954, "ext": "js", "max_stars_repo_path": "controllers/categories.js", "max_stars_repo_name": "vs0uz4/como_fazer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllers/categories.js", "max_issues_repo_name": "vs0uz4/como_fazer", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/categories.js", "max_forks_repo_name": "vs0uz4/como_fazer", "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": 22.7142857143, "max_line_length": 63, "alphanum_fraction": 0.6174004193}
const api = require('../api')

const index = async(req, res) => {
    const categories = await api.list('categories')
    res.render('categories/index', { categories: categories })
} 

const create = (req, res) => {
    res.render('categories/create')
}

const store = async(req, res) => {
    await api.create('categories', {
        name: req.body.name
    })
    res.redirect('/categories')
}

const edit = async(req, res) => {
    const category = await api.get('categories', req.params.id)
    res.render('categories/edit', {
        category
    })
}

const update = async(req, res) => {
    await api.update('categories', req.params.id, {
        name: req.body.name
    })
    res.redirect('/categories')
}

const destroy = async(req, res) => {
    await api.destroy('categories', req.params.id)
    await api.destroy('posts', req.params.id)
    res.redirect('/categories')
}

module.exports = {
    index, create, store, edit, update, destroy
}
