Skip to content

Commit

Permalink
Move logic into parent functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Sep 12, 2016
1 parent b110bc4 commit 9504fd1
Showing 1 changed file with 26 additions and 55 deletions.
81 changes: 26 additions & 55 deletions datastore/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ function addTask (description, callback) {
]
}, function (err) {
if (err) {
callback(err);
return;
return callback(err);
}

callback(null, taskKey);
var taskId = taskKey.path.pop();
console.log('Task %d created successfully.', taskId);
return callback(null, taskKey);
});
}
// [END add_entity]
Expand Down Expand Up @@ -126,7 +127,8 @@ function markDone (taskId, callback) {
}

// The transaction completed successfully.
callback();
console.log('Task %d updated successfully.', options.taskId);
return callback(null);
});
});
});
Expand All @@ -138,7 +140,14 @@ function listTasks (callback) {
var query = datastore.createQuery('Task')
.order('created');

datastore.runQuery(query, callback);
datastore.runQuery(query, function (err, tasks) {
if (err) {
return callback(err);
}

console.log('Found %d task(s)!', tasks.length);
return callback(null, tasks);
});
}
// [END retrieve_entities]

Expand All @@ -149,37 +158,24 @@ function deleteTask (taskId, callback) {
taskId
]);

datastore.delete(taskKey, callback);
}
// [END delete_entity]

// [START format_results]
function formatTasks (tasks) {
return tasks
.map(function (task) {
var taskKey = task.key.path.pop();
var status;

if (task.data.done) {
status = 'done';
} else {
status = 'created ' + new Date(task.data.created);
}
datastore.delete(taskKey, function (err) {
if (err) {
return callback(err);
}

return taskKey + ' : ' + task.data.description + ' (' + status + ')';
})
.join('\n');
return callback();
});
}
// [END format_results]
// [END delete_entity]

var cli = require('yargs');
var makeHandler = require('../utils').makeHandler

var program = module.exports = {
addEntity: addTask,
updateEntity: markDone,
retrieveEntities: listTasks,
deleteEntity: deleteTask,
formatTasks: formatTasks,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
Expand All @@ -189,41 +185,16 @@ var program = module.exports = {
cli
.demand(1)
.command('new <description>', 'Adds a task with a description <description>.', {}, function (options) {
addTask(options.description, function (err, taskKey) {
if (err) {
throw err;
}

var taskId = taskKey.path.pop();
console.log('Task %d created successfully.', taskId);
});
addTask(options.description, makeHandler());
})
.command('done <taskId>', 'Marks the specified task as done.', {}, function (options) {
markDone(options.taskId, function (err) {
if (err) {
throw err;
}

console.log('Task %d updated successfully.', options.taskId);
});
markDone(options.taskId, makeHandler());
})
.command('list', 'Lists all tasks ordered by creation time.', {}, function (options) {
listTasks(function (err, tasks) {
if (err) {
throw err;
}

console.log(formatTasks(tasks));
});
listTasks(makeHandler());
})
.command('delete <taskId>', 'Deletes a task.', {}, function (options) {
deleteTask(options.taskId, function (err) {
if (err) {
throw err;
}

console.log('Task %d deleted successfully.', options.taskId);
});
deleteTask(options.taskId, makeHandler());
})
.example('node $0 new "Buy milk"', 'Adds a task with description "Buy milk".')
.example('node $0 done 12345', 'Marks task 12345 as Done.')
Expand Down

0 comments on commit 9504fd1

Please sign in to comment.