Skip to content

Instantly share code, notes, and snippets.

@Andresdst
Forked from urielhdz/categories.js
Last active August 14, 2023 19:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andresdst/75f90c18f159618abd1f6110055555ed to your computer and use it in GitHub Desktop.
Save Andresdst/75f90c18f159618abd1f6110055555ed to your computer and use it in GitHub Desktop.
CRUD de categories

sequelize model:generate --name Category --attributes title:string,color:string

sequelize db:migrate

const Category = require('../models').Category;
module.exports = {
create: function(req,res){
Category.create({
title: req.body.title,
color: req.body.color
}).then(result =>{
res.json(result);
}).catch(err=>{
console.log(err);
res.json(err);
});
},
new: function(req,res){
res.render('categories/new');
},
index: function(req,res){
Category.findAll().then(function(categories){
res.render('categories/index',{categories});
});
},
show: function(req,res){
Category.findByPk(req.params.id).then(function(category){
res.render('categories/show',{category});
});
},
update: function(req,res){
Category.update({
title: req.body.title,
color: req.body.color
},{
where: {id: req.params.id}
}).then(function(response){
res.redirect('/categories/'+req.params.id);
});
},
edit: function(req,res){
Category.findByPk(req.params.id).then(function(category){
res.render('categories/edit',{category:category});
});
},
destroy: function(req,res){
Category.destroy({
where: {id: req.params.id}
}).then(function(contadorElementosEliminados){
res.redirect('/categories');
})
}
};
const express = require('express');
let CategoriesController = require('../controllers/categories');
let router = express.Router();
router.route('/categories').get(CategoriesController.index).post(CategoriesController.create);
router.get('/categories/new',CategoriesController.new);
router.get('/categories/:id/edit',CategoriesController.edit);
router.route('/categories/:id').get(CategoriesController.show).put(CategoriesController.update).delete(CategoriesController.destroy);
module.exports = router;
doctype html
html
head
title Editar Categoria
body
form(action=`/categories/${category.id}?_method=PUT` method="POST")
textarea(name="title" placeholder="Qué categoria vas a guardar")=category.title
input(type="submit" value="Guardar")
doctype html
html
head
title Categorías
body
ul
each category in categories
li=category.title
doctype html
html
head
title Nueva Categorias
body
form(action="/categories" method="POST")
textarea(name="title" placeholder="Qué Categoria vas a guardar")
br
textarea(name="color" placeholder="color")
input(type="submit" value="Guardar")
doctype html
html
head
title Categorías
body
h1= category.title
form(action=`/categories/${category.id}?_method=DELETE` method="POST")
input(type="submit" value="Eliminar")
@roycelino
Copy link

Excelente.....

@tonacs
Copy link

tonacs commented May 29, 2020

Muchas gracias.
Sólo cambiaría lo siguiente en el archivo new.pug

doctype html
html
  head
    title Nuevas Categorias
  body
    form(action="/categories" method="POST")
        input(type='text' name="title" placeholder='Título de la categoría')
        input(type='color' name="color" placeholder="color")
        input(type="submit" value="Guardar")

@guidomiranda
Copy link

guidomiranda commented Jul 10, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment