Separated user page stash component.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
const knex = require('./knex');
|
||||
const { HttpError } = require('./errors');
|
||||
const slugify = require('./utils/slugify');
|
||||
|
||||
function curateStash(stash) {
|
||||
if (!stash) {
|
||||
@@ -17,6 +18,17 @@ function curateStash(stash) {
|
||||
return curatedStash;
|
||||
}
|
||||
|
||||
function curateStashEntry(stash, user) {
|
||||
const curatedStashEntry = {
|
||||
user_id: user.id,
|
||||
name: stash.name,
|
||||
slug: slugify(stash.name),
|
||||
public: false,
|
||||
};
|
||||
|
||||
return curatedStashEntry;
|
||||
}
|
||||
|
||||
async function fetchStash(stashId, sessionUser) {
|
||||
if (!sessionUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
@@ -36,6 +48,18 @@ async function fetchStash(stashId, sessionUser) {
|
||||
return curateStash(stash);
|
||||
}
|
||||
|
||||
async function createStash(newStash, sessionUser) {
|
||||
if (!sessionUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
}
|
||||
|
||||
const stash = await knex('stashes')
|
||||
.insert(curateStashEntry(newStash, sessionUser))
|
||||
.returning('*');
|
||||
|
||||
return curateStash(stash);
|
||||
}
|
||||
|
||||
async function updateStash(stashId, newStash, sessionUser) {
|
||||
if (!sessionUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
@@ -123,6 +147,7 @@ async function unstashMovie(movieId, stashId, sessionUser) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createStash,
|
||||
curateStash,
|
||||
stashActor,
|
||||
stashScene,
|
||||
|
||||
@@ -44,6 +44,7 @@ const {
|
||||
} = require('./tags');
|
||||
|
||||
const {
|
||||
createStash,
|
||||
stashActor,
|
||||
stashScene,
|
||||
stashMovie,
|
||||
@@ -84,6 +85,7 @@ async function initServer() {
|
||||
|
||||
router.post('/api/users', signup);
|
||||
|
||||
router.post('/api/stashes', createStash);
|
||||
router.patch('/api/stashes/:stashId', updateStash);
|
||||
|
||||
router.post('/api/stashes/:stashId/actors', stashActor);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
createStash,
|
||||
stashActor,
|
||||
stashScene,
|
||||
stashMovie,
|
||||
@@ -10,6 +11,12 @@ const {
|
||||
updateStash,
|
||||
} = require('../stashes');
|
||||
|
||||
async function createStashApi(req, res) {
|
||||
const stash = await createStash(req.body, req.session.user);
|
||||
|
||||
res.send(stash);
|
||||
}
|
||||
|
||||
async function updateStashApi(req, res) {
|
||||
const stash = await updateStash(req.params.stashId, req.body, req.session.user);
|
||||
|
||||
@@ -53,6 +60,7 @@ async function unstashMovieApi(req, res) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createStash: createStashApi,
|
||||
stashActor: stashActorApi,
|
||||
stashScene: stashSceneApi,
|
||||
stashMovie: stashMovieApi,
|
||||
|
||||
Reference in New Issue
Block a user