'use strict'; const knex = require('./knex'); async function curateRelease(release) { const [actors, tags, media] = await Promise.all([ knex('actors_associated') .select('actors.id', 'actors.name', 'actors.gender', 'actors.slug') .where({ release_id: release.id }) .leftJoin('actors', 'actors.id', 'actors_associated.actor_id'), knex('tags_associated') .select('tags.name', 'tags.slug') .where({ release_id: release.id }) .leftJoin('tags', 'tags.id', 'tags_associated.tag_id'), knex('media') .where({ target_id: release.id }) .orderBy('role'), ]); return { id: release.id, title: release.title, date: release.date, dateAdded: release.created_at, description: release.description, url: release.url, shootId: release.shoot_id, entryId: release.entry_id, actors, director: release.director, tags, duration: release.duration, photos: media.filter(item => item.role === 'photo'), poster: media.filter(item => item.role === 'poster')[0], trailer: media.filter(item => item.role === 'trailer')[0], rating: { likes: release.likes, dislikes: release.dislikes, stars: release.stars, }, site: { id: release.site_id, name: release.site_name, slug: release.site_slug, url: release.site_url, }, studio: release.studio_id ? { id: release.studio_id, name: release.studio_name, slug: release.studio_slug, url: release.studio_url, } : null, network: { id: release.network_id, name: release.network_name, slug: release.network_slug, url: release.network_url, }, }; } function curateReleases(releases) { return Promise.all(releases.map(async release => curateRelease(release))); } async function fetchReleases(releaseId) { const releases = await knex('releases') .where(releaseId ? { 'releases.id': releaseId } : {}) .select( 'releases.*', 'sites.name as site_name', 'sites.slug as site_slug', 'sites.url as site_url', 'sites.network_id', 'studios.name as studio_name', 'sites.slug as site_slug', 'studios.url as studio_url', 'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', ) .leftJoin('sites', 'releases.site_id', 'sites.id') .leftJoin('studios', 'releases.studio_id', 'studios.id') .leftJoin('networks', 'sites.network_id', 'networks.id') .orderBy([{ column: 'date', order: 'desc' }, { column: 'created_at', order: 'desc' }]) .limit(100); return curateReleases(releases); } async function fetchSiteReleases(siteId, siteSlug) { const releases = await knex('releases') .where({ 'sites.id': siteId }) .orWhere({ 'sites.slug': siteSlug }) .select( 'releases.*', 'sites.name as site_name', 'sites.slug as site_slug', 'sites.url as site_url', 'sites.network_id', 'studios.name as studio_name', 'sites.slug as site_slug', 'studios.url as studio_url', 'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', ) .leftJoin('sites', 'releases.site_id', 'sites.id') .leftJoin('studios', 'releases.studio_id', 'studios.id') .leftJoin('networks', 'sites.network_id', 'networks.id') .orderBy([{ column: 'date', order: 'desc' }, { column: 'created_at', order: 'desc' }]) .limit(100); return curateReleases(releases); } async function fetchNetworkReleases(networkId, networkSlug) { const releases = await knex('releases') .where({ 'networks.id': networkId }) .orWhere({ 'networks.slug': networkSlug }) .select( 'releases.*', 'sites.name as site_name', 'sites.slug as site_slug', 'sites.url as site_url', 'sites.network_id', 'studios.name as studio_name', 'sites.slug as site_slug', 'studios.url as studio_url', 'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', ) .leftJoin('sites', 'releases.site_id', 'sites.id') .leftJoin('studios', 'releases.studio_id', 'studios.id') .leftJoin('networks', 'sites.network_id', 'networks.id') .orderBy([{ column: 'date', order: 'desc' }, { column: 'created_at', order: 'desc' }]) .limit(100); return curateReleases(releases); } async function fetchActorReleases(actorId, actorSlug) { const releases = await knex('actors_associated') .where({ 'actors.id': actorId }) .orWhere({ 'actors.slug': actorSlug }) .select( 'releases.*', 'actors.name as actor_name', 'sites.name as site_name', 'sites.slug as site_slug', 'sites.url as site_url', 'sites.network_id', 'studios.name as studio_name', 'sites.slug as site_slug', 'studios.url as studio_url', 'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', ) .leftJoin('releases', 'actors_associated.release_id', 'releases.id') .leftJoin('actors', 'actors_associated.actor_id', 'actors.id') .leftJoin('sites', 'releases.site_id', 'sites.id') .leftJoin('studios', 'releases.studio_id', 'studios.id') .leftJoin('networks', 'sites.network_id', 'networks.id') .orderBy([{ column: 'releases.date', order: 'desc' }, { column: 'releases.created_at', order: 'desc' }]) .limit(100); return curateReleases(releases); } async function fetchTagReleases(tagId, tagSlug) { const releases = await knex('tags_associated') .where({ 'tags.id': tagId }) .orWhere({ 'tags.slug': tagSlug }) .select( 'releases.*', 'tags.name as tag_name', 'sites.name as site_name', 'sites.slug as site_slug', 'sites.url as site_url', 'sites.network_id', 'studios.name as studio_name', 'sites.slug as site_slug', 'studios.url as studio_url', 'networks.name as network_name', 'networks.slug as network_slug', 'networks.url as network_url', ) .leftJoin('releases', 'tags_associated.release_id', 'releases.id') .leftJoin('tags', 'tags_associated.tag_id', 'tags.id') .leftJoin('sites', 'releases.site_id', 'sites.id') .leftJoin('studios', 'releases.studio_id', 'studios.id') .leftJoin('networks', 'sites.network_id', 'networks.id') .orderBy([{ column: 'releases.date', order: 'desc' }, { column: 'releases.created_at', order: 'desc' }]) .limit(100); return curateReleases(releases); } module.exports = { fetchReleases, fetchActorReleases, fetchSiteReleases, fetchNetworkReleases, fetchTagReleases, };