forked from DebaucheryLibrarian/traxxx
Added tags and entities to REST API..
This commit is contained in:
104
src/entities.js
104
src/entities.js
@@ -4,7 +4,6 @@ const config = require('config');
|
||||
|
||||
const argv = require('./argv');
|
||||
const knex = require('./knex');
|
||||
const whereOr = require('./utils/where-or');
|
||||
|
||||
function curateEntity(entity, includeParameters = false) {
|
||||
if (!entity) {
|
||||
@@ -29,6 +28,15 @@ function curateEntity(entity, includeParameters = false) {
|
||||
}, includeParameters));
|
||||
}
|
||||
|
||||
if (entity.tags) {
|
||||
curatedEntity.tags = entity.tags.map(tag => ({
|
||||
id: tag.id,
|
||||
name: tag.name,
|
||||
slug: tag.slug,
|
||||
priority: tag.priority,
|
||||
}));
|
||||
}
|
||||
|
||||
return curatedEntity;
|
||||
}
|
||||
|
||||
@@ -102,33 +110,89 @@ async function fetchIncludedEntities() {
|
||||
return curatedNetworks;
|
||||
}
|
||||
|
||||
async function fetchChannels(queryObject) {
|
||||
const sites = await knex('sites')
|
||||
.where(builder => whereOr(queryObject, 'sites', builder))
|
||||
.select(
|
||||
'sites.*',
|
||||
'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', 'networks.description as network_description', 'networks.parameters as network_parameters',
|
||||
)
|
||||
.leftJoin('networks', 'sites.network_id', 'networks.id')
|
||||
.limit(100);
|
||||
async function fetchEntity(entityId, type) {
|
||||
const entity = await knex('entities')
|
||||
.select(knex.raw(`
|
||||
entities.*,
|
||||
COALESCE(json_agg(children) FILTER (WHERE children.id IS NOT NULL), '[]') as children,
|
||||
COALESCE(json_agg(tags) FILTER (WHERE tags.id IS NOT NULL), '[]') as tags,
|
||||
row_to_json(parents) as parent
|
||||
`))
|
||||
.modify((queryBuilder) => {
|
||||
if (Number(entityId)) {
|
||||
queryBuilder.where('entities.id', entityId);
|
||||
return;
|
||||
}
|
||||
|
||||
return curateEntities(sites);
|
||||
if (type) {
|
||||
queryBuilder
|
||||
.where('entities.slug', entityId)
|
||||
.where('entities.type', type);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error('Invalid ID or unspecified entity type');
|
||||
})
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.leftJoin('entities as children', 'children.parent_id', 'entities.id')
|
||||
.leftJoin('entities_tags', 'entities_tags.entity_id', 'entities.id')
|
||||
.leftJoin('tags', 'tags.id', 'entities_tags.tag_id')
|
||||
.groupBy('entities.id', 'parents.id')
|
||||
.first();
|
||||
|
||||
return curateEntity(entity);
|
||||
}
|
||||
|
||||
async function fetchChannelsFromReleases() {
|
||||
const sites = await knex('releases')
|
||||
.select('site_id', '')
|
||||
.leftJoin('sites', 'sites.id', 'releases.site_id')
|
||||
.groupBy('sites.id')
|
||||
.limit(100);
|
||||
async function fetchEntities(type, limit) {
|
||||
const entities = await knex('entities')
|
||||
.select(knex.raw(`
|
||||
entities.*,
|
||||
COALESCE(json_agg(tags) FILTER (WHERE tags.id IS NOT NULL), '[]') as tags,
|
||||
row_to_json(parents) as parent
|
||||
`))
|
||||
.modify((queryBuilder) => {
|
||||
if (type) {
|
||||
queryBuilder.where('entities.type', type);
|
||||
}
|
||||
})
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.leftJoin('entities_tags', 'entities_tags.entity_id', 'entities.id')
|
||||
.leftJoin('tags', 'tags.id', 'entities_tags.tag_id')
|
||||
.groupBy('entities.id', 'parents.id')
|
||||
.limit(limit || 100);
|
||||
|
||||
return curateEntities(sites);
|
||||
return curateEntities(entities);
|
||||
}
|
||||
|
||||
async function searchEntities(query, type, limit) {
|
||||
const entities = await knex
|
||||
.select(knex.raw(`
|
||||
entities.*,
|
||||
COALESCE(json_agg(tags) FILTER (WHERE tags.id IS NOT NULL), '[]') as tags,
|
||||
row_to_json(parents) as parent
|
||||
`))
|
||||
.from(knex.raw('search_entities(?) as entities', [query]))
|
||||
.modify((queryBuilder) => {
|
||||
if (type) {
|
||||
queryBuilder.where('entities.type', type);
|
||||
}
|
||||
})
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.leftJoin('entities_tags', 'entities_tags.entity_id', 'entities.id')
|
||||
.leftJoin('tags', 'tags.id', 'entities_tags.tag_id')
|
||||
.groupBy('entities.id', 'parents.id')
|
||||
.limit(limit || 100);
|
||||
|
||||
return curateEntities(entities);
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
curateEntity,
|
||||
curateEntities,
|
||||
fetchIncludedEntities,
|
||||
fetchChannels,
|
||||
fetchChannelsFromReleases,
|
||||
fetchEntity,
|
||||
fetchEntities,
|
||||
searchEntities,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user