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.findById(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.findById(req.params.id).then(function(task){ | |
| res.render('categories/edit',{task:task}); | |
| }); | |
| }, | |
| 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 Tareas | |
| body | |
| form(action=`/tasks/${task.id}?_method=PUT` method="POST") | |
| textarea(name="description" placeholder="Qué pendiente vas a guardar")=task.description | |
| 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 Tareas | |
| body | |
| form(action="/tasks" method="POST") | |
| textarea(name="description" placeholder="Qué pendiente vas a guardar") | |
| input(type="submit" value="Guardar") |
| doctype html | |
| html | |
| head | |
| title Categorías | |
| body | |
| h1= task.title | |
| form(action=`/categories/${category.id}?_method=DELETE` method="POST") | |
| input(type="submit" value="Eliminar") |
Tanks