11import express from "express" ;
22import { QuestionDao } from "../models/questions" ;
3- import { ApiResponse , EMPTY_OBJECT , StatusMessageType } from "../types" ;
3+ import { ApiResponse , StatusMessageType } from "../types" ;
44import { 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 ,
0 commit comments