traxxx/src/actors.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
const knex = require('./knex');
async function curateActor(actor) {
const aliases = await knex('actors')
.where({ alias_for: actor.id });
return {
id: actor.id,
name: actor.name,
description: actor.description,
birthdate: actor.birthdate && new Date(actor.birthdate),
country: actor.country_alpha2,
city: actor.city,
ethnicity: actor.ethnicity,
height: actor.height,
boobSize: actor.boobs_size,
boobsNatural: actor.boobs_natural,
aliases: aliases.map(({ name }) => name),
slug: actor.slug,
};
}
function curateActors(releases) {
return Promise.all(releases.map(async release => curateActor(release)));
}
async function fetchActors(actorId, actorSlug) {
const releases = await knex('actors')
.where({ id: actorId })
.orWhere({ slug: actorSlug })
.limit(100);
return curateActors(releases);
}
async function storeActors(release, releaseEntry) {
const actors = await knex('actors').whereIn('name', release.actors);
const newActors = release.actors.filter(actorName => !actors.some(actor => actor.name === actorName));
const { rows: insertedActors } = newActors.length
? await knex.raw(`${knex('actors').insert(newActors.map(actorName => ({
name: actorName,
slug: actorName.toLowerCase().replace(/\s+/g, '-'),
})))} ON CONFLICT DO NOTHING RETURNING *`)
: { rows: [] };
return knex('actors_associated').insert(actors.concat(insertedActors).map(actor => ({
release_id: releaseEntry.id,
actor_id: actor.id,
})), '*');
}
module.exports = {
fetchActors,
storeActors,
};