Skip to content

Commit

Permalink
Merge pull request #34 from CSSE6400/86
Browse files Browse the repository at this point in the history
fixes in qutestion routes, test suite for questions finish and removed ! from all  messages from backend
  • Loading branch information
86LAK authored May 3, 2024
2 parents 36f7eba + 403fd2c commit d7d6888
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 55 deletions.
4 changes: 3 additions & 1 deletion backend/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export const setupTables = () => {
"examId" INTEGER REFERENCES exams("examId"),
"questionText" TEXT,
"questionPNG" BYTEA,
"questionType" VARCHAR(20)
"questionType" VARCHAR(20),
"created_at" TIMESTAMP DEFAULT NOW(),
"updated_at" TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS comments (
Expand Down
108 changes: 72 additions & 36 deletions backend/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ router.put('/questions/:questionId/edit', async (req: Request<QuestionRouteParam
const { questionText, questionType, questionPNG } = req.body;

if (!questionText && !questionType && !questionPNG) {
res.status(400).json('No changes made!');
res.status(400).json('No changes made');
return;
}

Expand All @@ -64,34 +64,39 @@ router.put('/questions/:questionId/edit', async (req: Request<QuestionRouteParam
count++;
}

query += `WHERE "questionId" = $${count}::int`
query += `updated_at = NOW() WHERE "questionId" = $${count}::int`
args.push(questionId);

const { rowCount } = await db.query(query, args);
if (rowCount === 0) {
res.status(404).json('Question not found!');
res.status(404).json('Question not found');
return;
}

res.status(200).json('Question Edited!');
res.status(200).json('Question edited');
});

// Edits a comment
router.put('/comments/:commentId/edit', async (req: Request<CommentRouteParams, any, CommentBodyParams>, res: Response) => {
const { commentId } = req.params;

if (!req.body.commentText && !req.body.commentPNG) {
res.status(400).json('No changes made!');
res.status(400).json('No changes made');
return;
}

if (!commentId) {
res.status(400).json('Invalid commentId');
return;
}

const { rowCount } = await editComment(commentId, req.body.commentText, req.body.commentPNG);
if (rowCount === 0) {
res.status(404).json('Question not found!');
res.status(404).json('Comment not found');
return;
}

res.status(200).json('Comment Edited!');
res.status(200).json('Comment Edited');
});

/*
Expand All @@ -102,85 +107,90 @@ router.put('/comments/:commentId/edit', async (req: Request<CommentRouteParams,
*
*/

//TODO needs to be tested
// Deletes a comment
router.patch('/comments/:commentId/delete', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;

const { rowCount } = await editComment(commentId, '', '');
if (rowCount === 0) {
res.status(404).json('Question not found!');
res.status(404).json('Question not found');
return;
}

res.status(200).json('Comment Deleted!');
res.status(200).json('Comment Deleted');
});

//TODO needs to be tested
// Sets a comment as correct
router.patch('/comments/:commentId/correct', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;

const { rowCount } = await db.query(`
UPDATE comments
SET "isCorrect" = true, updated_at = NOW()
SET "isCorrect" = true
WHERE "commentId" = $1
`, [commentId]);
if (rowCount === 0) {
res.status(404).json('Comment not found!');
res.status(404).json('Comment not found');
return;
}

res.status(200).json('Corrected!');
res.status(200).json('Corrected');
});

//TODO needs to be tested
// Endorses a comment
router.patch('/comments/:commentId/endorse', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;

const { rowCount } = await db.query(`
UPDATE comments
SET "isEndorsed" = true, updated_at = NOW()
SET "isEndorsed" = true
WHERE "commentId" = $1
`, [commentId]);
if (rowCount === 0) {
res.status(404).json('Comment not found!');
res.status(404).json('Comment not found');
return;
}

res.status(200).json('Endorsed!');
res.status(200).json('Endorsed');
});

//TODO needs to be tested
// Downvotes a comment
router.patch('/comments/:commentId/downvote', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;

const { rowCount } = await db.query(`
UPDATE comments
SET "downvotes" = "downvotes" + 1, updated_at = NOW()
SET "downvotes" = "downvotes" + 1
WHERE "commentId" = $1
`, [commentId]);
if (rowCount === 0) {
res.status(404).json('Comment not found!');
res.status(404).json('Comment not found');
return;
}

res.status(200).json('Downvoted!');
res.status(200).json('Downvoted');
});

//TODO needs to be tested
// Upvotes a comment
router.patch('/comments/:commentId/upvote', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;

const { rowCount } = await db.query(`
UPDATE comments
SET "upvotes" = "upvotes" + 1, updated_at = NOW()
SET "upvotes" = "upvotes" + 1
WHERE "commentId" = $1
`, [commentId]);
if (rowCount === 0) {
res.status(404).json('Comment not found!');
res.status(404).json('Comment not found');
return;
}

res.status(200).json('Upvoted!');
res.status(200).json('Upvoted');
});

/*
Expand All @@ -191,6 +201,7 @@ router.patch('/comments/:commentId/upvote', async (req: Request<CommentRoutePara
*
*/

//TODO needs to be tested
// Adds a new comment to the database
router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res: Response) => {
const {
Expand All @@ -206,26 +217,26 @@ router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res:

// Check key
if (!questionId) {
res.status(400).json('Missing questionId!');
res.status(400).json('Missing questionId');
return;
}

const { rowCount } = await db.query(`SELECT "questionId" FROM questions WHERE "questionId" = $1`, [questionId]);
if (rowCount === 0) {
res.status(404).json('Question not found!');
res.status(404).json('Question not found');
return;
}

// Check parent id
if (parentCommentId) {
const { rowCount, rows } = await db.query<Partial<IComment>>(`SELECT "commentId", "questionId" FROM comments WHERE "commentId" = $1`, [parentCommentId]);
if (rowCount === 0) {
res.status(404).json('Parent comment not found!');
res.status(404).json('Parent comment not found');
return;
}
const parentComment = rows[0];
if (parentComment.questionId !== questionId) {
res.status(400).json('Parent comment is not from the same question!');
res.status(400).json('Parent comment is not from the same question');
return;
}
}
Expand All @@ -235,7 +246,7 @@ router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res:
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`, [questionId, parentCommentId, commentText, commentPNG, isCorrect, isEndorsed, upvotes, downvotes]);

res.status(201).json('Comment Added!');
res.status(201).json('Comment Added');
});

// Adds a new question to the database
Expand All @@ -249,13 +260,13 @@ router.post('/questions', async (req: Request<any, any, QuestionBodyParams>, res

// Check key
if (!examId) {
res.status(400).json('Missing examId!');
res.status(400).json('Missing examId');
return;
}

const { rowCount } = await db.query(`SELECT "examId" FROM exams WHERE "examId" = $1`, [examId]);
if (rowCount === 0) {
res.status(404).json('Exam not found!');
res.status(404).json('ExamId not found');
return;
}

Expand All @@ -264,7 +275,7 @@ router.post('/questions', async (req: Request<any, any, QuestionBodyParams>, res
VALUES ($1, $2, $3, $4)
`, [examId, questionText, questionType, questionPNG]);

res.status(201).json('Question Added!');
res.status(201).json('Question added');
});

// Adds a new exam to the database
Expand Down Expand Up @@ -320,7 +331,7 @@ router.post('/courses', async (req: Request<any, any, CourseBodyParams>, res: Re
VALUES ($1, $2, $3)
`, [courseCode, courseName, courseDescription]);

res.status(201).json('Course Added!');
res.status(201).json('Course Added');
});

/*
Expand All @@ -331,6 +342,7 @@ router.post('/courses', async (req: Request<any, any, CourseBodyParams>, res: Re
*
*/

//TODO needs to be tested
// Gets comment by comment id
router.get('/comments/:commentId', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;
Expand All @@ -348,26 +360,49 @@ router.get('/comments/:commentId', async (req: Request<CommentRouteParams>, res:
router.get('/questions/:questionId/comments', async (req: Request<QuestionRouteParams>, res: Response) => {
const { questionId } = req.params;

// Check if the questionId exists in the database
const { rows : questionRows } = await db.query(`
SELECT "questionId"
From questions
WHERE "questionId" = $1
`, [questionId]);

if (questionRows.length === 0) {
return res.status(404).json({ error: 'Question not found' });
}

const { rows } = await db.query<IComment>(`
SELECT "commentId", "parentCommentId", "commentText", "commentPNG", "isCorrect", "isEndorsed", "upvotes", "downvotes", "created_at", "updated_at"
FROM comments
WHERE comments."questionId" = $1
SELECT "commentId", "parentCommentId", "commentText", "commentPNG", "isCorrect", "isEndorsed", "upvotes", "downvotes", "created_at", "updated_at"
FROM comments
WHERE comments."questionId" = $1
`, [questionId]);

res.status(200).json(nest(rows));
});


// Gets question information by question id
router.get('/questions/:questionId', async (req: Request<QuestionRouteParams>, res: Response) => {
const { questionId } = req.params;

// Check if the questionId exists in the database
const { rows : questionRows } = await db.query(`
SELECT "questionId"
From questions
WHERE "questionId" = $1
`, [questionId]);

if (questionRows.length === 0) {
return res.status(404).json({ error: 'Question not found' });
}

const { rows } = await db.query<Question>(`
SELECT "questionId", "questionText", "questionType", "questionPNG"
FROM questions
WHERE questions."questionId" = $1
`, [questionId]);

res.status(200).json(rows[0]);
res.status(200).json(rows[0]);
});

// Exam questions by exam ID
Expand Down Expand Up @@ -472,7 +507,7 @@ router.get('/evan', async (req: Request, res: Response) => {
router.get('/sketch', async (req: Request, res: Response) => {
const b = await db.query1('SELECT "examId" FROM exams WHERE "examId" = 1');
if (b.rows.length != 0) {
res.status(400).json('Data already exists!');
res.status(400).json('Data already exists');
return
}

Expand Down Expand Up @@ -511,7 +546,8 @@ router.get('/sketch', async (req: Request, res: Response) => {
VALUES
(1, 'Who is the best tutor at UQ?', 'Multiple Choice'),
(2, 'Who is not the best tutor at UQ?', 'Multiple Choice'),
(3, 'Who is the second best tutor at UQ?', 'Multiple Choice');
(3, 'Who is the second best tutor at UQ?', 'Multiple Choice'),
(4, 'A question with no comments', 'Multiple Choice');
INSERT INTO comments ("questionId", "parentCommentId", "commentText", "isCorrect", "isEndorsed", "upvotes", "downvotes")
VALUES
Expand Down
Loading

0 comments on commit d7d6888

Please sign in to comment.