官术网_书友最值得收藏!

  • Node.js 6.x Blueprints
  • Fernando Monteiro
  • 416字
  • 2021-07-14 10:35:07

Creating the application controllers

The next step is to create the controls for the models User and Band:

  1. Within the controllers folder, create a new file called User.js and add the following code:
          var models = require('../models/index'); 
          var User = require('../models/user'); 
     
          // Create Users 
          exports.create = function(req, res) { 
              // create a new instance of the Users model with request body 
              models.User.create({ 
                name: req.body.name, 
                  email: req.body.email 
              }).then(function(user) { 
                  res.json(user); 
              }); 
           }; 
       
           // List Users 
           exports.list = function(req, res) { 
               // List all users 
               models.User.findAll({}).then(function(users) { 
                   res.json(users); 
              }); 
          }; 
    

    Tip

    Note that the first line of the file imports the index model; this file is the basis for creation of all the controls, it is the sequelize that is used to map the other models.

  2. Do the same for the Band controller within the controllers folder; create a file called Band.js and add the following code:
          var models = require('../models/index'); 
          var Band = require('../models/band'); 
     
          // Create Band 
          exports.create = function(req, res) { 
              // create a new instance of the Bands model with request body 
              models.Band.create(req.body).then(function(band) { 
                  //res.json(band); 
                  res.redirect('/bands'); 
              }); 
          }; 
     
          // List Bands 
          exports.list = function(req, res) { 
              // List all bands and sort by Date 
              models.Band.findAll({ 
                // Order: lastest created 
                  order: 'createdAt DESC' 
              }).then(function(bands) { 
                   //res.json(bands); 
                  // Render result 
                   res.render('list', { 
                      title: 'List bands', 
                      bands: bands 
                   }); 
               });  
          }; 
     
          // Get by band id 
          exports.byId = function(req, res) { 
              models.Band.find({ 
                 where: { 
                   id: req.params.id 
                } 
              }).then(function(band) { 
                  res.json(band); 
              }); 
           } 
           // Update by id 
           exports.update = function (req, res) { 
               models.Band.find({ 
               where: { 
                  id: req.params.id 
               } 
            }).then(function(band) { 
                 if(band){ 
                   band.updateAttributes({ 
                      name: req.body.name, 
                      description: req.body.description, 
                      album: req.body.album, 
                      year: req.body.year, 
                      UserId: req.body.user_id 
                   }).then(function(band) { 
                      res.send(band); 
                  }); 
                } 
              }); 
          } 
     
          // Delete by id 
          exports.delete = function (req, res) { 
              models.Band.destroy({ 
                where: { 
                   id: req.params.id 
                } 
              }).then(function(band) { 
                  res.json(band); 
              }); 
          } 
    
  3. Now let's refactor the index.js controller and add the following code:
          // List Sample Bands 
          exports.show = function(req, res) { 
             // List all comments and sort by Date 
             var topBands = [ 
                  { 
                      name: 'Motorhead', 
                      description: 'Rock and Roll Band', 
                      album: 'http://s2.vagalume.com/motorhead/discografia
                      /orgasmatron-W320.jpg', year:'1986', 
                  }, 
                  { 
                      name: 'Judas Priest', 
                      description: 'Heavy Metal band', 
                      album: 'http://s2.vagalume.com/judas-priest/discografia
                       /screaming-for-vengeance-W320.jpg', year:'1982', 
                  }, 
                  { 
                      name: 'Ozzy Osbourne', 
                      description: 'Heavy Metal Band', 
                      album: 'http://s2.vagalume.com/ozzy-osbourne/discografia
                      /diary-of-a-madman-W320.jpg', year:'1981', 
                  } 
             ]; 
               res.render('index', { 
                   title: 'The best albums of the eighties', 
                   callToAction: 'Please be welcome, click the button below 
                   and register your favorite album.', bands: topBands 
               }); 
          }; 
    

Note that, using the previous code, we just created a simple list to show some albums on the home screen.

主站蜘蛛池模板: 垦利县| 九龙县| 通榆县| 炎陵县| 迁西县| 同心县| 新源县| 保德县| 交口县| 元氏县| 灵武市| 岑巩县| 昌乐县| 安平县| 体育| 慈溪市| 庆城县| 陆河县| 齐齐哈尔市| 崇礼县| 忻城县| 兰州市| 仪征市| 襄樊市| 墨脱县| 禄丰县| 安新县| 郑州市| 格尔木市| 绥芬河市| 揭西县| 哈尔滨市| 新乡市| 萨迦县| 喀喇沁旗| 高邑县| 新竹市| 吉林市| 巴林右旗| 乡宁县| 本溪|