Skip to content

Commit

Permalink
Merge pull request #6 from CSSE6400/jroute
Browse files Browse the repository at this point in the history
Adds some more routes
  • Loading branch information
JTrenerry authored Apr 25, 2024
2 parents 169b9f1 + 77896ab commit 7f5fd0d
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions backend/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,20 @@ export const router = Router();
*
*/

// Comments by question id
router.get('/questions/:questionId', async (req: Request, res: Response) => {

// Gets comment by comment id
router.get('/comments/:commentId', async (req: Request, res: Response) => {
const commentID = req.params.commentId;
const comment = await db.query(`
SELECT commentsid, parentcommentid, commenttext, commentpng, iscorrect, isendorsed, upvotes, downvotes, created_at, updated_at
FROM comments
WHERE comments.commentsid = $1
`, [commentID]);
res.status(200).json(comment.rows[0]);
});

// Gets all comments by question id
router.get('/questions/:questionId/comments', async (req: Request, res: Response) => {
const questionID = req.params.questionId;
const question = await db.query(`
SELECT commentsid, parentcommentid, commenttext, commentpng, iscorrect, isendorsed, upvotes, downvotes, created_at, updated_at
Expand All @@ -53,6 +65,17 @@ router.get('/questions/:questionId', async (req: Request, res: Response) => {
res.status(200).json(nest(question.rows));
});

// Gets question information by question id
router.get('/questions/:questionId', async (req: Request, res: Response) => {
const questionID = req.params.questionId;
const question = await db.query(`
SELECT questionID, questionText, questionType, questionpng
FROM questions
WHERE questions.questionID = $1
`, [questionID]);
res.status(200).json(question.rows[0]);
});

// Exam questions by exam ID
router.get('/exams/:examId/questions', async (req: Request, res: Response) => {
const examID = req.params.examId;
Expand Down Expand Up @@ -144,7 +167,7 @@ interface CommentObject {

// Helper functions

// Function to nest comments into their parent comments
// function to nest comments into their parent comments
function nest(jsonData: any[]) {
const dataDict: { [id: number]: CommentObject } = {};
jsonData.forEach(item => dataDict[item.commentsid] = item);
Expand All @@ -162,3 +185,23 @@ function nest(jsonData: any[]) {
const resultJsonData = jsonData.filter(item => item.parentcommentid === null);
return resultJsonData;
}

// function to return one comment with its children
function single_nest(jsonData: any[], commentID: number) {
const dataDict: { [id: number]: CommentObject } = {};
jsonData.forEach(item => dataDict[item.commentsid] = item);

jsonData.forEach(item => {
if (item.parentcommentid !== null) {
const parent = dataDict[item.parentcommentid];
if (!parent.children) {
parent.children = [];
}
parent.children.push(item);
}
});

const resultJsonData = jsonData.filter(item => item.commentsid !== commentID);
return resultJsonData;
}

0 comments on commit 7f5fd0d

Please sign in to comment.