order.js
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var console = process.console;
var app = require('../../server/server');
module.exports = function(Order) {
var ds = app.dataSources.mongodb;
var success = {"resultCode":20000,"resultDescription":"Success","data":{}};
var fail = {"resultCode":50000,"resultDescription":"Fail","data":{}};
Order.disableRemoteMethod("create", true);// post
Order.AddOrder = function(ctx,cb){
console.log(ctx);
findCustomer(ctx,cb,findItems)
};
function findCustomer(ctx,cb,next){
console.tag('Input').log(ctx);
var customer = ds.models.customer;
customer.findOne({"where":{"id":ctx.customer_id}},function(err,result){
if(err){
console.tag('Err').log(err);
} else{
if(result.length){
console.tag('Process/response').log(result);
success.data = result;
next ? next(ctx,cb) : cb(null,success);
}else{
fail.data = ctx;
fail.resultDescription = 'Customer ID not found';
cb(null,fail);
console.tag('ERR').log(fail)
}
}
});
}
function findItems(ctx,cb,next){
console.tag('Input').log(ctx);
var items = ds.models.item;
items.findOne({"where":{"id":ctx.item_id}},function(err,result){
if(err){
console.tag('Err').log(err);
} else{
if(result.length){
console.tag('Process/response').log(result);
success.data = result;
next ? next(ctx,cb) : cb(null,success);
}else{
fail.data = ctx;
fail.resultDescription = 'iTems ID not found';
cb(null,fail);
console.tag('ERR').log(fail)
}
}
})
}
Order.remoteMethod('AddOrder',{
http:{path:'/customer/:customer_id/item/:item_id',verb:'get'},
accepts: { arg: 'ctx', type: 'object', http: function(ctx) {
// รับข้อมุลมาใส่ไว้ใน ctx
// 1. รับข้อมูล object ที่ส่งมาโดยใช้ express
var req = ctx.req;
/* 2. Get ข้อมูลแบบ params
* params คือค่าที่ส่งมากับ url ex http://localhost/Item/params1/params2
* รับค่ามาตั้งแต่ http:{path:'/: ตัวแปรที่รับค่าเก็บไว้ /: ตัวแปรที่รับค่าเก็บไว้
*/
var params = {
"customer_id":req.params.customer_id,
"item_id":req.params.item_id
};
return params;
} } ,
returns: {arg: 'data', type: 'object', root: true}
})
};