2019-11-10 03:20:22 +00:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
2019-11-11 02:20:00 +00:00
|
|
|
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,
|
|
|
|
})), '*');
|
|
|
|
}
|
|
|
|
|
2019-11-10 03:20:22 +00:00
|
|
|
module.exports = {
|
|
|
|
fetchActors,
|
2019-11-11 02:20:00 +00:00
|
|
|
storeActors,
|
2019-11-10 03:20:22 +00:00
|
|
|
};
|