Implemented comment viewing and posting
This commit is contained in:
@@ -105,6 +105,7 @@ import { calendarRouter } from './routes/calendar';
|
||||
import { docsRouter } from './routes/docs';
|
||||
import { units } from './routes/units';
|
||||
import { modRequestRouter } from './routes/modRequest'
|
||||
import { discussionRouter } from './routes/discussion';
|
||||
|
||||
app.use('/application', applicationRouter);
|
||||
app.use('/ranks', ranks);
|
||||
@@ -121,6 +122,7 @@ app.use('/calendar', calendarRouter)
|
||||
app.use('/units', units)
|
||||
app.use('/docs', docsRouter)
|
||||
app.use('/mod-request', modRequestRouter)
|
||||
app.use('/discussions', discussionRouter)
|
||||
app.use('/', authRouter)
|
||||
|
||||
app.get('/ping', (req, res) => {
|
||||
|
||||
35
api/src/routes/discussion.ts
Normal file
35
api/src/routes/discussion.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
import { Request, Response } from 'express';
|
||||
import { requireLogin, requireMemberState, requireRole } from '../middleware/auth';
|
||||
import { logger } from '../services/logging/logger';
|
||||
import { audit } from '../services/logging/auditLog';
|
||||
import { MemberState } from '@app/shared/types/member';
|
||||
import { createDiscussion, getAllDiscussions, getDiscussionById, postComment } from '../services/db/discussionService';
|
||||
import { ModRequest } from '@app/shared/schemas/modRequest';
|
||||
import { DiscussionComment } from '@app/shared/types/discussion';
|
||||
|
||||
router.use(requireLogin);
|
||||
router.use(requireMemberState(MemberState.Member));
|
||||
|
||||
router.post('/comment', async (req: Request, res: Response) => {
|
||||
try {
|
||||
let comment = req.body as DiscussionComment;
|
||||
|
||||
console.log(comment);
|
||||
|
||||
await postComment(comment, req.user.id);
|
||||
|
||||
res.sendStatus(201);
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/comment/:id', async (req: Request, res: Response) => {
|
||||
|
||||
})
|
||||
|
||||
|
||||
export const discussionRouter = router;
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user