Added shelf template shared between shelf and post page.
This commit is contained in:
37
src/posts.js
37
src/posts.js
@@ -6,6 +6,13 @@ import { HttpError } from './errors';
|
||||
import { fetchShelf } from './shelves';
|
||||
import { fetchUsers } from './users';
|
||||
|
||||
const emptyVote = {
|
||||
tally: 0,
|
||||
total: 0,
|
||||
bump: false,
|
||||
sink: false,
|
||||
};
|
||||
|
||||
function curateDatabasePost(post, {
|
||||
shelf, users, votes,
|
||||
}) {
|
||||
@@ -18,7 +25,7 @@ function curateDatabasePost(post, {
|
||||
createdAt: post.created_at,
|
||||
shelf,
|
||||
user: users.find((user) => user.id === post.user_id),
|
||||
votes: votes[post.id],
|
||||
vote: votes[post.id] || emptyVote,
|
||||
commentCount: Number(post.comment_count),
|
||||
};
|
||||
|
||||
@@ -123,19 +130,27 @@ async function createPost(post, shelfId, user) {
|
||||
}
|
||||
|
||||
async function votePost(postId, value, user) {
|
||||
await knex('posts_votes')
|
||||
.insert({
|
||||
value,
|
||||
post_id: postId,
|
||||
user_id: user.id,
|
||||
})
|
||||
.onConflict(['post_id', 'user_id'])
|
||||
.merge()
|
||||
.returning('value');
|
||||
if (value === 0) {
|
||||
await knex('posts_votes')
|
||||
.where({
|
||||
post_id: postId,
|
||||
user_id: user.id,
|
||||
})
|
||||
.delete();
|
||||
} else {
|
||||
await knex('posts_votes')
|
||||
.insert({
|
||||
value,
|
||||
post_id: postId,
|
||||
user_id: user.id,
|
||||
})
|
||||
.onConflict(['post_id', 'user_id'])
|
||||
.merge();
|
||||
}
|
||||
|
||||
const votes = await fetchPostVotes([postId], user);
|
||||
|
||||
return votes[postId];
|
||||
return votes[postId] || emptyVote;
|
||||
}
|
||||
|
||||
export {
|
||||
|
||||
Reference in New Issue
Block a user