mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 05:19:22 +00:00
Can create new survey choices
This commit is contained in:
parent
6e5f166b84
commit
3798c709f5
@ -280,6 +280,8 @@ export const Routes : Route[] = [
|
||||
|
||||
{path: "/surveys/cancel_response", cb: (h) => SurveyController.CancelResponse(h)},
|
||||
|
||||
{path: "/survey/create_new_choice", cb: (h) => SurveyController.CreateNewChoice(h)},
|
||||
|
||||
{path: "/survey/block_new_choices_creation", cb: (h) => SurveyController.BlockNewChoicesCreation(h)},
|
||||
|
||||
|
||||
|
@ -44,6 +44,27 @@ export class SurveyController {
|
||||
h.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new choice for this survey
|
||||
*
|
||||
* @param h Request handler
|
||||
*/
|
||||
public static async CreateNewChoice(h: RequestHandler) {
|
||||
|
||||
const surveyID = await this.PostSurveyIDFromPostID(h, "postID");
|
||||
const newChoice = h.postString("choice");
|
||||
|
||||
// Check if the survey allow new choices
|
||||
const survey = await SurveyHelper.GetInfoBySurveyID(surveyID);
|
||||
if(!survey.allowNewChoices)
|
||||
h.error(401, "It is not possible to create new choices for this survey!");
|
||||
|
||||
// Create the choice
|
||||
await SurveyHelper.CreateChoice(surveyID, newChoice);
|
||||
|
||||
h.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* Block the creation of new survey choices
|
||||
*
|
||||
|
@ -48,13 +48,23 @@ export class SurveyHelper {
|
||||
|
||||
// Process choices
|
||||
for(const choice of survey.choices) {
|
||||
await this.CreateChoice(surveyID, choice);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new choice for the survey
|
||||
*
|
||||
* @param surveyID The ID of the target survey
|
||||
* @param choice The value of the choice
|
||||
*/
|
||||
public static async CreateChoice(surveyID: number, choice: string) {
|
||||
await DatabaseHelper.InsertRow(SURVEY_CHOICES_TABLE, {
|
||||
ID_sondage: surveyID,
|
||||
date_creation: mysql_date(),
|
||||
Choix: choice
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a survey is associated to a post or not
|
||||
@ -191,7 +201,37 @@ export class SurveyHelper {
|
||||
if(surveyRow == null)
|
||||
throw new Error("Could not find survey for post " + postID + " !");
|
||||
|
||||
const survey = this.DBToSurveyInfo(surveyRow);
|
||||
return await this.ParseDatabaseSurveyRow(surveyRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about a survey, given its ID
|
||||
*
|
||||
* @param surveyID The ID of the survey
|
||||
*/
|
||||
public static async GetInfoBySurveyID(surveyID: number) : Promise<Survey> {
|
||||
|
||||
// Get main information about the survey
|
||||
const surveyRow = await DatabaseHelper.QueryRow({
|
||||
table: SURVEY_INFO_TABLE,
|
||||
where: {
|
||||
ID: surveyID
|
||||
}
|
||||
});
|
||||
|
||||
if(surveyRow == null)
|
||||
throw new Error("Could not find survey " + surveyID + " !");
|
||||
|
||||
return await this.ParseDatabaseSurveyRow(surveyRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a database row into a Survey object
|
||||
*
|
||||
* @param row Information about the survey
|
||||
*/
|
||||
private static async ParseDatabaseSurveyRow(row: any) : Promise<Survey> {
|
||||
const survey = this.DBToSurveyInfo(row);
|
||||
|
||||
|
||||
// Get the choices of the survey
|
||||
|
Loading…
Reference in New Issue
Block a user