Add: Post Metrics
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @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 parsePostMetricsFromRow(post, row) {
|
||||
return {
|
||||
post: post,
|
||||
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('$types/base').Post} post
|
||||
* @returns {Promise<PostMetrics | import('$types/error').Error>}
|
||||
*/
|
||||
export async function getPostMetrics(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 parsePostMetricsFromRow(post, result[0]);
|
||||
}
|
||||
Reference in New Issue
Block a user