|
| 1 | +import express from "express"; |
| 2 | +import { QuestionDao } from "../models/questions"; |
| 3 | +import { ApiResponse, StatusMessageType } from "../types"; |
| 4 | +import { handleCustomError, handleServerError } from "../utils"; |
| 5 | + |
| 6 | +// GET /questions/:id |
| 7 | +export const getQuestion = async ( |
| 8 | + req: express.Request, |
| 9 | + res: express.Response |
| 10 | +) => { |
| 11 | + try { |
| 12 | + const id = req.params.id; |
| 13 | + if (!id) { |
| 14 | + handleCustomError(res, { |
| 15 | + type: StatusMessageType.ERROR, |
| 16 | + message: "Question ID must be provided", |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + const question = await QuestionDao.getQuestionById(id); |
| 21 | + if (!question) { |
| 22 | + handleCustomError(res, { |
| 23 | + type: StatusMessageType.ERROR, |
| 24 | + message: "Question not found", |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + const response: ApiResponse = { |
| 29 | + payload: question, |
| 30 | + statusMessage: null, |
| 31 | + }; |
| 32 | + res.status(200).json(response); |
| 33 | + } catch (error) { |
| 34 | + handleServerError(error, res); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +// POST /questions |
| 39 | +export const createQuestion = async ( |
| 40 | + req: express.Request, |
| 41 | + res: express.Response |
| 42 | +) => { |
| 43 | + try { |
| 44 | + const { title, description, tags, difficulty } = req.body; |
| 45 | + if (!title || !description || !difficulty) { |
| 46 | + handleCustomError(res, { |
| 47 | + type: StatusMessageType.ERROR, |
| 48 | + message: "Title, description and difficulty must be provided", |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 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 | + ); |
| 65 | + const response: ApiResponse = { |
| 66 | + payload: question, |
| 67 | + statusMessage: { |
| 68 | + type: StatusMessageType.SUCCESS, |
| 69 | + message: "Question created successfully", |
| 70 | + }, |
| 71 | + }; |
| 72 | + res.status(201).json(response); |
| 73 | + } catch (error) { |
| 74 | + handleServerError(error, res); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +// PUT /questions/:id |
| 79 | +export const updateQuestion = async ( |
| 80 | + req: express.Request, |
| 81 | + res: express.Response |
| 82 | +) => { |
| 83 | + try { |
| 84 | + const id = req.params.id; |
| 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) { |
| 95 | + handleCustomError(res, { |
| 96 | + type: StatusMessageType.ERROR, |
| 97 | + message: "At least one tag must be provided", |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + const question = await QuestionDao.updateQuestion( |
| 102 | + id, |
| 103 | + title, |
| 104 | + description, |
| 105 | + tags, |
| 106 | + difficulty |
| 107 | + ); |
| 108 | + if (!question) { |
| 109 | + handleCustomError(res, { |
| 110 | + type: StatusMessageType.ERROR, |
| 111 | + message: "Question not found", |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + const response: ApiResponse = { |
| 116 | + payload: question, |
| 117 | + statusMessage: { |
| 118 | + type: StatusMessageType.SUCCESS, |
| 119 | + message: "Question updated successfully", |
| 120 | + }, |
| 121 | + }; |
| 122 | + res.status(200).json(response); |
| 123 | + } catch (error) { |
| 124 | + handleServerError(error, res); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +// DELETE /questions/:id |
| 129 | +export const deleteQuestion = async ( |
| 130 | + req: express.Request, |
| 131 | + res: express.Response |
| 132 | +) => { |
| 133 | + try { |
| 134 | + const id = req.params.id; |
| 135 | + if (!id) { |
| 136 | + handleCustomError(res, { |
| 137 | + type: StatusMessageType.ERROR, |
| 138 | + message: "Question ID must be provided", |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + const question = await QuestionDao.deleteQuestion(id); |
| 143 | + if (!question) { |
| 144 | + handleCustomError(res, { |
| 145 | + type: StatusMessageType.ERROR, |
| 146 | + message: "Question not found", |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + const response: ApiResponse = { |
| 151 | + payload: question, |
| 152 | + statusMessage: { |
| 153 | + type: StatusMessageType.SUCCESS, |
| 154 | + message: "Question deleted successfully", |
| 155 | + }, |
| 156 | + }; |
| 157 | + res.status(200).json(response); |
| 158 | + } catch (error) { |
| 159 | + handleServerError(error, res); |
| 160 | + } |
| 161 | +}; |
0 commit comments