traxxx/src/stashes.js

86 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-03-15 02:30:47 +00:00
'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,
2021-03-17 01:09:34 +00:00
scene_id: sceneId,
2021-03-15 02:30:47 +00:00
});
}
async function unstashActor(actorId, stashId, sessionUser) {
await knex
2021-03-17 01:09:34 +00:00
.from('stashes_actors AS deletable')
.where('deletable.actor_id', actorId)
.where('deletable.stash_id', stashId)
.whereExists(knex('stashes_actors') // verify user owns this stash
2021-03-15 02:30:47 +00:00
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
2021-03-17 01:09:34 +00:00
.where('stashes_actors.stash_id', knex.raw('deletable.stash_id'))
.where('stashes.user_id', sessionUser.id))
.delete();
}
async function unstashScene(sceneId, stashId, sessionUser) {
await knex
.from('stashes_scenes AS deletable')
.where('deletable.scene_id', sceneId)
.where('deletable.stash_id', stashId)
.whereExists(knex('stashes_scenes') // verify user owns this stash
.leftJoin('stashes', 'stashes.id', 'stashes_scenes.stash_id')
.where('stashes_scenes.stash_id', knex.raw('deletable.stash_id'))
.where('stashes.user_id', sessionUser.id))
2021-03-15 02:30:47 +00:00
.delete();
}
module.exports = {
curateStash,
stashActor,
stashScene,
2021-03-17 01:09:34 +00:00
unstashScene,
2021-03-15 02:30:47 +00:00
unstashActor,
};