-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.js
101 lines (93 loc) · 2.65 KB
/
routes.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const payments = require('./controllers/payments.js');
exports.route = function(event){
return new Promise(function(resolve, reject) {
let params = parse(event);
//Routes are implemented here, and they are based on the resource in question
switch(event.resource) {
case "/paymentsApi/get/all":
if (validateGetAll(params)){
payments.getAll(params).then(function(items){
resolve(items);
});
}
break;
case "/paymentsApi/get/{id}":
if(validateID(event)){
payments.get(params).then(function(item){
console.log("calling get function from payments now");
resolve(item);
});
}
break;
case "/paymentsApi/create":
if (validateCreate(params) ){
payments.create(params).then(function(item){
resolve(item);
});
}
break;
case "/paymentsApi/update/{id}":
if (validateID(event) ){
payments.update(params).then(function(item){
resolve(item);
});
}
break;
case "/paymentsApi/delete/{id}":
if (validateID(event)){
console.log("delete params are",params);
payments.delete(params).then(function(item){
resolve(item);
});
}
break;
default:
return {};
}
});
}
//primitive validation functions for our event params, it must include an ID
//if the method is GET/PUT/DELETE and must include contractId, value, time if method is POST
//except for get/all contractId, startDate, endDate are required
function validateID(event){
if(event.pathParameters){
if(numeric(event.pathParameters.id)){
return true;
}
}
return false;
}
function validateGetAll(params){
if (numeric(params.contractId) && params.startDate && params.endDate){
return true;
}
return false;
}
function validateCreate(params){
if (numeric(params.contractId) && numeric(params.value) && params.time){
return true;
}
return false;
}
//check if value is numeric or not
function numeric(n){
return !isNaN(parseFloat(n));
}
//have a proper params ready for our controller
function parse(event){
if (event.body !== null && event.body !== undefined) {
let parsed = JSON.parse(event.body);
if(event.pathParameters && event.pathParameters.id){
parsed["id"] = parseInt(event.pathParameters.id);
}
console.log("parsed params", parsed);
return parsed;
}
else if(event.pathParameters && event.pathParameters.id){
return {id: parseInt(event.pathParameters.id)};
}
else {
return {error: "body not parsable"};
}
}