Added shelf template shared between shelf and post page.

This commit is contained in:
2023-06-25 20:43:42 +02:00
parent f3c8718fb2
commit 041b502776
6 changed files with 179 additions and 104 deletions

View File

@@ -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 {