0
我想用一个银行账户使用sequelize和nodejs建立一个存款和提款的简单API,但我有点困惑,我如何使用我的方法,我把它放在classmethods。任何人都可以请展示我如何使用这个到我的控制器。下面是我的模型Sequelize ClassMethods
'use strict';
module.exports = function(sequelize, DataTypes) {
var Account = sequelize.define('Account', {
name: DataTypes.STRING,
balance: DataTypes.DOUBLE,
pin: DataTypes.INTEGER,
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
},
addMoney: function(amount){
amount = Math.abs(amount);
return this.increment('balance', {by : amount}).save();
},
withdrawMoney: function(amount){
amount = Math.abs(amount);
return this.decrement('balance', {by : amount}).save();
}
}
});
return Account;
}
下面是我的控制器,但我不知道如何使用我的类方法中的控制器
var models = require('../models/index');
module.exports = {
newAccount(req, res, next){
models.Account.create({
balance: req.body.balance,
note: req.body.note,
pin: req.body.pin,
}).then(function(account){
res.json(account);
}).catch(function(error){
res.json(error)
})
},
listAccount(req, res, next){
models.Account.
findAll({
})
.then(function(accounts) {
res.status(200).send(accounts);
}).catch(function(error){
res.status(400).send(error)
});
}
}
,这是我的情况下,路线,这只是路线避免张贴太多的代码
app.get('/accounts', accountCtrl.listAccount);
app.post('/account/new', accountCtrl.newAccount);
app.put('/account/:id', accountCtrl.updateAccount);
app.delete('/account/:id', accountCtrl.removeAccount);
感谢您在avdance任何帮助,我是新来sequelize