Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
preparing user api
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig committed Oct 15, 2015
0 parents commit a65605e
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
lambda.zip

3 changes: 3 additions & 0 deletions bundle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

zip -r lambda.zip lambda.js config.json node_modules/moment node_modules/node-uuid
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"UserTable": "todo-user",
"TaskTable": "todo-task"
}
310 changes: 310 additions & 0 deletions lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
var moment = require('moment');
var uuid = require('node-uuid');
var AWS = require('aws-sdk');
var db = new AWS.DynamoDB();

function getValue(attribute, type) {
if (attribute === undefined) {
return null;
}
return attribute[type];
}

function mapTaskItem(item) {
return {
"tid": item.tid.N,
"description": item.description.S,
"created": item.created.N,
"due": getValue(item.due, 'N'),
"category": getValue(item.category, 'S'),
"completed": getValue(item.completed, 'N')
};
}

function mapUserItem(item) {
return {
"uid": item.uid.S,
"email": item.email.S,
"phone": item.phone.S
};
}

exports.getUsers = function(event, context) {
var params = {
"TableName": "todo-user",
"Limit": event.parameters.limit || 10
};
if (event.parameters.next !== null) {
params.ExclusiveStartKey = {
"uid": {
"S": event.parameters.next
}
};
}
db.scan(params, function(err, data) {
if (err) {
context.fail(err);
} else {
var res = {
"body": data.Items.map(mapUserItem)
};
if (data.LastEvaluatedKey !== undefined) {
res.headers = {"next": data.LastEvaluatedKey.uid.S};
}
context.succeed(res);
}
});
};

exports.postUser = function(event, context) {
var uid = uuid.v4();
var params = {
"Item": {
"uid": {
"S": uid
},
"email": {
"S": event.body.email
},
"phone": {
"S": event.body.phone
}
},
"TableName": "todo-user",
"ConditionExpression": "attribute_not_exists(uid)"
};
db.putItem(params, function(err) {
if (err) {
context.fail(err);
} else {
context.succeed({"body": uid});
}
});
};

exports.getUser = function(event, context) {
var params = {
"Key": {
"uid": {
"S": event.parameters.uid
}
},
"TableName": "todo-user"
};
db.getItem(params, function(err, data) {
if (err) {
context.fail(err);
} else {
if (data.Item) {
context.succeed({"body": mapUserItem(data.Item)});
} else {
context.fail(new Error('not found'));
}
}
});
};

exports.deleteUser = function(event, context) {
var params = {
"Key": {
"uid": {
"S": event.parameters.uid
}
},
"TableName": "todo-user"
};
db.deleteItem(params, function(err) {
if (err) {
context.fail(err);
} else {
context.succeed();
}
});
};

/*
if (input['task-add'] === true) {
var tid = Date.now();
var params = {
"Item": {
"uid": {
"S": input['<uid>']
},
"tid": {
"N": tid.toString()
},
"description": {
"S": input['<description>']
},
"created": {
"N": moment().format("YYYYMMDD")
}
},
"TableName": "todo-task",
"ConditionExpression": "attribute_not_exists(uid) and attribute_not_exists(tid)"
};
if (input['--dueat'] !== null) {
params.Item.due = {
"N": input['--dueat']
};
}
if (input['<category>'] !== null) {
params.Item.category = {
"S": input['<category>']
};
}
db.putItem(params, function(err) {
if (err) {
console.error('error', err);
} else {
console.log('task added with tid ' + tid);
}
});
} else if (input['task-rm'] === true) {
var params = {
"Key": {
"uid": {
"S": input['<uid>']
},
"tid": {
"N": input['<tid>']
}
},
"TableName": "todo-task"
};
db.deleteItem(params, function(err) {
if (err) {
console.error('error', err);
} else {
console.log('task removed with tid ' + input['<tid>']);
}
});
} else if (input['task-ls'] === true) {
var params = {
"KeyConditionExpression": "uid = :uid",
"ExpressionAttributeValues": {
":uid": {
"S": input['<uid>']
}
},
"TableName": "todo-task",
"Limit": input['--limit']
};
if (input['--next'] !== null) {
params.KeyConditionExpression += ' AND tid > :next';
params.ExpressionAttributeValues[':next'] = {
"N": input['--next']
};
}
if (input['--overdue'] === true) {
params.FilterExpression = "due < :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--due'] === true) {
params.FilterExpression = "due = :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--withoutdue'] === true) {
params.FilterExpression = "attribute_not_exists(due)";
} else if (input['--futuredue'] === true) {
params.FilterExpression = "due > :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--dueafter'] !== null) {
params.FilterExpression = "due > :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": input['--dueafter']};
} else if (input['--duebefore'] !== null) {
params.FilterExpression = "due < :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": input['--duebefore']};
}
if (input['<category>'] !== null) {
if (params.FilterExpression === undefined) {
params.FilterExpression = '';
} else {
params.FilterExpression += ' AND ';
}
params.FilterExpression += 'category = :category';
params.ExpressionAttributeValues[':category'] = {
"S": input['<category>']
};
}
db.query(params, function(err, data) {
if (err) {
console.error('error', err);
} else {
console.log('tasks', data.Items.map(mapTaskItem));
if (data.LastEvaluatedKey !== undefined) {
console.log('more tasks available with --next=' + data.LastEvaluatedKey.tid.N);
}
}
});
} else if (input['task-la'] === true) {
var params = {
"KeyConditionExpression": "category = :category",
"ExpressionAttributeValues": {
":category": {
"S": input['<category>']
}
},
"TableName": "todo-task",
"IndexName": "category-index",
"Limit": input['--limit']
};
if (input['--next'] !== null) {
params.KeyConditionExpression += ' AND tid > :next';
params.ExpressionAttributeValues[':next'] = {
"N": input['--next']
};
}
if (input['--overdue'] === true) {
params.FilterExpression = "due < :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--due'] === true) {
params.FilterExpression = "due = :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--withoutdue'] === true) {
params.FilterExpression = "attribute_not_exists(due)";
} else if (input['--futuredue'] === true) {
params.FilterExpression = "due > :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": moment().format("YYYYMMDD")};
} else if (input['--dueafter'] !== null) {
params.FilterExpression = "due > :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": input['--dueafter']};
} else if (input['--duebefore'] !== null) {
params.FilterExpression = "due < :yyyymmdd";
params.ExpressionAttributeValues[':yyyymmdd'] = {"N": input['--duebefore']};
}
db.query(params, function(err, data) {
if (err) {
console.error('error', err);
} else {
console.log('tasks', data.Items.map(mapTaskItem));
if (data.LastEvaluatedKey !== undefined) {
console.log('more tasks available with --next=' + data.LastEvaluatedKey.tid.N);
}
}
});
} else if (input['task-done'] === true) {
var params = {
"Key": {
"uid": {
"S": input['<uid>']
},
"tid": {
"N": input['<tid>']
}
},
"UpdateExpression": "SET completed = :yyyymmdd",
"ExpressionAttributeValues": {
":yyyymmdd": {
"N": moment().format("YYYYMMDD")
}
},
"TableName": "todo-task"
};
db.updateItem(params, function(err) {
if (err) {
console.error('error', err);
} else {
console.log('task completed with tid ' + input['<tid>']);
}
});
}
*/
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"aws-sdk": "2.1.35",
"moment": "2.10.3",
"node-uuid": "1.4.3"
},
"bundledDependencies": ["moment", "node-uuid"],
"private": true
}
Loading

0 comments on commit a65605e

Please sign in to comment.