Refactor: Better Post Metrics data fetching
This commit is contained in:
+27
-12
@@ -3,15 +3,32 @@ import { getUser, getUsersCachedByRef } from './user';
|
||||
|
||||
/**
|
||||
* @typedef {import('$types/base').Post} Post
|
||||
* @typedef {import('$types/base').PostMetrics} PostMetrics
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {import('postgres').Row} row
|
||||
* @returns {PostMetrics}
|
||||
*/
|
||||
function parsePostMetricsFromRow(row) {
|
||||
return {
|
||||
commentCount: BigInt(row['comment_count']),
|
||||
userCount: BigInt(row['user_count']),
|
||||
latestActivity: row['latest_activity'],
|
||||
engagement: BigInt(row['engagement']),
|
||||
age: row['age'],
|
||||
relevancy: row['relevancy'],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('$types/base').User | null} author
|
||||
* @param {import('$types/base').Category} category
|
||||
* @param {import('postgres').Row} row
|
||||
* @param {boolean} withMetrics
|
||||
* @returns {Post}
|
||||
*/
|
||||
function parsePostFromRow(author, category, row) {
|
||||
function parsePostFromRow(author, category, row, withMetrics = false) {
|
||||
return {
|
||||
id: row['id'],
|
||||
author: author,
|
||||
@@ -22,7 +39,8 @@ function parsePostFromRow(author, category, row) {
|
||||
rating: {
|
||||
likes: BigInt(row['likes']),
|
||||
dislikes: BigInt(row['dislikes']),
|
||||
}
|
||||
},
|
||||
metrics: withMetrics ? parsePostMetricsFromRow(row) : null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -31,19 +49,15 @@ function parsePostFromRow(author, category, row) {
|
||||
* @param {import('$types/base').Category | undefined} category
|
||||
* @param {number} limit
|
||||
* @param {number} offset
|
||||
* @param {boolean} withMetrics
|
||||
* @returns {Promise<Post[]>}
|
||||
*/
|
||||
export async function getPosts(sql, category = undefined, limit = 10, offset = 0) {
|
||||
let filter;
|
||||
|
||||
if (category === undefined) {
|
||||
filter = sql``;
|
||||
} else {
|
||||
filter = sql`WHERE category_id = ${ category.id }`;
|
||||
}
|
||||
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`;
|
||||
|
||||
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
|
||||
${ filter }
|
||||
FETCH FIRST ${ limit } ROWS ONLY
|
||||
@@ -60,7 +74,8 @@ export async function getPosts(sql, category = undefined, limit = 10, offset = 0
|
||||
return posts.map(row => parsePostFromRow(
|
||||
users.get(row['author_id']) || null,
|
||||
/** @type {import('$types/base').Category} */ (categories.get(row['category_id'])),
|
||||
row
|
||||
row,
|
||||
withMetrics
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,46 +1,89 @@
|
||||
/**
|
||||
* @typedef {import('$types/base').PostMetrics} PostMetrics
|
||||
*/
|
||||
// /**
|
||||
// * @typedef {import('$types/base').PostMetrics} PostMetrics
|
||||
// */
|
||||
|
||||
import { sql } from '$lib/db.server';
|
||||
// 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
|
||||
// * @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('$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 }`;
|
||||
// /**
|
||||
// * @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']
|
||||
// };
|
||||
// }
|
||||
|
||||
const result = await query;
|
||||
// /**
|
||||
// * @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 }`;
|
||||
|
||||
if (result.length == 0) {
|
||||
return {
|
||||
error: true,
|
||||
msg: `Could not find PostMetrics for Post ID ${post.id}`
|
||||
};
|
||||
}
|
||||
// const result = await query;
|
||||
|
||||
return parsePostMetricsFromRow(post, result[0]);
|
||||
}
|
||||
// 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