From 03459cd3a0f07d92337779dd43f7a3b3a936282f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jan 2020 14:43:46 +0100 Subject: [PATCH] Return the choice of a user to a survey --- src/controllers/SurveyController.ts | 4 ++++ src/helpers/SurveyHelper.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index 23c2f38..b499927 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -1,5 +1,6 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { RequestHandler } from "../entities/RequestHandler"; +import { SurveyHelper } from "../helpers/SurveyHelper"; /** * Survey controller @@ -29,6 +30,9 @@ export class SurveyController { survey.choices.forEach((c) => data.choices[c.id.toString()] = this.SurveyChoiceToAPI(c)) + if(h.signedIn) + data.user_choice = await SurveyHelper.GetUserChoice(survey.id, h.getUserId()); + return data; } diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 740f856..d4a22af 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -71,6 +71,24 @@ export class SurveyHelper { return survey; } + /** + * Get the choice of a user for a survey + * + * @param surveyID Target survey + * @param userID Target user + */ + public static async GetUserChoice(surveyID: number, userID: number) { + const result = await DatabaseHelper.QueryRow({ + table: SURVEY_RESPONSE_TABLE, + where: { + ID_utilisateurs: userID, + ID_sondage: surveyID + } + }); + + return result == null ? 0 /* no response yet */ : result.ID_sondage_choix; + } + /** * Turn a database entry into a survey object