mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 16:45:16 +00:00
Can get & return basic information about surveys
This commit is contained in:
@ -4,6 +4,8 @@ import { PostsHelper } from "../helpers/PostsHelper";
|
||||
import { Post, PostVisibilityLevel, PostKind } from "../entities/Post";
|
||||
import { MoviesController } from "./MoviesController";
|
||||
import { MoviesHelper } from "../helpers/MoviesHelper";
|
||||
import { SurveyHelper } from "../helpers/SurveyHelper";
|
||||
import { SurveyController } from "./SurveyController";
|
||||
|
||||
/**
|
||||
* Posts controller
|
||||
@ -35,7 +37,7 @@ export class PostsController {
|
||||
|
||||
let list = [];
|
||||
for (const p of posts) {
|
||||
list.push(await this.PostToAPI(p));
|
||||
list.push(await this.PostToAPI(h, p));
|
||||
}
|
||||
|
||||
h.send(list);
|
||||
@ -45,9 +47,10 @@ export class PostsController {
|
||||
/**
|
||||
* Turn a post object into an API entry
|
||||
*
|
||||
* @param h Request handler
|
||||
* @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 = {
|
||||
ID: p.id,
|
||||
userID: p.userID,
|
||||
@ -79,7 +82,11 @@ export class PostsController {
|
||||
link_url: !p.hasLink ? null : p.link.url,
|
||||
link_title: !p.hasLink ? null : p.link.title,
|
||||
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;
|
||||
|
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user