こんにちは、開発のために MEANjs を試しています。
「アカウント」「収入」「支出」のCRUDで構成されたアプリを作りました。それぞれ を使用して作成されyo meanjs:crud-module <module-name>
ます。ここで、収入と支出の両方で構成される「送金」タイプでプロジェクトを拡張することはありません。つまり、送金を作成すると、送信アカウントに費用が作成され、受信アカウントに収入が作成されます。
これをできる限り簡単かつ論理的に作成したいと考えています。
サーバー側に可能な限り多くのロジックを配置することは理にかなっていると思います。そのため、フロントエンドは、収益モデルと費用モデルのデータを組み合わせてバックエンド コントローラーで作成された転送オブジェクトとして転送を識別します。
収入が実際に譲渡に属していることを識別するために、次の属性をモデルに追加します。ここで、属性を使用して譲渡であるかどうかを示します: isTransfer、および「費用」属性を使用してどの費用に属するか
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Income Schema
*/
var IncomeSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Income name',
trim: true
},
amount: {
type: String,
default: '',
required: 'Please fill Income amount',
trim: true
},
date: {
type: Date,
default: '',
required: 'Please fill Income date',
trim: true
},
monthly: {
type: Boolean,
default: '',
required: 'Please fill whether income is recurring monthly',
trim: true
},
yearly: {
type: Boolean,
default: '',
required: 'Please fill whether income is recurring yearly',
trim: true
},
account: {
type: Schema.ObjectId,
ref: 'Account',
required: 'Please select account'
},
isTransfer: {
type: Boolean,
default: false,
trim: true
},
expense:{
type: Schema.ObjectId,
ref:'Expense'
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Income', IncomeSchema);
次に、スタック全体で転送を作成するのは、実行するのと同じくらい簡単だと思います。
yo meanjs:crud-module transfers
私の次のステップは、バックエンドの転送のモデルを削除し、転送を作成しようとするたびに収入と支出が作成されるようにバックエンド コントローラーを書き直すことです。
コントローラは、バックエンド コントローラからフロントエンドまで、実際の「転送オブジェクト」も返します。
最初に、転送オブジェクトをフロントエンドにのみ存在するものとして追加し、フロントエンド サービスから収入と支出を作成するためのリクエストを送信しようとしましたが、コードがかなり詰まり、1 つの転送を作成するためにバックエンドに多くの呼び出しを行う必要がありました。 . いえ
- 収入を生み出す -> 収入を得るために必要な資源を節約する
- 応答から収入 ID を取得する
- 収入 ID で経費を作成 -> 経費のリソース呼び出しを保存
- 応答から経費 ID を取得します
- 収入を経費 ID で更新します。-> 収入のためのリソースコールを再度保存
バックエンドコントローラーで転送オブジェクトを作成する方が理にかなっていると思います。これは転送オブジェクトを管理する合理的な方法ですか?
別の方法で転送を作成する方が理にかなっていますか? それともスタックの別のレベルですか?
収入と支出への参照のみを含む転送のモデルを作成できると思いますが、それでも収入/支出オブジェクトを正しく作成する必要があります。
コマンドを実行しyo meanjs:crud-module <module-name>
て転送モジュールを生成し、転送モデル全体を削除して、サーバー コントローラーからコメント アウトしました。フロントエンドの表現はそのままにして、リストと作成、編集などを行いました。代わりに収入と支出のモデルに関連するように、サーバーコントローラーを作り直す必要があります。以下の例:
したがって、サーバー Transfers コントローラーの create メソッドは、soo のように収入と支出の忌まわしきものになります。
/**
* Create a Income and Expense
*/
exports.create = function(req, res) {
var income = new Income(req.body);
var expense = new Expense(req.body);
expense.user = income.user = req.user;
console.log('Request: ' + JSON.stringify(req.body));
//This should create an income and expense that is part of a transfer
expense.isTransfer = income.isTransfer = true;
//Generate ID's manually to create the relation between income and expense:
income.expense = expense._id = mongoose.Types.ObjectId();
expense.income = income._id = mongoose.Types.ObjectId();
//Set income and expense receiving and sending account, these attributes where simply attached to the body:
income.account = req.body.receivingAccount;
expense.account = req.body.sendingAccount;
//Save income if successfull, also save expense, if both succeeds return income and expense
income.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
expense.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp({income: income, expense: expense});
}
});
}
});
};
また、Transfer を作成するためのクライアント コントローラーは、醜いものではありませんが、作成された Transfer オブジェクトは、mongoose モデルで定義された transfer オブジェクトに対応していないことに注意してください。
// Create new Transfer when received in the server controller this yields both an income and an expense.
$scope.create = function() {
// Create new Transfer object, this defines what your request.body will contain.
var transfer = new Transfers ({
name: this.name,
amount: this.amount,
date: this.date,
monthly: this.recurring==='monthly',
yearly: this.recurring==='yearly',
receivingAccount: this.receivingAccount._id,
sendingAccount: this.sendingAccount._id
});
// Redirect after save
transfer.$save(function(response) {
$location.path('transfers/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
ありがとう