Refactor: Better PostMetric API and Rendering
This commit is contained in:
+28
-10
@@ -46,15 +46,24 @@ function parsePostFromRow(author, category, row, withMetrics = false) {
|
||||
|
||||
/**
|
||||
* @param {import('postgres').Sql} sql
|
||||
* @param {import('$types/base').Category | undefined} category
|
||||
* @param {number} limit
|
||||
* @param {number} offset
|
||||
* @param {boolean} withMetrics
|
||||
* @param {{
|
||||
* category?: import('$types/base').Category | undefined,
|
||||
* limit?: number,
|
||||
* offset?: number,
|
||||
* withMetrics?: boolean
|
||||
* }} opts
|
||||
* @returns {Promise<Post[]>}
|
||||
*/
|
||||
export async function getPosts(sql, category = undefined, limit = 10, offset = 0, withMetrics = false) {
|
||||
const filter = category === undefined ? sql`` : sql`WHERE category_id = ${ category.id }`;
|
||||
const metrics = !withMetrics ? sql`` : sql`, comment_count, user_count, latest_activity, engagement, age, relevancy`;
|
||||
export async function getPosts(sql, opts = {}) {
|
||||
const {
|
||||
category = undefined,
|
||||
limit = 10,
|
||||
offset = 0,
|
||||
withMetrics = false,
|
||||
} = opts;
|
||||
|
||||
const filter = category ? sql`WHERE category_id = ${ category.id }` : sql``;
|
||||
const metrics = withMetrics ? sql`, comment_count, user_count, latest_activity, engagement, age, relevancy` : sql``;
|
||||
|
||||
const query = sql`
|
||||
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes ${ metrics }
|
||||
@@ -83,11 +92,20 @@ export async function getPosts(sql, category = undefined, limit = 10, offset = 0
|
||||
*
|
||||
* @param {import('postgres').Sql} sql
|
||||
* @param {number} post_id
|
||||
* @param {{
|
||||
* withMetrics?: boolean
|
||||
* }} opts
|
||||
* @returns {Promise<Post | import('$types/error').Error>}
|
||||
*/
|
||||
export async function getPost(sql, post_id) {
|
||||
export async function getPost(sql, post_id, opts = {}) {
|
||||
const {
|
||||
withMetrics = false
|
||||
} = opts;
|
||||
|
||||
const metrics = withMetrics ? sql`, comment_count, user_count, latest_activity, engagement, age, relevancy` : sql``;
|
||||
|
||||
const query = sql`
|
||||
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes
|
||||
SELECT id, author_id, name, category_id, latest_content, created_date, likes, dislikes ${ metrics }
|
||||
FROM doki8902.message_post
|
||||
WHERE id = ${ post_id };`;
|
||||
|
||||
@@ -130,5 +148,5 @@ export async function getPost(sql, post_id) {
|
||||
/**
|
||||
* @type {Post}
|
||||
*/
|
||||
return parsePostFromRow(author, category, post);
|
||||
return parsePostFromRow(author, category, post, withMetrics);
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
// /**
|
||||
// * @typedef {import('$types/base').PostMetrics} PostMetrics
|
||||
// */
|
||||
|
||||
// import { sql } from '$lib/db.server';
|
||||
|
||||
// /**
|
||||
// * @param {import('$types/base').Post} post
|
||||
// * @param {import('postgres').Row} row
|
||||
// * @returns {PostMetrics}
|
||||
// */
|
||||
// function parsePostMetricsFromRowAndPost(post, row) {
|
||||
// return {
|
||||
// postId: post.id,
|
||||
// commentCount: BigInt(row['comment_count']),
|
||||
// userCount: BigInt(row['user_count']),
|
||||
// latestActivity: row['latest_activity'],
|
||||
// rating: post.rating,
|
||||
// score: BigInt(row['score']),
|
||||
// engagement: BigInt(row['engagement']),
|
||||
// age: row['age'],
|
||||
// relevancy: row['relevancy']
|
||||
// };
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param {import('postgres').Row} row
|
||||
// * @returns {PostMetrics}
|
||||
// */
|
||||
// function parsePostMetricsFromRow(row) {
|
||||
// return {
|
||||
// postId: row['post_id'],
|
||||
// commentCount: BigInt(row['comment_count']),
|
||||
// userCount: BigInt(row['user_count']),
|
||||
// latestActivity: row['latest_activity'],
|
||||
// rating: {
|
||||
// likes: row['likes'],
|
||||
// dislikes: row['dislikes']
|
||||
// },
|
||||
// score: BigInt(row['score']),
|
||||
// engagement: BigInt(row['engagement']),
|
||||
// age: row['age'],
|
||||
// relevancy: row['relevancy']
|
||||
// };
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param {import('$types/base').Post} post
|
||||
// * @returns {Promise<PostMetrics | import('$types/error').Error>}
|
||||
// */
|
||||
// export async function getPostMetricsForPost(post) {
|
||||
// const query = sql`
|
||||
// SELECT comment_count, user_count, latest_activity, score, engagement, age, relevancy
|
||||
// FROM doki8902.post_metrics
|
||||
// WHERE post_id = ${ post.id }`;
|
||||
|
||||
// const result = await query;
|
||||
|
||||
// if (result.length == 0) {
|
||||
// return {
|
||||
// error: true,
|
||||
// msg: `Could not find PostMetrics for Post ID ${post.id}`
|
||||
// };
|
||||
// }
|
||||
|
||||
// return parsePostMetricsFromRowAndPost(post, result[0]);
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param {number} post_id
|
||||
// * @returns {Promise<PostMetrics | import('$types/error').Error>}
|
||||
// */
|
||||
// export async function getPostMetricsForPostId(post_id) {
|
||||
// const query = sql`
|
||||
// SELECT post_id, comment_count, user_count, latest_activity, likes, dislikes, score, engagement, age, relevancy
|
||||
// FROM doki8902.post_metrics
|
||||
// WHERE post_id = ${ post_id }`;
|
||||
|
||||
// const result = await query;
|
||||
|
||||
// if (result.length == 0) {
|
||||
// return {
|
||||
// error: true,
|
||||
// msg: `Could not find PostMetrics for Post ID ${post_id}`
|
||||
// };
|
||||
// }
|
||||
|
||||
// return parsePostMetricsFromRow(result[0]);
|
||||
// }
|
||||
Reference in New Issue
Block a user