Added release type distinction to REST API.

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

View File

@ -130,7 +130,7 @@ function toBaseActors(actorsOrNames, release) {
}); });
} }
function curateActor(actor) { function curateActor(actor, withDetails = false) {
if (!actor) { if (!actor) {
return null; return null;
} }
@ -140,9 +140,15 @@ function curateActor(actor) {
name: actor.name, name: actor.name,
slug: actor.slug, slug: actor.slug,
gender: actor.gender, gender: actor.gender,
networkId: actor.network_id,
aliasFor: actor.alias_for,
dateOfBirth: actor.date_of_birth,
birthCountry: actor.birth_country_alpha2,
...(withDetails && {
alias: actor.alias && { alias: actor.alias && {
id: actor.alias.id, id: actor.alias.id,
name: actor.alias.name, name: actor.alias.name,
slug: actor.slug,
gender: actor.alias.gender, gender: actor.alias.gender,
}, },
network: actor.network && { network: actor.network && {
@ -150,7 +156,6 @@ function curateActor(actor) {
name: actor.network.name, name: actor.network.name,
slug: actor.network.slug, slug: actor.network.slug,
}, },
dateOfBirth: actor.date_of_birth,
dateOfDeath: actor.date_of_death, dateOfDeath: actor.date_of_death,
cup: actor.cup, cup: actor.cup,
bust: actor.bust, bust: actor.bust,
@ -166,7 +171,7 @@ function curateActor(actor) {
tattoos: actor.tattoos, tattoos: actor.tattoos,
piercings: actor.piercings, piercings: actor.piercings,
description: actor.description, description: actor.description,
origin: actor.birth_country && { placeOfBirth: actor.birth_country && {
country: { country: {
alpha2: actor.birth_country.alpha2, alpha2: actor.birth_country.alpha2,
name: actor.birth_country.name, name: actor.birth_country.name,
@ -175,7 +180,7 @@ function curateActor(actor) {
state: actor.birth_state, state: actor.birth_state,
city: actor.birth_city, city: actor.birth_city,
}, },
residence: actor.residence_country && { placeOfResidence: actor.residence_country && {
country: { country: {
alpha2: actor.residence_country.alpha2, alpha2: actor.residence_country.alpha2,
name: actor.residence_country.name, name: actor.residence_country.name,
@ -192,6 +197,7 @@ function curateActor(actor) {
size: actor.avatar.size, size: actor.avatar.size,
source: actor.avatar.source, source: actor.avatar.source,
}, },
}),
}; };
return curatedActor; return curatedActor;
@ -724,11 +730,21 @@ async function fetchActor(actorId) {
.leftJoin('media', 'media.id', 'actors.avatar_media_id') .leftJoin('media', 'media.id', 'actors.avatar_media_id')
.first(); .first();
return curateActor(actor); return curateActor(actor, true);
}
async function searchActors(query) {
const actors = await knex
.select('*')
.from(knex.raw('search_actors(?) as actors', [query]))
.limit(10);
return actors.map(actor => curateActor(actor));
} }
module.exports = { module.exports = {
associateActors, associateActors,
fetchActor, fetchActor,
scrapeActors, scrapeActors,
searchActors,
}; };

View File

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

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { fetchActor } = require('../actors'); const { fetchActor, searchActors } = require('../actors');
async function fetchActorApi(req, res) { async function fetchActorApi(req, res) {
const actor = await fetchActor(req.params.actorId); const actor = await fetchActor(req.params.actorId);
@ -13,6 +13,20 @@ async function fetchActorApi(req, res) {
res.status(404).send({ actor: null }); res.status(404).send({ actor: null });
} }
async function fetchActorsApi(req, res) {
const query = req.query.query || req.query.q;
if (query) {
const actors = await searchActors(query, req.query.limit);
res.send({ actors });
return;
}
res.send({ hint: 'specify a query or ID', actors: [] });
}
module.exports = { module.exports = {
fetchActor: fetchActorApi, fetchActor: fetchActorApi,
fetchActors: fetchActorsApi,
}; };

View File

@ -2,28 +2,48 @@
const { fetchRelease, fetchReleases, searchReleases } = require('../releases'); const { fetchRelease, fetchReleases, searchReleases } = require('../releases');
async function fetchReleaseApi(req, res) { async function fetchReleaseApi(req, res, type = 'scene') {
const release = await fetchRelease(req.params.releaseId); const release = await fetchRelease(req.params.releaseId, type);
if (release) { if (release) {
res.send({ release }); res.send({ [type]: release });
return; return;
} }
res.status(404).send({ release: null }); res.status(404).send({ [type]: null });
} }
async function fetchReleasesApi(req, res) { async function fetchReleasesApi(req, res, type = 'scene') {
const query = req.query.query || req.query.q; const query = req.query.query || req.query.q;
const releases = query const releases = query
? await searchReleases(query, req.query.limit) ? await searchReleases(query, req.query.limit, type)
: await fetchReleases(req.query.limit); : await fetchReleases(req.query.limit, type);
res.send({ releases }); res.send({ [`${type}s`]: releases });
}
async function fetchSceneApi(req, res) {
return fetchReleaseApi(req, res, 'scene');
}
async function fetchScenesApi(req, res) {
return fetchReleasesApi(req, res, 'scene');
}
async function fetchMovieApi(req, res) {
return fetchReleaseApi(req, res, 'movie');
}
async function fetchMoviesApi(req, res) {
return fetchReleasesApi(req, res, 'movie');
} }
module.exports = { module.exports = {
fetchRelease: fetchReleaseApi, fetchRelease: fetchReleaseApi,
fetchReleases: fetchReleasesApi, fetchReleases: fetchReleasesApi,
fetchScene: fetchSceneApi,
fetchMovie: fetchMovieApi,
fetchScenes: fetchScenesApi,
fetchMovies: fetchMoviesApi,
}; };

View File

@ -15,11 +15,16 @@ const logger = require('../logger')(__filename);
const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins'); const { ActorPlugins, SitePlugins, ReleasePlugins } = require('./plugins/plugins');
const { const {
fetchRelease, fetchScene,
fetchReleases, fetchScenes,
fetchMovie,
fetchMovies,
} = require('./releases'); } = require('./releases');
const { fetchActor } = require('./actors'); const {
fetchActor,
fetchActors,
} = require('./actors');
function initServer() { function initServer() {
const app = express(); const app = express();
@ -63,9 +68,13 @@ function initServer() {
router.use(bodyParser.json({ strict: false })); router.use(bodyParser.json({ strict: false }));
router.get('/api/releases', fetchReleases); router.get('/api/scenes', fetchScenes);
router.get('/api/releases/:releaseId', fetchRelease); router.get('/api/scenes/:releaseId', fetchScene);
router.get('/api/movies', fetchMovies);
router.get('/api/movies/:releaseId', fetchMovie);
router.get('/api/actors', fetchActors);
router.get('/api/actors/:actorId', fetchActor); router.get('/api/actors/:actorId', fetchActor);
router.get('*', (req, res) => { router.get('*', (req, res) => {