Added release type distinction to REST API.

This commit is contained in:
2020-05-20 01:38:58 +02:00
parent 057362d011
commit b6691e1991
5 changed files with 137 additions and 73 deletions

View File

@@ -31,7 +31,12 @@ function curateRelease(release, withMedia = false) {
actors: (release.actors || []).map(actor => ({
id: actor.id,
name: actor.name,
slug: actor.slug,
gender: actor.gender,
networkId: actor.network_id,
aliasFor: actor.alias_for,
dateOfBirth: actor.date_of_birth,
birthCountry: actor.birth_country_alpha2,
})),
...(withMedia && {
poster: release.poster ? {
@@ -55,7 +60,7 @@ function curateRelease(release, withMedia = false) {
};
}
function withRelations(queryBuilder, withMedia = false) {
function withRelations(queryBuilder, withMedia = false, type = 'scene') {
queryBuilder
.select(knex.raw(`
releases.id, releases.entry_id, releases.shoot_id, releases.title, releases.url, releases.date, releases.description, releases.duration, releases.created_at,
@@ -64,6 +69,7 @@ function withRelations(queryBuilder, withMedia = false) {
row_to_json(site_networks) as site_network,
json_agg(DISTINCT actors) as actors
`))
.where('type', type)
.leftJoin('sites', 'sites.id', 'releases.site_id')
.leftJoin('networks', 'networks.id', 'releases.network_id')
.leftJoin('networks as site_networks', 'site_networks.id', 'sites.network_id')
@@ -88,28 +94,27 @@ function withRelations(queryBuilder, withMedia = false) {
}
}
async function fetchRelease(releaseId) {
async function fetchRelease(releaseId, type = 'scene') {
const release = await knex('releases')
.where('releases.id', releaseId)
.modify(withRelations, true)
.modify(withRelations, true, type)
.first();
return curateRelease(release, true);
}
async function fetchReleases(limit = 100) {
async function fetchReleases(limit = 100, type = 'scene') {
const releases = await knex('releases')
.modify(withRelations)
.modify(withRelations, false, type)
.limit(Math.min(limit, 1000));
return releases.map(release => curateRelease(release));
}
async function searchReleases(query, limit = 100) {
// const releases = await knex.raw('SELECT * FROM search_releases(?) LIMIT ?;', [query, limit]);
async function searchReleases(query, limit = 100, type = 'scene') {
const releases = await knex
.from(knex.raw('search_releases(?) as releases', [query]))
.modify(withRelations)
.modify(withRelations, false, type)
.limit(Math.min(limit, 1000));
return releases.map(release => curateRelease(release));