Last active
September 21, 2016 20:21
-
-
Save tomsarduy/3c15cfee01e1ab021df838a5b0516d9e 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
_ = require('lodash'); | |
/** | |
* Parse Query from Express req format to oDATA | |
* Parameters supported: $filter, $select, $orderby, $top, $skip | |
* Change the application status of the application | |
* @param req.query | |
* @return object | |
*/ | |
exports.parseQuery = function(query) { | |
var result = {}; | |
if (query) { | |
if (query.count) { | |
result.$top = query.count; | |
} | |
if (query.page){ | |
result.$skip = (query.page-1)*query.count; | |
} | |
if (query.sorting){ | |
result.$orderby = ""; | |
_.forEach(query.sorting, (value, key) => { | |
key = _.replace(key, '.', '/'); | |
result.$orderby += key + " " + value + ", "; | |
}); | |
result.$orderby = result.$orderby.slice(0, -2); | |
} | |
if (query.filter) { | |
result.$filter = ""; | |
_.forEach(query.filter, (value, key) => { | |
key = _.replace(key, '.', '/'); | |
result.$filter += "substringof(" + key + ",'" + decodeURIComponent(value) + "') and "; | |
}); | |
// remove last "and" | |
result.$filter = result.$filter.slice(0, -5); | |
} | |
if (query.select) { | |
result.$select = decodeURIComponent(query.select); | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment