Skip to content

Commit

Permalink
bluzi#198 feature: Get translation (/:name/:language) - Should retu…
Browse files Browse the repository at this point in the history
…rn the translation of `:name` in `:language`
  • Loading branch information
bashz committed Oct 30, 2017
1 parent 135b5ee commit 7a58c1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ router.get('/search/:term', (req, res, next) => {
.catch(err => res.status(404).send({ err: true, debug: err }));
});

router.get('/:name/:language', (req, res, next) => {
utils.getTranslation(req.params.name, req.params.language)
.then(results => res.status(200).send(results).end())
.catch(err => res.status(404).send({ err: true, debug: err }));
});

module.exports = router;
11 changes: 11 additions & 0 deletions api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ module.exports.searchTerm = (term) => {
});
}

module.exports.getTranslation = (name, language) => {
return new Promise((resolve, reject) => {
db.query(`SELECT value FROM translations WHERE name = ${db.escape(name)} AND language = ${db.escape(language)}`, (err, value, fields) => {
if (err) return reject(err);
if (!value || !value.length) return reject(`Could not find '${language}' translation for '${name}' in the database`);

return resolve(value[0]);
});
});
}

module.exports.generatePublicObject = (name) => {
return new Promise((resolve, reject) => {
db.query(`SELECT * FROM meanings WHERE name = ${db.escape(name)}`, (err, meaning, fields) => {
Expand Down

0 comments on commit 7a58c1d

Please sign in to comment.