Last active
March 17, 2016 19:06
-
-
Save EricAtMSFT/4a11bb493709659cd067 to your computer and use it in GitHub Desktop.
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
// Store Procedure for getting the most recent photos for each category | |
// @param numberOfPhotos - The number of photos to return per category | |
// @param currentDocumentVersion - The current document version number that the service is using | |
function getRecentPhotosForCategories(numberOfPhotos, currentDocumentVersion) { | |
let catCount = 0; | |
let catIndex = 0; | |
let existingPhotos = []; | |
getAllCategories(function (allCategories) { | |
for (var i = 0; i < allCategories.length; i++) { | |
// Retrieve the most recent photos for this category | |
// and append them to the response body | |
getRecentPhotosForCategory(allCategories[i].id); | |
} | |
}); | |
function getAllCategories(callback) { | |
// Perform Query using JavaScript Language Integrated Query. | |
var result =__.filter(function(doc) { | |
return doc.DocumentType == "CATEGORY" && doc.DocumentVersion == currentDocumentVersion; | |
}, function(err, documents) { | |
if (err) { | |
throw new Error("Unable to query for all categories, aborting."); | |
} | |
if (documents.length < 1) { | |
return; | |
} | |
catCount = documents.length; | |
callback(documents); | |
}); | |
if (!result.isAccepted) { | |
throw new Error("Sproc is too close to violating resource limit, aborting."); | |
} | |
} | |
function getRecentPhotosForCategory(categoryId) { | |
// Perform Query using chained JavaScript Language Integrated Query. | |
var result = __.chain() | |
.filter(function(doc) { | |
return doc.DocumentType == "PHOTO" && doc.CategoryId == categoryId && doc.DocumentVersion == currentDocumentVersion && doc.Status == 1; | |
}) | |
.sortByDescending(function (photoDoc) { return photoDoc.CreatedDateTime.Epoch }) | |
.value({ pageSize: numberOfPhotos }, function (err, documents) { | |
if (err) { | |
throw new Error("Unable to query for photos in category " + categoryId + ", aborting."); | |
} | |
// Append the documents to our total collection | |
existingPhotos = existingPhotos.concat(documents); | |
++catIndex; | |
// Once we have iterated over every category, we can return | |
// them all to the response. | |
if (catIndex >= catCount) { | |
__.response.setBody(existingPhotos); | |
} | |
}); | |
if (!result.isAccepted) { | |
throw new Error("Sproc is too close to violating resource limit, aborting."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment