Repository navigation
Expand file tree
/
Copy pathlambda.js
More file actions
102 lines (99 loc) · 3.08 KB
/
Copy pathlambda.js
File metadata and controls
102 lines (99 loc) · 3.08 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
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
102
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'ap-northeast-1'});
exports.handler = (event, context, callback) => {
console.log('SQS trigger fired');
if (event.http_method === 'GET'){
if (typeof event.item !== 'undefined'){
var params = {
Key:{
item: event.item
},
TableName: 'warehouse'
};
docClient.get(params, function(err, data) {
if (err) {
callback(err, null);
} else {
callback(null, data)
}
});
}
else{
var params = {
TableName: 'warehouse'
};
docClient.scan(params, function (err, data){
if (err) {
callback(err, null);
} else {
if (typeof event.isSort !== 'undefined' && event.isSort == 1){
console.log(event.isSort);
var sorted = data.Items.sort(function(first, second){
return first.item < second.item;
});
data.Items = sorted;
}
callback(null, data)
}
});
}
}
else if (event.http_method === 'DELETE'){
var params = {
Key:{
item: event.item
},
TableName: 'warehouse'
};
docClient.delete(params, function(err, data) {
if (err) {
callback(err, null);
} else {
callback(null, {'message': event.item+" successfully removed."});
}
});
}
else if (event.http_method === 'POST'){
var allitems = event.body_json
allitems.data.forEach(function(it) {
var params = {
Item:{
item: it.item,
iname: it.iname,
date: Date(Date.now())
},
TableName: 'warehouse'
};
docClient.put(params, function(err, data) {
if (err) {
callback(err,null);
} else {
callback(null, {"message":" successfully posted."});
}
});
});
}
else if (event.http_method === 'PUT'){
var params = {
Key:{
item: event.item
},
UpdateExpression: "set iname = :r",
ExpressionAttributeValues:{
":r":event.iname
},
ReturnValues:"UPDATED_NEW",
TableName: 'warehouse'
};
docClient.update(params, function(err, data) {
if (err) {
callback(err, null);
} else {
callback(null, {"message": event.item+" successfully updated."});
}
});
}
else{
callback(null, {message: 'no method as '+event.hasOwnProperty});
}
};