Added basic post creation.
This commit is contained in:
66
src/posts.js
Normal file
66
src/posts.js
Normal file
@@ -0,0 +1,66 @@
|
||||
// import knex from './knex';
|
||||
import { verifyPrivilege } from './privileges';
|
||||
import knex from './knex';
|
||||
import { HttpError } from './errors';
|
||||
|
||||
import { fetchShelf, curateDatabaseShelf } from './shelves';
|
||||
import { curateDatabaseUser } from './users';
|
||||
|
||||
function curatePost(post) {
|
||||
const curatedPost = {
|
||||
id: post.id,
|
||||
title: post.title,
|
||||
body: post.body,
|
||||
url: post.url,
|
||||
shelfId: post.shelf_id,
|
||||
createdAt: post.created_at,
|
||||
shelf: curateDatabaseShelf(post.shelf),
|
||||
user: curateDatabaseUser(post.user),
|
||||
};
|
||||
|
||||
return curatedPost;
|
||||
}
|
||||
|
||||
async function fetchShelfPosts(shelfId, limit = 100) {
|
||||
const shelf = await fetchShelf(shelfId);
|
||||
|
||||
const posts = await knex('posts')
|
||||
.select('posts.*', knex.raw('row_to_json(users) as user'), knex.raw('row_to_json(shelves) as shelf'))
|
||||
.leftJoin('users', 'users.id', 'posts.user_id')
|
||||
.leftJoin('shelves', 'shelves.id', 'posts.shelf_id')
|
||||
.where('shelf_id', shelf.id)
|
||||
.orderBy('created_at', 'desc')
|
||||
.limit(limit);
|
||||
|
||||
return posts.map((post) => curatePost(post));
|
||||
}
|
||||
|
||||
async function createPost(post, shelfId, user) {
|
||||
await verifyPrivilege('createPost', user);
|
||||
|
||||
const shelf = await fetchShelf(shelfId);
|
||||
|
||||
if (!shelf) {
|
||||
throw new HttpError({
|
||||
statusMessage: 'The target shelf does not exist',
|
||||
statusCode: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const postId = await knex('posts')
|
||||
.insert({
|
||||
title: post.title,
|
||||
body: post.body,
|
||||
url: post.url,
|
||||
shelf_id: shelf.id,
|
||||
user_id: user.id,
|
||||
})
|
||||
.returning('id');
|
||||
|
||||
return postId;
|
||||
}
|
||||
|
||||
export {
|
||||
createPost,
|
||||
fetchShelfPosts,
|
||||
};
|
||||
18
src/privileges.js
Normal file
18
src/privileges.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { HttpError } from './errors';
|
||||
|
||||
function verifyPrivilege(privilege, user, context) {
|
||||
if (!user) {
|
||||
throw new HttpError({
|
||||
statusMessage: 'You are not authenticated',
|
||||
statusCode: 401,
|
||||
});
|
||||
}
|
||||
|
||||
console.log('verify privilege', privilege, user, context);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export {
|
||||
verifyPrivilege,
|
||||
};
|
||||
@@ -51,4 +51,5 @@ export {
|
||||
fetchShelf,
|
||||
fetchShelves,
|
||||
createShelf,
|
||||
curateDatabaseShelf,
|
||||
};
|
||||
|
||||
@@ -143,6 +143,7 @@ async function createUser(credentials, context) {
|
||||
}
|
||||
|
||||
export {
|
||||
curateDatabaseUser,
|
||||
createUser,
|
||||
login,
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ export default function errorHandler(error, req, res, _next) {
|
||||
if (error.statusCode) {
|
||||
res.status(error.statusCode).send({
|
||||
statusCode: error.statusCode,
|
||||
message: error.statusMessage,
|
||||
statusMessage: error.statusMessage,
|
||||
});
|
||||
|
||||
return;
|
||||
|
||||
11
src/web/posts.js
Normal file
11
src/web/posts.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { createPost } from '../posts';
|
||||
|
||||
async function createPostApi(req, res) {
|
||||
const post = await createPost(req.body, req.params.shelfId, req.user);
|
||||
|
||||
res.send(post);
|
||||
}
|
||||
|
||||
export {
|
||||
createPostApi as createPost,
|
||||
};
|
||||
@@ -25,6 +25,8 @@ import {
|
||||
|
||||
import { createShelf } from './shelves';
|
||||
|
||||
import { createPost } from './posts';
|
||||
|
||||
const logger = initLogger();
|
||||
|
||||
async function startServer() {
|
||||
@@ -54,13 +56,19 @@ async function startServer() {
|
||||
app.use(viteDevMiddleware);
|
||||
}
|
||||
|
||||
// SESSIONS
|
||||
router.get('/api/session', fetchUser);
|
||||
router.post('/api/session', login);
|
||||
router.delete('/api/session', logout);
|
||||
|
||||
// USERS
|
||||
router.post('/api/users', createUser);
|
||||
|
||||
// SHELVES
|
||||
router.post('/api/shelves', createShelf);
|
||||
|
||||
router.post('/api/users', createUser);
|
||||
// POSTS
|
||||
router.post('/api/shelves/:shelfId/posts', createPost);
|
||||
|
||||
router.get('*', defaultHandler);
|
||||
router.use(errorHandler);
|
||||
|
||||
Reference in New Issue
Block a user