mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 21:39:22 +00:00
Can create new Surveys
This commit is contained in:
parent
547d66e4dd
commit
167373c578
@ -17,6 +17,7 @@ import { check_string_before_insert, check_youtube_id, checkURL } from "../utils
|
|||||||
import { pathUserData } from "../utils/UserDataUtils";
|
import { pathUserData } from "../utils/UserDataUtils";
|
||||||
import { statSync } from "fs";
|
import { statSync } from "fs";
|
||||||
import { lookup } from "mime-types";
|
import { lookup } from "mime-types";
|
||||||
|
import { NewSurvey } from "../entities/NewSurvey";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts controller
|
* Posts controller
|
||||||
@ -108,6 +109,9 @@ export class PostsController {
|
|||||||
// Determine the target for the new post
|
// Determine the target for the new post
|
||||||
let kindPage: PostPageKind;
|
let kindPage: PostPageKind;
|
||||||
let pageID: number;
|
let pageID: number;
|
||||||
|
|
||||||
|
let survey : NewSurvey | undefined = undefined;
|
||||||
|
|
||||||
switch(h.postString("kind-page")) {
|
switch(h.postString("kind-page")) {
|
||||||
|
|
||||||
// If the post is targetting a user
|
// If the post is targetting a user
|
||||||
@ -256,6 +260,22 @@ export class PostsController {
|
|||||||
break;
|
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:
|
default:
|
||||||
h.error(500, "Unsupported kind of post!");
|
h.error(500, "Unsupported kind of post!");
|
||||||
}
|
}
|
||||||
@ -263,6 +283,13 @@ export class PostsController {
|
|||||||
// Create the post
|
// Create the post
|
||||||
const postID = await PostsHelper.Create(newPost);
|
const postID = await PostsHelper.Create(newPost);
|
||||||
|
|
||||||
|
// Create associated survey (if any)
|
||||||
|
if(survey != undefined) {
|
||||||
|
survey.postID = postID;
|
||||||
|
SurveyHelper.Create(survey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO : create a notification
|
// TODO : create a notification
|
||||||
|
|
||||||
h.send({
|
h.send({
|
||||||
|
26
src/entities/NewSurvey.ts
Normal file
26
src/entities/NewSurvey.ts
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
import { Survey, SurveyChoice } from "../entities/Survey";
|
import { Survey, SurveyChoice } from "../entities/Survey";
|
||||||
import { DatabaseHelper } from "./DatabaseHelper";
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||||||
|
import { NewSurvey } from "../entities/NewSurvey";
|
||||||
|
import { mysql_date } from "../utils/DateUtils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Survey helper
|
* Survey helper
|
||||||
@ -27,6 +29,31 @@ const SURVEY_RESPONSE_TABLE = "sondage_reponse";
|
|||||||
*/
|
*/
|
||||||
export class SurveyHelper {
|
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
|
* Get information about the survey of a post
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user