mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Can get & return basic information about surveys
This commit is contained in:
parent
5d81421e8d
commit
24d3102d49
@ -4,6 +4,8 @@ import { PostsHelper } from "../helpers/PostsHelper";
|
|||||||
import { Post, PostVisibilityLevel, PostKind } from "../entities/Post";
|
import { Post, PostVisibilityLevel, PostKind } from "../entities/Post";
|
||||||
import { MoviesController } from "./MoviesController";
|
import { MoviesController } from "./MoviesController";
|
||||||
import { MoviesHelper } from "../helpers/MoviesHelper";
|
import { MoviesHelper } from "../helpers/MoviesHelper";
|
||||||
|
import { SurveyHelper } from "../helpers/SurveyHelper";
|
||||||
|
import { SurveyController } from "./SurveyController";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts controller
|
* Posts controller
|
||||||
@ -35,7 +37,7 @@ export class PostsController {
|
|||||||
|
|
||||||
let list = [];
|
let list = [];
|
||||||
for (const p of posts) {
|
for (const p of posts) {
|
||||||
list.push(await this.PostToAPI(p));
|
list.push(await this.PostToAPI(h, p));
|
||||||
}
|
}
|
||||||
|
|
||||||
h.send(list);
|
h.send(list);
|
||||||
@ -45,9 +47,10 @@ export class PostsController {
|
|||||||
/**
|
/**
|
||||||
* Turn a post object into an API entry
|
* Turn a post object into an API entry
|
||||||
*
|
*
|
||||||
|
* @param h Request handler
|
||||||
* @param p The post
|
* @param p The post
|
||||||
*/
|
*/
|
||||||
public static async PostToAPI(p: Post) : Promise<any> {
|
public static async PostToAPI(h: RequestHandler, p: Post) : Promise<any> {
|
||||||
let data : any = {
|
let data : any = {
|
||||||
ID: p.id,
|
ID: p.id,
|
||||||
userID: p.userID,
|
userID: p.userID,
|
||||||
@ -79,7 +82,11 @@ export class PostsController {
|
|||||||
link_url: !p.hasLink ? null : p.link.url,
|
link_url: !p.hasLink ? null : p.link.url,
|
||||||
link_title: !p.hasLink ? null : p.link.title,
|
link_title: !p.hasLink ? null : p.link.title,
|
||||||
link_description: !p.hasLink ? null : p.link.description,
|
link_description: !p.hasLink ? null : p.link.description,
|
||||||
link_image: !p.hasLink ? null : p.link.image
|
link_image: !p.hasLink ? null : p.link.image,
|
||||||
|
|
||||||
|
|
||||||
|
// Survey specific
|
||||||
|
data_survey: !p.hasSurvey ? null : await SurveyController.SurveyToAPI(h, await SurveyHelper.GetInfo(p.id)),
|
||||||
};
|
};
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
47
src/controllers/SurveyController.ts
Normal file
47
src/controllers/SurveyController.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { Survey, SurveyChoice } from "../entities/Survey";
|
||||||
|
import { RequestHandler } from "../entities/RequestHandler";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Survey controller
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export class SurveyController {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a survey into an API entry
|
||||||
|
*
|
||||||
|
* @param h Request handler
|
||||||
|
* @param survey The survey
|
||||||
|
*/
|
||||||
|
public static async SurveyToAPI(h: RequestHandler, survey: Survey) : Promise<any> {
|
||||||
|
let data = {
|
||||||
|
ID: survey.id,
|
||||||
|
userID: survey.userID,
|
||||||
|
postID: survey.postID,
|
||||||
|
creation_time: survey.timeCreate,
|
||||||
|
question: survey.question,
|
||||||
|
user_choice: -1,
|
||||||
|
choices: {},
|
||||||
|
}
|
||||||
|
|
||||||
|
survey.choices.forEach((c) => data.choices[c.id.toString()] = this.SurveyChoiceToAPI(c))
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a survey choice into an API entry
|
||||||
|
*
|
||||||
|
* @param c The choice
|
||||||
|
*/
|
||||||
|
private static SurveyChoiceToAPI(c: SurveyChoice) {
|
||||||
|
return {
|
||||||
|
choiceID: c.id,
|
||||||
|
name: c.name,
|
||||||
|
responses: c.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -160,4 +160,8 @@ export class Post implements PostBuilder {
|
|||||||
get hasLink() : boolean {
|
get hasLink() : boolean {
|
||||||
return this.kind == PostKind.POST_KIND_WEBLINK;
|
return this.kind == PostKind.POST_KIND_WEBLINK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get hasSurvey() : boolean {
|
||||||
|
return this.kind == PostKind.POST_KIND_SURVEY;
|
||||||
|
}
|
||||||
}
|
}
|
35
src/entities/Survey.ts
Normal file
35
src/entities/Survey.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Survey information
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface SurveyChoice {
|
||||||
|
id: number,
|
||||||
|
name: string,
|
||||||
|
count: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SurveyBuilder {
|
||||||
|
id: number;
|
||||||
|
userID: number;
|
||||||
|
timeCreate: number;
|
||||||
|
postID: number;
|
||||||
|
question: string;
|
||||||
|
choices: SurveyChoice[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Survey implements SurveyBuilder {
|
||||||
|
id: number; userID: number;
|
||||||
|
timeCreate: number;
|
||||||
|
postID: number;
|
||||||
|
question: string;
|
||||||
|
choices: SurveyChoice[];
|
||||||
|
|
||||||
|
public constructor(info: SurveyBuilder) {
|
||||||
|
for (const key in info) {
|
||||||
|
if (info.hasOwnProperty(key))
|
||||||
|
this[key] = info[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ export interface QueryInformation {
|
|||||||
where ?: Object,
|
where ?: Object,
|
||||||
customWhere ?: string,
|
customWhere ?: string,
|
||||||
customWhereArgs ?: Array<string>,
|
customWhereArgs ?: Array<string>,
|
||||||
|
groupBy ?: string,
|
||||||
order ?: string,
|
order ?: string,
|
||||||
limit ?: number,
|
limit ?: number,
|
||||||
}
|
}
|
||||||
@ -137,6 +138,11 @@ export class DatabaseHelper {
|
|||||||
info.customWhereArgs.forEach((e) => args.push(e));
|
info.customWhereArgs.forEach((e) => args.push(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Group by clause (if any)
|
||||||
|
if(info.groupBy) {
|
||||||
|
request += " GROUP BY " + info.groupBy + " ";
|
||||||
|
}
|
||||||
|
|
||||||
// Order (if any)
|
// Order (if any)
|
||||||
if(info.order)
|
if(info.order)
|
||||||
request += " ORDER BY " + 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user