Added search to tags page.
This commit is contained in:
30
src/tags.js
30
src/tags.js
@@ -30,25 +30,37 @@ function curateTag(tag, context) {
|
||||
}
|
||||
|
||||
export async function fetchTags(options = {}) {
|
||||
const query = options.query?.trim();
|
||||
|
||||
const [tags, posters] = await Promise.all([
|
||||
knex('tags')
|
||||
.select('tags.*')
|
||||
.select('aliases.*')
|
||||
.leftJoin(knex.raw('tags AS aliases ON aliases.id = tags.alias_for OR (tags.alias_for IS NULL AND aliases.id = tags.id)'))
|
||||
.modify((builder) => {
|
||||
if (!options.includeAliases) {
|
||||
builder.whereNull('alias_for');
|
||||
}
|
||||
|
||||
if (options.query) {
|
||||
if (query) {
|
||||
builder
|
||||
.whereILike('name', `%${options.query}%`)
|
||||
.orWhereILike('slug', `%${options.query}%`);
|
||||
.whereILike('tags.name', `%${query}%`)
|
||||
.orWhereILike('tags.slug', `%${query}%`)
|
||||
.groupBy('aliases.id')
|
||||
.orderBy([
|
||||
{
|
||||
column: knex.raw('similarity(aliases.slug, :query)', { query }),
|
||||
order: 'desc',
|
||||
},
|
||||
{
|
||||
column: 'aliases.slug',
|
||||
order: 'asc',
|
||||
},
|
||||
]);
|
||||
} else if (!options.includeAliases) {
|
||||
builder.whereNull('alias_for');
|
||||
}
|
||||
}),
|
||||
knex('tags_posters')
|
||||
.select('tags_posters.tag_id', 'media.*', knex.raw('row_to_json(entities) as entity'), knex.raw('row_to_json(parents) as entity_parent'))
|
||||
.leftJoin('media', 'media.id', 'tags_posters.media_id')
|
||||
.leftJoin('entities', 'entities.id', 'media.entity_id')
|
||||
.leftJoin('entities as parents', 'entities.id', 'entities.parent_id'),
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id'),
|
||||
]);
|
||||
|
||||
const postersByTagId = Object.fromEntries(posters.map((poster) => [poster.tag_id, poster]));
|
||||
|
||||
Reference in New Issue
Block a user