2019-11-11 04:18:28 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { fetchTags } = require('../tags');
|
|
|
|
|
|
|
|
async function fetchTagsApi(req, res) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const tagId = typeof req.params.tagId === 'number' ? req.params.tagId : undefined; // null will literally include NULL results
|
|
|
|
const tagSlug = typeof req.params.tagId === 'string' ? req.params.tagId : undefined;
|
2019-11-11 04:18:28 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (tagId || tagSlug) {
|
|
|
|
const tags = await fetchTags({
|
|
|
|
id: tagId,
|
|
|
|
slug: tagSlug,
|
|
|
|
}, null, req.query.limit);
|
2019-12-01 04:32:47 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (tags.length > 0) {
|
|
|
|
res.send(tags[0]);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-01 04:32:47 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
res.status(404).send();
|
|
|
|
return;
|
|
|
|
}
|
2019-12-01 04:32:47 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const query = {};
|
|
|
|
const groupsQuery = {};
|
2019-12-04 20:58:08 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (req.query.priority) query.priority = req.query.priority.split(',');
|
|
|
|
if (req.query.slug) query.slug = req.query.slug.split(',');
|
|
|
|
if (req.query.group) {
|
|
|
|
groupsQuery.slug = req.query.group.split(',');
|
|
|
|
}
|
2019-12-04 20:58:08 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const tags = await fetchTags(query, groupsQuery, req.query.limit);
|
2019-11-11 04:18:28 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
res.send(tags);
|
2019-11-11 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-05-14 02:26:05 +00:00
|
|
|
fetchTags: fetchTagsApi,
|
2019-11-11 04:18:28 +00:00
|
|
|
};
|