Created
June 11, 2014 10:55
-
-
Save warnov/382a0f0298671052d276 to your computer and use it in GitHub Desktop.
Código de ejemplo para el TechDay Bogotá 2014. Muestra cómo usar NodeJS sobre Azure Mobile Services, para proveer un API que permite actuar sobre las tablas del Azure Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by War on 6/11/2014. | |
*/ | |
var azure = require('azure'); | |
exports.post = function (request, response) { | |
var tableService = azure.createTableService("ACCOUNTNAME", "PAK"); | |
//Setting working variables from post parameters | |
var cleankName = request.body.cleankName; | |
var url = request.body.url; | |
var tags = request.body.tags; | |
var task = { | |
PartitionKey: 'warnov', RowKey: cleankName, Tags: tags, Url: url | |
}; | |
tableService.insertEntity('Cleanks', task, function (error) { | |
if (!error) { | |
var tagArray = tags.split(","); | |
tagArray.forEach(function (tag) { | |
tableService.queryEntity('Tags' | |
, 'warnov' | |
, tag | |
, function (error, entity) { | |
if (error) {//this is a new tag | |
var task = { | |
PartitionKey: 'warnov', RowKey: tag, Cleanks: cleankName | |
}; | |
tableService.insertEntity('Tags', task, finish_); | |
} else update(entity); | |
} | |
) | |
}); | |
} else { | |
response.send(500, { message: "Unable to insert cleank" }); | |
} | |
}); | |
function update(entity) { | |
var currentCleanks = entity.Cleanks; | |
entity.Cleanks = currentCleanks + "," + cleankName; | |
tableService.insertOrMergeEntity( | |
'Tags' | |
, entity | |
, finish_ | |
); | |
} | |
function finish_(error) { | |
if (!error)response.send(200, {message: "Tag successfully inserted"}); | |
else response.send(500, {message: "Unable to insert tag"}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment