2019-03-24 00:29:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-25 02:57:33 +00:00
|
|
|
const knex = require('./knex');
|
|
|
|
|
2019-11-11 04:18:28 +00:00
|
|
|
async function curateTag(tag) {
|
|
|
|
const aliases = await knex('tags').where({ alias_for: tag.id });
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: tag.id,
|
|
|
|
name: tag.name,
|
|
|
|
description: tag.description,
|
|
|
|
group: {
|
|
|
|
id: tag.group_id,
|
|
|
|
name: tag.group_name,
|
|
|
|
description: tag.group_description,
|
|
|
|
slug: tag.group_slug,
|
|
|
|
},
|
|
|
|
aliases: aliases.map(({ name }) => name),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function curateTags(tags) {
|
|
|
|
return Promise.all(tags.map(async tag => curateTag(tag)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storeTags(release, releaseEntry) {
|
|
|
|
return knex('tags_associated').insert(release.tags.map(tagId => ({
|
|
|
|
tag_id: tagId,
|
|
|
|
release_id: releaseEntry.id,
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchTags(tagId, tagSlug) {
|
|
|
|
const tags = await knex('tags')
|
|
|
|
.where({ 'tags.id': tagId })
|
|
|
|
.orWhere({ 'tags.slug': tagSlug })
|
|
|
|
.andWhere({ 'tags.alias_for': null })
|
|
|
|
.select(
|
|
|
|
'tags.*',
|
|
|
|
'tags_groups.id as group_id', 'tags_groups.name as group_name', 'tags_groups.slug as group_slug', 'tags_groups.description as groups_description',
|
|
|
|
)
|
|
|
|
.leftJoin('tags_groups', 'tags.group_id', 'tags_groups.id')
|
|
|
|
.limit(100);
|
|
|
|
|
|
|
|
return curateTags(tags);
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:57:33 +00:00
|
|
|
async function matchTags(rawTags) {
|
2019-10-30 03:45:42 +00:00
|
|
|
const tags = rawTags
|
|
|
|
.concat(rawTags.map(tag => tag.toLowerCase()))
|
|
|
|
.concat(rawTags.map(tag => tag.toUpperCase()));
|
|
|
|
|
2019-10-27 23:58:54 +00:00
|
|
|
const tagEntries = await knex('tags')
|
|
|
|
.pluck('aliases.id')
|
2019-10-30 03:45:42 +00:00
|
|
|
.whereIn('tags.name', tags)
|
2019-10-27 23:58:54 +00:00
|
|
|
.where(function where() {
|
|
|
|
this
|
|
|
|
.whereNull('tags.alias_for')
|
|
|
|
.orWhereNull('aliases.alias_for');
|
|
|
|
})
|
|
|
|
.join('tags as aliases', function join() {
|
|
|
|
this
|
|
|
|
.on('tags.alias_for', 'aliases.id')
|
|
|
|
.orOn('tags.id', 'aliases.id');
|
|
|
|
})
|
|
|
|
.groupBy('aliases.id');
|
2019-04-07 03:01:06 +00:00
|
|
|
|
2019-10-27 23:58:54 +00:00
|
|
|
return tagEntries;
|
2019-03-25 02:57:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 02:20:00 +00:00
|
|
|
module.exports = {
|
|
|
|
storeTags,
|
2019-11-11 04:18:28 +00:00
|
|
|
fetchTags,
|
|
|
|
matchTags,
|
2019-11-11 02:20:00 +00:00
|
|
|
};
|