forked from DebaucheryLibrarian/traxxx
74 lines
1.5 KiB
JavaScript
74 lines
1.5 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const knex = require('./knex');
|
||
|
const { HttpError } = require('./errors');
|
||
|
|
||
|
function curateStash(stash) {
|
||
|
const curatedStash = {
|
||
|
id: stash.id,
|
||
|
name: stash.name,
|
||
|
slug: stash.slug,
|
||
|
};
|
||
|
|
||
|
return curatedStash;
|
||
|
}
|
||
|
|
||
|
async function fetchStash(stashId, sessionUser) {
|
||
|
if (!sessionUser) {
|
||
|
throw new HttpError('You are not authenthicated', 401);
|
||
|
}
|
||
|
|
||
|
const stash = await knex('stashes')
|
||
|
.where({
|
||
|
id: stashId,
|
||
|
user_id: sessionUser.id,
|
||
|
})
|
||
|
.first();
|
||
|
|
||
|
if (!stash) {
|
||
|
throw new HttpError('You are not authorized to modify this stash', 403);
|
||
|
}
|
||
|
|
||
|
return stash;
|
||
|
}
|
||
|
|
||
|
async function stashActor(actorId, stashId, sessionUser) {
|
||
|
const stash = await fetchStash(stashId, sessionUser);
|
||
|
|
||
|
await knex('stashes_actors')
|
||
|
.insert({
|
||
|
stash_id: stash.id,
|
||
|
actor_id: actorId,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function stashScene(sceneId, stashId, sessionUser) {
|
||
|
const stash = await fetchStash(stashId, sessionUser);
|
||
|
|
||
|
await knex('stashes_scenes')
|
||
|
.insert({
|
||
|
stash_id: stash.id,
|
||
|
actor_id: sceneId,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function unstashActor(actorId, stashId, sessionUser) {
|
||
|
await knex
|
||
|
.from('stashes_actors')
|
||
|
.whereIn('stashes_actors.id', knex('stashes_actors')
|
||
|
.select('stashes_actors.id')
|
||
|
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
|
||
|
.where('stashes.user_id', sessionUser.id) // verify user owns this stash
|
||
|
.where('stashes_actors.actor_id', actorId)
|
||
|
.where('stashes_actors.stash_id', stashId))
|
||
|
.delete();
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
curateStash,
|
||
|
stashActor,
|
||
|
stashScene,
|
||
|
// unstashScene,
|
||
|
unstashActor,
|
||
|
};
|