如何在Sequelize中实现主动MVC模型

时间:2018-12-21 01:13:16

标签: node.js postgresql model-view-controller sequelize.js

我正在开发NodeJS + Sequelize应用程序。现在,我将所有逻辑都放在控制器中,但是有人告诉我这是错误的方法(胖笨丑控制器)。这是我的控制器文件:

const Deals = require('../models').deals;

module.exports = {
  createDeal: (req, res, next) => {
    const { description, category, open_time, close_time, property_id } = req.body;
    return Deals
    .create({ description, category, open_time, close_time, property_id })
    .then(deal => res.status(200).json({ status: 'Created one property', deal }))
    .catch(error => console.log(error));
  },

  getAllDeals: (req, res, next) => {
    return Deals
    .findAll({paranoid: false})
    .then(deals => res.status(200).json({ status: 'Retrieved all deals', deals }))
    .catch(error => console.log(error));
  },

  getSingleDeal: (req, res, next) => {
    const { id } = req.params;
    return Deals
    .findById(id)
    .then(deal => res.status(200).json({ status: 'Retrieved one deal', deal }))
    .catch(error => console.log(error));
  },

  updateDeal: (req, res, next) => {
    const { id } = req.params;
    const { description, category, open_time, close_time } = req.body;
    return Deals
    .findById(id)
    .then(deal => {
      if (!deal) {
        return res.status(404).send({ message: 'Deal not found' })
      }
      return deal
      .update({
        description: description,
        category: category,
        open_time: open_time,
        close_time: close_time
      })
      .then(deal => res.status(200).json({ status: 'Updated one deal', deal }))
      .catch(error => console.log(error));
    })
    .catch(error => console.log(error));
  },

  destroyDeal: (req, res, next) => {
    const { id } = req.params;
    return Deals
    .findById(id)
    .then(deal => {
      if (!deal) {
        return res.status(404).send({ message: 'Deal not found' })
      }
      return deal
      .destroy()
      .then(() =>  res.status(200).json({ status: 'Deleted one deal', deal }))
      .catch(error => console.log(error));
    })
  }
}

这是我拥有的模型文件:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Deals = sequelize.define('deals', {
    description: {
      allowNull: false,
      type: DataTypes.STRING
    },
    category: {
      allowNull: false,
      type: DataTypes.STRING
    },
    open_time: {
      allowNull: false,
      type: DataTypes.STRING
    },
    close_time: {
      allowNull: false,
      type: DataTypes.STRING
    },
    property_id: {
      allowNull: false,
      type: DataTypes.INTEGER
    }
  }, {
    timestamps: true,
    paranoid: true
  });
  Deals.associate = function(models) {
    // associations can be defined here
    Deals.belongsTo(models.properties, {
      foreignKey: 'property_id',
      onDelete: 'CASCADE'
    })
  };
  return Deals;
};

因此,有人告诉我,我的updateDeal()方法是错误的,因为它向模型或数据库发出多个请求,而这仅仅是“从用户那里获取并分析请求,然后选择下一步(例如将其发送到系统的其他元素)”。因此,我应该将控制器的所有逻辑带到我的模型函数中。我想请教一下它的工作原理或一些代码示例。

1 个答案:

答案 0 :(得分:0)

我能够更改它。这是从我的控制器来的:

updateDeal: (req, res, next) => {
    const { id } = req.params;
    return Deals
      .updateById(id, req.body)
      .then(deal => res.status(200).json({ status: 'Updated one deal', deal }))
      .catch(error => {
        return res.status(400).send({message: error.message})
      });
  }

这是来自模型的:

Deals.updateById = function(id, data){
    return Deals
      .findById(id)
      .then(deal => {
        if (!deal) {
          throw Error('Deal Not Found')
        }
        return deal
        .update(data, {fields: Object.keys(data)})
      })

  }
  return Deals;