Added favorites button to actor page.
This commit is contained in:
73
src/stashes.js
Normal file
73
src/stashes.js
Normal file
@@ -0,0 +1,73 @@
|
||||
'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,
|
||||
};
|
||||
Reference in New Issue
Block a user