Forked from misshula/gist:66ac1f77dc674a971918cae9f49c727b
Created
April 19, 2017 19:59
-
-
Save earobinson/86ccf46e2f3d7ada683f638df70ce349 to your computer and use it in GitHub Desktop.
:slug or :id ORRRRRR :slugOrId
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
exports.fetchRetailer = function(req, res, next) { | |
let queryParams = null; | |
if (req.params.id) { | |
if (!validator.isMongoId(req.params.id.toString())) { | |
logger.error('`retailer.fetchBrand`: invalid retailer param, not a mongo id.', logger.context({ | |
params: req.params | |
})); | |
return next({ | |
code: 404, | |
status: 'No retailer found' | |
}); | |
} | |
queryParams = { | |
_id: req.params.id | |
}; | |
} else if (req.params.slug) { | |
queryParams = { | |
slug: decodeURIComponent(req.params.slug.trim().toLowerCase()) | |
}; | |
} else { | |
logger.error('`retailer.fetchBrand`: No retailer id or slug provided', logger.context({ | |
params: req.params | |
})); | |
return next({ | |
code: 400, | |
status: 'No retailer id or slug provided' | |
}); | |
} | |
Retailer.findOne(queryParams, function(err, retailerModel) { | |
ORRRRRRR | |
exports.fetchRetailer = function(req, res, next) { | |
let queryParams = null; | |
if (req.params.slugOrId) { | |
queryParams = { | |
$or: [ | |
{ _id: req.params.slugOrId }, | |
{ slug: decodeURIComponent(req.params.slugOrId.trim().toLowerCase() } | |
] | |
}; | |
} else { | |
logger.error('`retailer.fetchBrand`: No retailer id or slug provided', logger.context({ | |
params: req.params | |
})); | |
return next({ | |
code: 400, | |
status: 'No retailer id or slug provided' | |
}); | |
} | |
Retailer.findOne(queryParams, function(err, retailerModel) { | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment