Skip to content

Commit 7e32c2c

Browse files
Update Question model
1 parent dc16a2c commit 7e32c2c

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

questions/src/controllers/questions.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
import { QuestionDao } from "../models/questions";
3-
import { ApiResponse, EMPTY_OBJECT, StatusMessageType } from "../types";
3+
import { ApiResponse, StatusMessageType } from "../types";
44
import { handleCustomError, handleServerError } from "../utils";
55

66
// GET /questions/:id
@@ -41,15 +41,27 @@ export const createQuestion = async (
4141
res: express.Response
4242
) => {
4343
try {
44-
const { title, description } = req.body;
45-
if (!title || !description) {
44+
const { title, description, tags, difficulty } = req.body;
45+
if (!title || !description || !difficulty) {
4646
handleCustomError(res, {
4747
type: StatusMessageType.ERROR,
48-
message: "Title and description must be provided",
48+
message: "Title, description and difficulty must be provided",
4949
});
5050
}
5151

52-
const question = await QuestionDao.createQuestion(title, description);
52+
if (!tags || tags.length === 0) {
53+
handleCustomError(res, {
54+
type: StatusMessageType.ERROR,
55+
message: "At least one tag must be provided",
56+
});
57+
}
58+
59+
const question = await QuestionDao.createQuestion(
60+
title,
61+
description,
62+
tags,
63+
difficulty
64+
);
5365
const response: ApiResponse = {
5466
payload: question,
5567
statusMessage: {
@@ -70,15 +82,29 @@ export const updateQuestion = async (
7082
) => {
7183
try {
7284
const id = req.params.id;
73-
const { title, description } = req.body;
74-
if (!id || !title || !description) {
85+
const { title, description, tags, difficulty } = req.body;
86+
if (!id || !title || !description || !difficulty) {
87+
handleCustomError(res, {
88+
type: StatusMessageType.ERROR,
89+
message:
90+
"Question ID, title, description and difficulty must be provided",
91+
});
92+
}
93+
94+
if (!tags || tags.length === 0) {
7595
handleCustomError(res, {
7696
type: StatusMessageType.ERROR,
77-
message: "Question ID, title, and description must be provided",
97+
message: "At least one tag must be provided",
7898
});
7999
}
80100

81-
const question = await QuestionDao.updateQuestion(id, title, description);
101+
const question = await QuestionDao.updateQuestion(
102+
id,
103+
title,
104+
description,
105+
tags,
106+
difficulty
107+
);
82108
if (!question) {
83109
handleCustomError(res, {
84110
type: StatusMessageType.ERROR,

questions/src/models/questions.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,66 @@
11
import mongoose from "mongoose";
22

33
const QuestionSchema = new mongoose.Schema({
4-
title: { type: String, required: true },
5-
description: { type: String, required: true },
4+
title: {
5+
type: String,
6+
required: true,
7+
validate: {
8+
validator: (title: string) => title.length > 0,
9+
message: "Title is required",
10+
},
11+
},
12+
difficulty: {
13+
type: String,
14+
enum: ["Easy", "Medium", "Hard"],
15+
required: true,
16+
ValidityState: {
17+
values: ["Easy", "Medium", "Hard"],
18+
message: "{VALUE} is not supported",
19+
},
20+
},
21+
tags: {
22+
type: Array,
23+
required: true,
24+
validate: {
25+
validator: (tags: [String]) => tags.length > 0,
26+
message: "At least one tag is required",
27+
},
28+
},
29+
description: {
30+
type: String,
31+
required: true,
32+
validate: {
33+
validator: (description: string) => description.length > 0,
34+
message: "Description is required",
35+
},
36+
},
637
});
738

8-
const QuestionModel = mongoose.model("Question", QuestionSchema);
39+
export const QuestionModel = mongoose.model("Question", QuestionSchema);
940

1041
const getQuestionById = async (id: string) =>
1142
QuestionModel.findById(id).then((question) => question?.toObject());
1243

13-
const createQuestion = async (title: string, description: string) => {
14-
const question = new QuestionModel({ title, description });
44+
const createQuestion = async (
45+
title: string,
46+
description: string,
47+
tags: [String],
48+
difficulty: string
49+
) => {
50+
const question = new QuestionModel({ title, description, tags, difficulty });
1551
return question.save().then((question) => question.toObject());
1652
};
1753

1854
const updateQuestion = async (
1955
id: string,
2056
title: string,
21-
description: string
57+
description: string,
58+
tags: [String],
59+
difficulty: string
2260
) => {
2361
const updatedQuestion = await QuestionModel.findByIdAndUpdate(
2462
id,
25-
{ title, description },
63+
{ title, description, tags, difficulty },
2664
{ new: true }
2765
);
2866
return updatedQuestion?.toObject();

questions/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const handleServerError = (error: Error, res: express.Response) => {
2525
payload: EMPTY_OBJECT,
2626
statusMessage: {
2727
type: StatusMessageType.ERROR,
28-
message: "Something went wrong! Please try again later.",
28+
message: error.message,
2929
},
3030
};
3131

0 commit comments

Comments
 (0)