mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Add survey posts support
This commit is contained in:
@ -31,4 +31,6 @@ pub mod group_member_api;
|
||||
pub mod friend_api;
|
||||
pub mod friendship_status_api;
|
||||
pub mod post_api;
|
||||
pub mod movie_api;
|
||||
pub mod movie_api;
|
||||
pub mod survey_choice_api;
|
||||
pub mod survey_api;
|
@ -4,10 +4,11 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::api_data::movie_api::MovieAPI;
|
||||
use crate::api_data::survey_api::SurveyAPI;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::post::{Post, PostKind};
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::movies_helper;
|
||||
use crate::helpers::{movies_helper, survey_helper};
|
||||
use crate::utils::user_data_utils::user_data_url;
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -40,11 +41,14 @@ pub struct PostAPI {
|
||||
|
||||
// Countdown timer specific
|
||||
time_end: Option<u64>,
|
||||
|
||||
// Survey specific
|
||||
data_survey: Option<SurveyAPI>,
|
||||
}
|
||||
|
||||
impl PostAPI {
|
||||
/// Turn a `Post` entry into an API entry
|
||||
pub fn new(p: &Post) -> ResultBoxError<PostAPI> {
|
||||
pub fn new(p: &Post, user: &Option<UserID>) -> ResultBoxError<PostAPI> {
|
||||
let mut post = PostAPI {
|
||||
ID: p.id,
|
||||
userID: p.user_id.id(),
|
||||
@ -73,6 +77,9 @@ impl PostAPI {
|
||||
|
||||
// Countdown timer-specific
|
||||
time_end: None,
|
||||
|
||||
// Survey specific
|
||||
data_survey: None,
|
||||
};
|
||||
|
||||
match &p.kind {
|
||||
@ -99,7 +106,9 @@ impl PostAPI {
|
||||
|
||||
PostKind::POST_KIND_COUNTDOWN(time_end) => post.time_end = Some(*time_end),
|
||||
|
||||
PostKind::POST_KIND_SURVEY => {}
|
||||
PostKind::POST_KIND_SURVEY =>
|
||||
post.data_survey = Some(SurveyAPI::new(&survey_helper::get_info(p.id)?, user.clone())?),
|
||||
|
||||
PostKind::POST_KIND_YOUTUBE => {}
|
||||
}
|
||||
|
||||
@ -107,7 +116,7 @@ impl PostAPI {
|
||||
}
|
||||
|
||||
/// Turn a list of posts into an API entry
|
||||
pub fn for_list(l: &Vec<Post>) -> ResultBoxError<Vec<PostAPI>> {
|
||||
l.iter().map(Self::new).collect()
|
||||
pub fn for_list(l: &Vec<Post>, user_id: Option<UserID>) -> ResultBoxError<Vec<PostAPI>> {
|
||||
l.iter().map(|p| Self::new(p, &user_id)).collect()
|
||||
}
|
||||
}
|
47
src/api_data/survey_api.rs
Normal file
47
src/api_data/survey_api.rs
Normal file
@ -0,0 +1,47 @@
|
||||
//! # Survey API information
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::api_data::survey_choice_api::SurveyChoiceAPI;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::survey::Survey;
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::survey_helper;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct SurveyAPI {
|
||||
ID: u64,
|
||||
userID: u64,
|
||||
postID: u64,
|
||||
creation_time: u64,
|
||||
question: String,
|
||||
user_choice: u64,
|
||||
choices: HashMap<u64, SurveyChoiceAPI>,
|
||||
allowNewChoices: bool,
|
||||
}
|
||||
|
||||
impl SurveyAPI {
|
||||
/// Create a new survey API entry
|
||||
pub fn new(s: &Survey, curr_user_id: Option<UserID>) -> ResultBoxError<SurveyAPI> {
|
||||
let user_choice = match &curr_user_id {
|
||||
None => 0, /* -1 is not possible */
|
||||
Some(user_id) => survey_helper::get_user_choice(s.id, user_id)?,
|
||||
};
|
||||
|
||||
Ok(SurveyAPI {
|
||||
ID: s.id,
|
||||
userID: s.user_id.id(),
|
||||
postID: s.post_id,
|
||||
creation_time: s.time_create,
|
||||
question: s.question.clone(),
|
||||
user_choice,
|
||||
choices: SurveyChoiceAPI::for_list(&s.choices),
|
||||
allowNewChoices: s.allow_new_choices,
|
||||
})
|
||||
}
|
||||
}
|
33
src/api_data/survey_choice_api.rs
Normal file
33
src/api_data/survey_choice_api.rs
Normal file
@ -0,0 +1,33 @@
|
||||
//! # Survey choice API
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::data::survey::SurveyChoice;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct SurveyChoiceAPI {
|
||||
choiceID: u64,
|
||||
name: String,
|
||||
responses: u64,
|
||||
}
|
||||
|
||||
impl SurveyChoiceAPI {
|
||||
pub fn new(c: &SurveyChoice) -> SurveyChoiceAPI {
|
||||
SurveyChoiceAPI {
|
||||
choiceID: c.id,
|
||||
name: c.name.clone(),
|
||||
responses: c.count,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_list(c: &Vec<SurveyChoice>) -> HashMap<u64, SurveyChoiceAPI> {
|
||||
let mut map = HashMap::with_capacity(c.len());
|
||||
c.iter().for_each(|c| { map.insert(c.id, Self::new(c)); });
|
||||
map
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user