diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 62e256b..bc2dac3 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -280,6 +280,8 @@ export const Routes : Route[] = [ {path: "/surveys/cancel_response", cb: (h) => SurveyController.CancelResponse(h)}, + {path: "/survey/create_new_choice", cb: (h) => SurveyController.CreateNewChoice(h)}, + {path: "/survey/block_new_choices_creation", cb: (h) => SurveyController.BlockNewChoicesCreation(h)}, diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index 08f8c89..848ed06 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -44,6 +44,27 @@ export class SurveyController { h.success(); } + /** + * Create a new choice for this survey + * + * @param h Request handler + */ + public static async CreateNewChoice(h: RequestHandler) { + + const surveyID = await this.PostSurveyIDFromPostID(h, "postID"); + const newChoice = h.postString("choice"); + + // Check if the survey allow new choices + const survey = await SurveyHelper.GetInfoBySurveyID(surveyID); + if(!survey.allowNewChoices) + h.error(401, "It is not possible to create new choices for this survey!"); + + // Create the choice + await SurveyHelper.CreateChoice(surveyID, newChoice); + + h.success(); + } + /** * Block the creation of new survey choices * diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index e0950ee..6833839 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -48,14 +48,24 @@ export class SurveyHelper { // Process choices for(const choice of survey.choices) { - await DatabaseHelper.InsertRow(SURVEY_CHOICES_TABLE, { - ID_sondage: surveyID, - date_creation: mysql_date(), - Choix: choice - }); + await this.CreateChoice(surveyID, choice); } } + /** + * Insert a new choice for the survey + * + * @param surveyID The ID of the target survey + * @param choice The value of the choice + */ + public static async CreateChoice(surveyID: number, choice: string) { + await DatabaseHelper.InsertRow(SURVEY_CHOICES_TABLE, { + ID_sondage: surveyID, + date_creation: mysql_date(), + Choix: choice + }); + } + /** * Check out whether a survey is associated to a post or not * @@ -190,8 +200,38 @@ export class SurveyHelper { if(surveyRow == null) throw new Error("Could not find survey for post " + postID + " !"); - - const survey = this.DBToSurveyInfo(surveyRow); + + return await this.ParseDatabaseSurveyRow(surveyRow); + } + + /** + * Get information about a survey, given its ID + * + * @param surveyID The ID of the survey + */ + public static async GetInfoBySurveyID(surveyID: number) : Promise { + + // Get main information about the survey + const surveyRow = await DatabaseHelper.QueryRow({ + table: SURVEY_INFO_TABLE, + where: { + ID: surveyID + } + }); + + if(surveyRow == null) + throw new Error("Could not find survey " + surveyID + " !"); + + return await this.ParseDatabaseSurveyRow(surveyRow); + } + + /** + * Turn a database row into a Survey object + * + * @param row Information about the survey + */ + private static async ParseDatabaseSurveyRow(row: any) : Promise { + const survey = this.DBToSurveyInfo(row); // Get the choices of the survey