mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-09-22 23:28:48 +00:00
Can get & return basic information about surveys
This commit is contained in:
src
controllers
entities
helpers
@@ -21,6 +21,7 @@ export interface QueryInformation {
|
||||
where ?: Object,
|
||||
customWhere ?: string,
|
||||
customWhereArgs ?: Array<string>,
|
||||
groupBy ?: string,
|
||||
order ?: string,
|
||||
limit ?: number,
|
||||
}
|
||||
@@ -137,6 +138,11 @@ export class DatabaseHelper {
|
||||
info.customWhereArgs.forEach((e) => args.push(e));
|
||||
}
|
||||
|
||||
// Group by clause (if any)
|
||||
if(info.groupBy) {
|
||||
request += " GROUP BY " + info.groupBy + " ";
|
||||
}
|
||||
|
||||
// Order (if any)
|
||||
if(info.order)
|
||||
request += " ORDER BY " + info.order + " ";
|
||||
|
103
src/helpers/SurveyHelper.ts
Normal file
103
src/helpers/SurveyHelper.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { Survey, SurveyChoice } from "../entities/Survey";
|
||||
import { DatabaseHelper } from "./DatabaseHelper";
|
||||
|
||||
/**
|
||||
* Survey helper
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
/**
|
||||
* Survey info table
|
||||
*/
|
||||
const SURVEY_INFO_TABLE = "sondage";
|
||||
|
||||
/**
|
||||
* Survey choices table
|
||||
*/
|
||||
const SURVEY_CHOICES_TABLE = "sondage_choix";
|
||||
|
||||
/**
|
||||
* Survey responses table
|
||||
*/
|
||||
const SURVEY_RESPONSE_TABLE = "sondage_reponse";
|
||||
|
||||
/**
|
||||
* Survey helper
|
||||
*/
|
||||
export class SurveyHelper {
|
||||
|
||||
/**
|
||||
* Get information about the survey of a post
|
||||
*
|
||||
* @param postID The ID of the associated post
|
||||
*/
|
||||
public static async GetInfo(postID: number) : Promise<Survey> {
|
||||
|
||||
// Get main information about the survey
|
||||
const surveyRow = await DatabaseHelper.QueryRow({
|
||||
table: SURVEY_INFO_TABLE,
|
||||
where: {
|
||||
ID_texte: postID
|
||||
}
|
||||
});
|
||||
|
||||
if(surveyRow == null)
|
||||
throw new Error("Could not find survey for post " + postID + " !");
|
||||
|
||||
const survey = this.DBToSurveyInfo(surveyRow);
|
||||
|
||||
|
||||
// Get the choices of the survey
|
||||
const choices = await DatabaseHelper.Query({
|
||||
table: SURVEY_CHOICES_TABLE,
|
||||
tableAlias: "c",
|
||||
joins: [
|
||||
{
|
||||
table: SURVEY_RESPONSE_TABLE,
|
||||
tableAlias: "r",
|
||||
condition: "c.ID = r.ID_sondage_choix"
|
||||
}
|
||||
],
|
||||
where: {
|
||||
"c.ID_sondage": survey.id
|
||||
},
|
||||
groupBy: "c.ID",
|
||||
fields: ["c.*", "COUNT(*) AS count_choice"]
|
||||
});
|
||||
|
||||
choices.forEach((row) => survey.choices.push(this.DBToSurveyChoice(row)));
|
||||
|
||||
return survey;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Turn a database entry into a survey object
|
||||
*
|
||||
* @param row The row to transform
|
||||
*/
|
||||
private static DBToSurveyInfo(row: any) : Survey {
|
||||
return new Survey({
|
||||
id: row.ID,
|
||||
userID: row.ID_utilisateurs,
|
||||
postID: row.ID_texte,
|
||||
timeCreate: new Date(row.date_creation).getTime()/1000,
|
||||
question: row.question,
|
||||
choices: []
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a database entry into a survey choice
|
||||
*
|
||||
* @param row The row to transform
|
||||
*/
|
||||
private static DBToSurveyChoice(row: any) : SurveyChoice {
|
||||
return {
|
||||
id: row.ID,
|
||||
name: row.Choix,
|
||||
count: row.count_choice
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user