Fixed entity API database query.

This commit is contained in:
DebaucheryLibrarian 2020-10-18 00:01:34 +02:00
parent ca22aedaaa
commit 2536405dba
2 changed files with 7 additions and 4 deletions

View File

@ -88,7 +88,7 @@ To generate thumbnails for new logos and tag photos, install ImageMagick and run
* `--level`: Change log level to `silly`, `verbose`, `info`, `warn` or `error`.
### API
A GraphQL API is available at `/graphql`, and a REST API is available at the following endpoints:
A GraphQL API is available at `/graphql`, and a REST API is available at the following `GET` endpoints:
* `/api/scenes`: Fetch the latest releases. Supports search with `query` or `q` parameter;
* `/api/scenes/{ID}`: Fetch scene by ID.

View File

@ -17,7 +17,6 @@ function curateEntity(entity, includeParameters = false) {
description: entity.description,
slug: entity.slug,
type: entity.type,
parameters: includeParameters ? entity.parameters : null,
parent: curateEntity(entity.parent, includeParameters),
} : {};
@ -37,6 +36,10 @@ function curateEntity(entity, includeParameters = false) {
}));
}
if (includeParameters) {
curatedEntity.parameters = entity.parameters;
}
return curatedEntity;
}
@ -168,7 +171,7 @@ async function fetchEntities(type, limit) {
async function searchEntities(query, type, limit) {
const entities = knex
.select(knex.raw(`
entities.*,
entities.id, entities.name, entities.slug, entities.type, entities.url, entities.description,
COALESCE(json_agg(tags) FILTER (WHERE tags.id IS NOT NULL), '[]') as tags,
row_to_json(parents) as parent
`))
@ -181,7 +184,7 @@ async function searchEntities(query, type, limit) {
.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')
.groupBy('entities.id', 'entities.name', 'entities.slug', 'entities.type', 'entities.url', 'entities.description', 'parents.id')
.limit(limit || 100);
console.log(entities.toString());