From 167373c578044da09595dcf146fe58cace95fdb2 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 20 Mar 2020 13:28:01 +0100 Subject: [PATCH] Can create new Surveys --- src/controllers/PostsController.ts | 27 +++++++++++++++++++++++++++ src/entities/NewSurvey.ts | 26 ++++++++++++++++++++++++++ src/helpers/SurveyHelper.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/entities/NewSurvey.ts diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index aea8253..38be7f9 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -17,6 +17,7 @@ import { check_string_before_insert, check_youtube_id, checkURL } from "../utils import { pathUserData } from "../utils/UserDataUtils"; import { statSync } from "fs"; import { lookup } from "mime-types"; +import { NewSurvey } from "../entities/NewSurvey"; /** * Posts controller @@ -108,6 +109,9 @@ export class PostsController { // Determine the target for the new post let kindPage: PostPageKind; let pageID: number; + + let survey : NewSurvey | undefined = undefined; + switch(h.postString("kind-page")) { // If the post is targetting a user @@ -255,6 +259,22 @@ export class PostsController { newPost.timeEnd = h.postInt("time-end"); break; + + // Survey controller + case PostKind.POST_KIND_SURVEY: + + // Create the survey + survey = new NewSurvey({ + question: h.postString("question"), + userID: h.getUserId(), + choices: h.postString("answers").split("<>") + }) + + if(survey.choices.length < 2) + h.error(401, "Survey must have at least two answers!"); + + break; + default: h.error(500, "Unsupported kind of post!"); @@ -263,6 +283,13 @@ export class PostsController { // Create the post const postID = await PostsHelper.Create(newPost); + // Create associated survey (if any) + if(survey != undefined) { + survey.postID = postID; + SurveyHelper.Create(survey); + } + + // TODO : create a notification h.send({ diff --git a/src/entities/NewSurvey.ts b/src/entities/NewSurvey.ts new file mode 100644 index 0000000..c8e840e --- /dev/null +++ b/src/entities/NewSurvey.ts @@ -0,0 +1,26 @@ +/** + * New survey information handler + * + * @author Pierre HUBERT + */ + +export interface NewSurveyBuilder { + postID?: number, + userID: number, + question: string, + choices: Array +} + +export class NewSurvey implements NewSurveyBuilder { + postID: number; + userID: number; + question: string; + choices: string[]; + + public constructor(info: NewSurveyBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } +} \ No newline at end of file diff --git a/src/helpers/SurveyHelper.ts b/src/helpers/SurveyHelper.ts index d4a22af..966783f 100644 --- a/src/helpers/SurveyHelper.ts +++ b/src/helpers/SurveyHelper.ts @@ -1,5 +1,7 @@ import { Survey, SurveyChoice } from "../entities/Survey"; import { DatabaseHelper } from "./DatabaseHelper"; +import { NewSurvey } from "../entities/NewSurvey"; +import { mysql_date } from "../utils/DateUtils"; /** * Survey helper @@ -27,6 +29,31 @@ const SURVEY_RESPONSE_TABLE = "sondage_reponse"; */ export class SurveyHelper { + /** + * Create a new survey + * + * @param survey Information about the survey to create + */ + public static async Create(survey: NewSurvey) { + + // Insert main row + const surveyID = await DatabaseHelper.InsertRow(SURVEY_INFO_TABLE, { + ID_utilisateurs: survey.userID, + ID_texte: survey.postID, + date_creation: mysql_date(), + question: survey.question + }) + + // Process choices + for(const choice of survey.choices) { + await DatabaseHelper.InsertRow(SURVEY_CHOICES_TABLE, { + ID_sondage: surveyID, + date_creation: mysql_date(), + Choix: choice + }); + } + } + /** * Get information about the survey of a post *