Skip to content

Commit e993794

Browse files
tyouweiKurtyjlee
authored andcommitted
update CUD operations to use _id
1 parent 3fdff7e commit e993794

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

peerprep-fe/src/app/(main)/components/problems/ProblemRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function ProblemRow({ problem }: { problem: Problem }) {
2121
const handleProblemClick = async () => {
2222
// Fetch problem details here (replace with actual API call)
2323
const details: ProblemDialogData = {
24-
question_id: problem.question_id,
24+
_id: problem._id,
2525
title: problem.title,
2626
difficulty: problem.difficulty,
2727
description: problem.description,

peerprep-fe/src/app/(main)/components/problems/ProblemTable.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export default function ProblemTable({ problems }: ProblemTableProps) {
2929
</tr>
3030
</thead>
3131
<tbody>
32-
{filteredProblems.map((problem, index) => (
33-
<ProblemRow key={index} problem={problem} />
32+
{filteredProblems.map((problem) => (
33+
// should not use index as key
34+
<ProblemRow key={problem._id} problem={problem} />
3435
))}
3536
</tbody>
3637
</table>

peerprep-fe/src/types/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
interface Problem {
2-
question_id: number;
2+
_id: number;
33
title: string;
44
difficulty: number;
55
description: string;
@@ -11,7 +11,7 @@ interface Problem {
1111
}
1212

1313
interface ProblemDialogData {
14-
question_id: number;
14+
_id: number;
1515
title: string;
1616
difficulty: number;
1717
description: string;

question-service/src/app.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import cors from 'cors';
44
import * as dotenv from 'dotenv';
55
import questionsRouter from './routes/questionsController';
66
import userQuestionsRouter from './routes/userQuestionsController';
7-
import insertQuestionsRouter from './routes/insertQuestionsController';
8-
import updateQuestionsRouter from './routes/updateQuestionsController';
9-
import deleteQuestionsRouter from './routes/deleteQuestionsController';
107

118
dotenv.config({ path: '.env.dev' });
129

@@ -30,9 +27,6 @@ app.use(
3027
// different routes
3128
app.use(`${apiVersion}/questions`, questionsRouter);
3229
app.use(`${apiVersion}/userquestions`, userQuestionsRouter);
33-
// app.use(`${apiVersion}/createquestion`, insertQuestionsRouter);
34-
// app.use(`${apiVersion}/updatequestion`, updateQuestionsRouter);
35-
// app.use(`${apiVersion}/deletequestion`, deleteQuestionsRouter);
3630

3731
app.listen(PORT, () => {
3832
console.log(`Server is running on port ${PORT}`);

question-service/src/routes/questionsController.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/routes/items.ts
22
import express, { Request, Response } from 'express';
3-
import { Collection } from 'mongodb';
3+
import { Collection, ObjectId } from 'mongodb';
44
import { connectToDB } from '../db/mongoClient';
55
import { Questions } from '../models/types';
66

@@ -39,15 +39,18 @@ router.put('/:questionId', async (req: Request, res: Response) => {
3939
}
4040

4141
const result = await questionsCollection.updateOne(
42-
{ _question_id: Number(questionId) },
43-
{ $set: updatedQuestion }
42+
{ _id: new ObjectId(questionId) },
43+
{ $set: updatedQuestion },
4444
);
4545

4646
if (result.matchedCount === 0) {
4747
return res.status(404).json({ error: 'Question not found.' });
4848
}
4949

50-
res.status(200).json({ message: 'Question updated successfully' });
50+
res.status(200).json({
51+
message: 'Question updated successfully',
52+
data: [{ _id: questionId }],
53+
});
5154
} catch (error) {
5255
console.error('Error updating question:', error);
5356
res.status(500).json({ error: 'Failed to update question' });
@@ -58,11 +61,19 @@ router.put('/:questionId', async (req: Request, res: Response) => {
5861
router.post('/', async (req: Request, res: Response) => {
5962
try {
6063
const newQuestion: Questions = req.body; // Assume the body contains a question object
61-
62-
const { _question_id, difficulty, description, examples, constraints, tags, title_slug, title, pictures } = newQuestion;
64+
65+
const {
66+
difficulty,
67+
description,
68+
examples,
69+
constraints,
70+
tags,
71+
title_slug,
72+
title,
73+
pictures,
74+
} = newQuestion;
6375

6476
if (
65-
typeof _question_id !== 'number' ||
6677
typeof difficulty !== 'number' ||
6778
typeof description !== 'string' ||
6879
!Array.isArray(examples) ||
@@ -71,12 +82,17 @@ router.post('/', async (req: Request, res: Response) => {
7182
typeof title_slug !== 'string' ||
7283
typeof title !== 'string'
7384
) {
74-
return res.status(400).json({ error: 'Invalid question data. Please check your input.' });
85+
return res
86+
.status(400)
87+
.json({ error: 'Invalid question data. Please check your input.' });
7588
}
7689

7790
const result = await questionsCollection.insertOne(newQuestion);
7891

79-
res.status(201).json({ message: 'Question inserted successfully', questionId: result.insertedId });
92+
res.status(201).json({
93+
message: 'Question inserted successfully',
94+
data: [{ _id: result.insertedId }],
95+
});
8096
} catch (error) {
8197
console.error('Error inserting question:', error);
8298
res.status(500).json({ error: 'Failed to insert question' });
@@ -88,13 +104,22 @@ router.delete('/:questionId', async (req: Request, res: Response) => {
88104
const { questionId } = req.params;
89105

90106
try {
91-
const result = await questionsCollection.deleteOne({ _question_id: Number(questionId) });
107+
const result = await questionsCollection.deleteOne({
108+
_id: new ObjectId(questionId),
109+
});
92110

93111
if (result.deletedCount === 0) {
94112
return res.status(404).json({ error: 'Question not found.' });
95113
}
96114

97-
res.status(200).json({ message: 'Question deleted successfully' });
115+
res.status(200).json({
116+
message: 'Question deleted successfully',
117+
data: [
118+
{
119+
_id: questionId,
120+
},
121+
],
122+
});
98123
} catch (error) {
99124
console.error('Error deleting question:', error);
100125
res.status(500).json({ error: 'Failed to delete question' });

0 commit comments

Comments
 (0)