1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Can create new Surveys

This commit is contained in:
Pierre HUBERT 2020-03-20 13:28:01 +01:00
parent 547d66e4dd
commit 167373c578
3 changed files with 80 additions and 0 deletions

View File

@ -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({

26
src/entities/NewSurvey.ts Normal file
View File

@ -0,0 +1,26 @@
/**
* New survey information handler
*
* @author Pierre HUBERT
*/
export interface NewSurveyBuilder {
postID?: number,
userID: number,
question: string,
choices: Array<string>
}
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];
}
}
}

View File

@ -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
*