Implemented comment viewing and posting

This commit is contained in:
2026-03-01 12:52:22 -05:00
parent 5cdbf72328
commit 5483e42bb4
9 changed files with 203 additions and 18 deletions

View File

@@ -1,10 +1,9 @@
import { toDateTime } from "@app/shared/utils/time";
import pool from "../../db";
import { LOARequest, LOAType } from '@app/shared/types/loa'
import { PagedData } from '@app/shared/types/pagination'
import { DiscussionPost } from '@app/shared/types/discussion';
import { DiscussionComment } from '@app/shared/types/discussion';
/**
* Retrieves all discussion posts with pagination and optional type filtering.
@@ -98,7 +97,8 @@ export async function createDiscussion<T>(type: string, authorID: number, postTi
* @returns {Promise<DiscussionPost<T> | null>} The discussion post or null if not found
*/
export async function getDiscussionById<T>(id: number): Promise<DiscussionPost<T> | null> {
const sql = `
// Get the post
const postSql = `
SELECT
p.*,
m.name as poster_name
@@ -107,14 +107,42 @@ export async function getDiscussionById<T>(id: number): Promise<DiscussionPost<T
WHERE p.id = ?
LIMIT 1;
`;
const results = (await pool.query(sql, [id])) as DiscussionPost<T>[];
if (results.length === 0) {
const postResults = (await pool.query(postSql, [id])) as DiscussionPost<T>[];
if (postResults.length === 0) {
return null;
}
return results[0];
const post = postResults[0];
// Get comments for the post
const commentSql = `
SELECT
c.*
FROM discussion_comments AS c
WHERE c.post_id = ?
AND c.is_deleted = FALSE
ORDER BY c.created_at ASC;
`;
const comments = (await pool.query(commentSql, [id])) as DiscussionComment[];
// Attach comments to post
post.comments = comments;
return post;
}
export async function postComment() {
export async function getPostComments(postID: number): Promise<DiscussionComment[]> {
let comments = await pool.query("SELECT * FROM discussion_comments WHERE post_id = ?", [postID]);
return comments;
}
export async function postComment(commentData: DiscussionComment, poster: number) {
const sql = `
INSERT INTO discussion_comments (post_id, poster_id, content) VALUES (?, ?, ?);
`;
const result = await pool.query(sql, [commentData.post_id, poster, commentData.content]);
if (!result.affectedRows || result.affectedRows !== 1) {
throw new Error('Failed to insert comment: expected 1 row to be inserted');
}
}