Added basic comments.
This commit is contained in:
44
src/comments.js
Normal file
44
src/comments.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import knex from './knex';
|
||||
import { fetchUsers } from './users';
|
||||
import { verifyPrivilege } from './privileges';
|
||||
|
||||
function curateDatabaseComment(comment, { users }) {
|
||||
return {
|
||||
id: comment.id,
|
||||
body: comment.body,
|
||||
userId: comment.user_id,
|
||||
user: users.find((user) => user.id === comment.user_id),
|
||||
createdAt: comment.created_at,
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchPostComments(postId, { limit = 100 } = {}) {
|
||||
const comments = await knex('comments')
|
||||
.where('post_id', postId)
|
||||
.orderBy('created_at', 'asc')
|
||||
.limit(limit);
|
||||
|
||||
const users = await fetchUsers(comments.map((comment) => comment.user_id));
|
||||
|
||||
return comments.map((comment) => curateDatabaseComment(comment, { users }));
|
||||
}
|
||||
|
||||
async function addComment(comment, postId, user) {
|
||||
await verifyPrivilege('addComment', user);
|
||||
|
||||
const commentEntry = await knex('comments')
|
||||
.insert({
|
||||
body: comment.body,
|
||||
post_id: postId,
|
||||
user_id: user.id,
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
console.log(comment, user);
|
||||
return curateDatabaseComment(commentEntry);
|
||||
}
|
||||
|
||||
export {
|
||||
fetchPostComments,
|
||||
addComment,
|
||||
};
|
||||
53
src/posts.js
53
src/posts.js
@@ -3,36 +3,62 @@ import { verifyPrivilege } from './privileges';
|
||||
import knex from './knex';
|
||||
import { HttpError } from './errors';
|
||||
|
||||
import { fetchShelf, curateDatabaseShelf } from './shelves';
|
||||
import { curateDatabaseUser } from './users';
|
||||
import { fetchShelf } from './shelves';
|
||||
import { fetchUsers } from './users';
|
||||
|
||||
function curatePost(post) {
|
||||
function curatePost(post, { shelf, users }) {
|
||||
const curatedPost = {
|
||||
id: post.id,
|
||||
title: post.title,
|
||||
body: post.body,
|
||||
url: post.url,
|
||||
link: post.link,
|
||||
shelfId: post.shelf_id,
|
||||
createdAt: post.created_at,
|
||||
shelf: curateDatabaseShelf(post.shelf),
|
||||
user: curateDatabaseUser(post.user),
|
||||
shelf,
|
||||
user: users.find((user) => user.id === post.user_id),
|
||||
commentCount: Number(post.comment_count),
|
||||
};
|
||||
|
||||
return curatedPost;
|
||||
}
|
||||
|
||||
async function fetchShelfPosts(shelfId, limit = 100) {
|
||||
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')
|
||||
.select('posts.*', knex.raw('count(comments.id) as comment_count'))
|
||||
.leftJoin('comments', 'comments.post_id', 'posts.id')
|
||||
.where('shelf_id', shelf.id)
|
||||
.orderBy('created_at', 'desc')
|
||||
.groupBy('posts.id')
|
||||
.limit(limit);
|
||||
|
||||
return posts.map((post) => curatePost(post));
|
||||
const users = await fetchUsers(posts.map((post) => post.user_id));
|
||||
|
||||
return posts.map((post) => curatePost(post, { shelf, users }));
|
||||
}
|
||||
|
||||
async function fetchPost(postId) {
|
||||
const post = await knex('posts')
|
||||
.select('posts.*', knex.raw('count(comments.id) as comment_count'))
|
||||
.leftJoin('comments', 'comments.post_id', 'posts.id')
|
||||
.where('posts.id', postId)
|
||||
.groupBy('posts.id')
|
||||
.first();
|
||||
|
||||
if (!post) {
|
||||
throw new HttpError({
|
||||
statusMessage: 'This post does not exist',
|
||||
statusCode: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const [shelf, users] = await Promise.all([
|
||||
fetchShelf(post.shelf_id),
|
||||
fetchUsers([post.user_id]),
|
||||
]);
|
||||
|
||||
return curatePost(post, { shelf, users });
|
||||
}
|
||||
|
||||
async function createPost(post, shelfId, user) {
|
||||
@@ -47,11 +73,13 @@ async function createPost(post, shelfId, user) {
|
||||
});
|
||||
}
|
||||
|
||||
console.log(post);
|
||||
|
||||
const postId = await knex('posts')
|
||||
.insert({
|
||||
title: post.title,
|
||||
body: post.body,
|
||||
url: post.url,
|
||||
link: post.link,
|
||||
shelf_id: shelf.id,
|
||||
user_id: user.id,
|
||||
})
|
||||
@@ -62,5 +90,6 @@ async function createPost(post, shelfId, user) {
|
||||
|
||||
export {
|
||||
createPost,
|
||||
fetchPost,
|
||||
fetchShelfPosts,
|
||||
};
|
||||
|
||||
@@ -8,6 +8,7 @@ function curateDatabaseShelf(shelf) {
|
||||
return {
|
||||
id: shelf.id,
|
||||
slug: shelf.slug,
|
||||
name: shelf.slug,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,12 @@ async function login(credentials) {
|
||||
return curateDatabaseUser(user);
|
||||
}
|
||||
|
||||
async function fetchUsers(userIds) {
|
||||
const users = await knex('users').whereIn('id', userIds);
|
||||
|
||||
return users.map((user) => curateDatabaseUser(user));
|
||||
}
|
||||
|
||||
async function createUser(credentials, context) {
|
||||
if (!credentials.username) {
|
||||
throw new HttpError({
|
||||
@@ -145,5 +151,6 @@ async function createUser(credentials, context) {
|
||||
export {
|
||||
curateDatabaseUser,
|
||||
createUser,
|
||||
fetchUsers,
|
||||
login,
|
||||
};
|
||||
|
||||
11
src/web/comments.js
Normal file
11
src/web/comments.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { addComment } from '../comments';
|
||||
|
||||
async function addCommentApi(req, res) {
|
||||
const comment = await addComment(req.body, req.params.postId, req.user);
|
||||
|
||||
res.send(comment);
|
||||
}
|
||||
|
||||
export {
|
||||
addCommentApi as addComment,
|
||||
};
|
||||
@@ -21,8 +21,6 @@ export default async function initDefaultHandler() {
|
||||
|
||||
const body = await httpResponse.getBody();
|
||||
|
||||
console.log(pageContext.pageData);
|
||||
|
||||
if (res.writeEarlyHints) {
|
||||
res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) });
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
import { createShelf } from './shelves';
|
||||
|
||||
import { createPost } from './posts';
|
||||
import { addComment } from './comments';
|
||||
|
||||
const logger = initLogger();
|
||||
|
||||
@@ -70,6 +71,9 @@ async function startServer() {
|
||||
// POSTS
|
||||
router.post('/api/shelves/:shelfId/posts', createPost);
|
||||
|
||||
// COMMENTS
|
||||
router.post('/api/posts/:postId/comments', addComment);
|
||||
|
||||
router.get('*', defaultHandler);
|
||||
router.use(errorHandler);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user