Skip to content

Instantly share code, notes, and snippets.

@lrlineroa
Created July 10, 2019 10:46
Show Gist options
  • Save lrlineroa/2e1400e4814736466c4d29cadea8a5a8 to your computer and use it in GitHub Desktop.
Save lrlineroa/2e1400e4814736466c4d29cadea8a5a8 to your computer and use it in GitHub Desktop.
var express = require('express');
var router = express.Router();
const Category = require('../models/category')
//index and form to create
router.get('/', async function (req, res, next) {
console.log('index')
categories = await Category.find();
res.render('categories/index', { title: 'Tarjeta APP', categories: categories , query:req.query });
});
//create
router.post('/', async (req, res) => {
let category = new Category(req.body);
await category.save();
res.redirect('/categories?created=true');
});
//form to edit
router.get('/edit/:id',async (req,res)=>{
const {id} =req.params
const category=await Category.findById(id);
res.render('categories/edit',{title: 'Tarjeta APP', category})
});
//update
router.post('/edit/:id',async (req,res)=>{
const {id} =req.params
await Category.updateOne({_id:id},req.body);
res.redirect('/categories?updated=true')
});
//delete
router.get('/delete/:id', async (req, res) => {
const {id} =req.params
await Category.remove({_id:id});
res.redirect('/categories?deleted=true');
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment