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