From 0e3bbd21259496e769d16ff43858f0854b3af280 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 16:34:20 +0100 Subject: [PATCH] Can respond to survey --- src/controllers/Routes.ts | 5 +++ src/controllers/SurveyController.ts | 31 +++++++++++++++++++ src/helpers/SurveyHelper.ts | 47 ++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index df5d480..add1e22 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -14,6 +14,7 @@ import { MoviesController } from "./MoviesController"; import { PostsController } from "./PostsController"; import { CommentsController } from "./CommentsController"; import { LikesController } from "./LikesController"; +import { SurveyController } from "./SurveyController"; /** * Controllers routes @@ -221,6 +222,10 @@ export const Routes : Route[] = [ {path: "/likes/update", cb: (h) => LikesController.Update(h)}, + // Surveys controller + {path: "/surveys/send_response", cb: (h) => SurveyController.SendResponse(h)}, + + // Notifications controller {path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)}, diff --git a/src/controllers/SurveyController.ts b/src/controllers/SurveyController.ts index b499927..26659d5 100644 --- a/src/controllers/SurveyController.ts +++ b/src/controllers/SurveyController.ts @@ -10,6 +10,25 @@ import { SurveyHelper } from "../helpers/SurveyHelper"; export class SurveyController { + /** + * Send the response to a survey to the server + * + * @param h Request handler + */ + public static async SendResponse(h: RequestHandler) { + const surveyID = await this.PostSurveyIDFromPostID(h, "postID"); + const choiceID = h.postInt("choiceID"); + + await SurveyHelper.CancelResponse(h.getUserId(), surveyID); + + if(!await SurveyHelper.ChoiceExists(surveyID, choiceID)) + h.error(404, "Choice not found for this survey!"); + + await SurveyHelper.SendResponse(h.getUserId(), surveyID, choiceID); + + h.success(); + } + /** * Turn a survey into an API entry @@ -36,6 +55,18 @@ export class SurveyController { return data; } + /** + * Get the ID of a survey from a POST id + * + * @param h Request handler + * @param field The name of the POST field containing the + * POST id + */ + private static async PostSurveyIDFromPostID(h: RequestHandler, field: string) : Promise { + const postID = await h.postPostIDWithAccess(field); + return await SurveyHelper.GetID(postID); + } + /** * Turn a survey choice into an API entry * diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index 03b2739..f109002 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -73,7 +73,7 @@ export class SurveyHelper { * * @param postID Target post ID */ - private static async GetID(postID: number) : Promise { + public static async GetID(postID: number) : Promise { const info = await DatabaseHelper.QueryRow({ table: SURVEY_INFO_TABLE, where: { @@ -88,6 +88,51 @@ export class SurveyHelper { return info.ID; } + /** + * Cancel the response of a user to a survey + * + * @param userID Target user ID + * @param surveyID Target survey + */ + public static async CancelResponse(userID: number, surveyID: number) { + await DatabaseHelper.DeleteRows(SURVEY_RESPONSE_TABLE, { + ID_sondage: surveyID, + ID_utilisateurs: userID + }); + } + + /** + * Check out whether a choice for the survey exists or not + * + * @param surveyID Target survey ID + * @param choiceID Target choice ID + */ + public static async ChoiceExists(surveyID: number, choiceID: number) : Promise { + return await DatabaseHelper.Count({ + table: SURVEY_CHOICES_TABLE, + where: { + ID_sondage: surveyID, + ID: choiceID + } + }) > 0; + } + + /** + * Save user response to a survey + * + * @param userID Target user + * @param surveyID Target survey + * @param choiceID Selected choice + */ + public static async SendResponse(userID: number, surveyID: number, choiceID: number) { + await DatabaseHelper.InsertRow(SURVEY_RESPONSE_TABLE, { + ID_utilisateurs: userID, + ID_sondage: surveyID, + ID_sondage_choix: choiceID, + date_envoi: mysql_date() + }); + } + /** * Delete the survey associated to a post *