diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 270ce97..2763d3a 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -30,4 +30,5 @@ pub mod res_change_group_logo; pub mod group_member_api; pub mod friend_api; pub mod friendship_status_api; -pub mod post_api; \ No newline at end of file +pub mod post_api; +pub mod movie_api; \ No newline at end of file diff --git a/src/api_data/movie_api.rs b/src/api_data/movie_api.rs new file mode 100644 index 0000000..4a9c544 --- /dev/null +++ b/src/api_data/movie_api.rs @@ -0,0 +1,34 @@ +//! # Movie API information +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::movie::Movie; +use crate::utils::user_data_utils::user_data_url; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct MovieAPI { + id: u64, + uri: String, + url: String, + userID: u64, + name: String, + file_type: String, + size: usize, +} + +impl MovieAPI { + /// Construct a new instance of movie + pub fn new(m: &Movie) -> MovieAPI { + MovieAPI { + id: m.id, + uri: m.uri.clone(), + url: user_data_url(m.uri.as_str()), + userID: m.user_id.id(), + name: m.name.clone(), + file_type: m.file_type.clone(), + size: m.size, + } + } +} \ No newline at end of file diff --git a/src/api_data/post_api.rs b/src/api_data/post_api.rs index 71bafad..9f7c353 100644 --- a/src/api_data/post_api.rs +++ b/src/api_data/post_api.rs @@ -3,8 +3,11 @@ //! @author Pierre Hubert use serde::Serialize; +use crate::api_data::movie_api::MovieAPI; +use crate::data::error::ResultBoxError; use crate::data::post::{Post, PostKind}; use crate::data::user::UserID; +use crate::helpers::movies_helper; use crate::utils::user_data_utils::user_data_url; #[derive(Serialize)] @@ -30,11 +33,15 @@ pub struct PostAPI { link_title: Option, link_description: Option, link_image: Option, + + // Movie specific + video_id: Option, + video_info: Option, } impl PostAPI { /// Turn a `Post` entry into an API entry - pub fn new(p: &Post) -> PostAPI { + pub fn new(p: &Post) -> ResultBoxError { let mut post = PostAPI { ID: p.id, userID: p.user_id.id(), @@ -56,6 +63,10 @@ impl PostAPI { link_title: None, link_description: None, link_image: None, + + // Movie specific + video_id: None, + video_info: None, }; match &p.kind { @@ -75,17 +86,21 @@ impl PostAPI { post.link_image = link.image.clone(); } - PostKind::POST_KIND_MOVIE => {} + PostKind::POST_KIND_MOVIE(movie_id) => { + post.video_id = Some(*movie_id); + post.video_info = Some(MovieAPI::new(&movies_helper::get_info(*movie_id)?)) + } + PostKind::POST_KIND_COUNTDOWN => {} PostKind::POST_KIND_SURVEY => {} PostKind::POST_KIND_YOUTUBE => {} } - post + Ok(post) } /// Turn a list of posts into an API entry - pub fn for_list(l: &Vec) -> Vec { + pub fn for_list(l: &Vec) -> ResultBoxError> { l.iter().map(Self::new).collect() } } \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index a9a6675..891fe9a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -37,6 +37,9 @@ pub mod database_tables_names { /// Posts table pub const POSTS_TABLE: &str = "texte"; + + /// Movies table + pub const MOVIES_TABLE: &str = "galerie_video"; } /// The account image to show for user who do not have any diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 5e891e6..d0d41eb 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -20,5 +20,5 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult { .set_start_from(start_from) .get_user(&user_id)?; - r.set_response(PostAPI::for_list(&posts)) + r.set_response(PostAPI::for_list(&posts)?) } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index baec52d..c806716 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -19,4 +19,5 @@ pub mod group_member; pub mod global_search_result; pub mod friend; pub mod friendship_status; -pub mod post; \ No newline at end of file +pub mod post; +pub mod movie; \ No newline at end of file diff --git a/src/data/movie.rs b/src/data/movie.rs new file mode 100644 index 0000000..14b7074 --- /dev/null +++ b/src/data/movie.rs @@ -0,0 +1,14 @@ +//! # Movie information +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct Movie { + pub id: u64, + pub user_id: UserID, + pub name: String, + pub uri: String, + pub file_type: String, + pub size: usize, +} \ No newline at end of file diff --git a/src/data/post.rs b/src/data/post.rs index 9f04699..5595f17 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -56,7 +56,7 @@ pub enum PostKind { POST_KIND_IMAGE(PostFile), POST_KIND_WEBLINK(PostWebLink), POST_KIND_PDF(PostFile), - POST_KIND_MOVIE, + POST_KIND_MOVIE(u64), // The ID of the movie POST_KIND_COUNTDOWN, POST_KIND_SURVEY, POST_KIND_YOUTUBE, @@ -69,7 +69,7 @@ impl PostKind { PostKind::POST_KIND_IMAGE(_) => "image", PostKind::POST_KIND_WEBLINK(_) => "weblink", PostKind::POST_KIND_PDF(_) => "pdf", - PostKind::POST_KIND_MOVIE => "movie", + PostKind::POST_KIND_MOVIE(_) => "movie", PostKind::POST_KIND_COUNTDOWN => "countdown", PostKind::POST_KIND_SURVEY => "survey", PostKind::POST_KIND_YOUTUBE => "youtube", diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 09cb44a..73ec7c7 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -10,4 +10,5 @@ pub mod likes_helper; pub mod groups_helper; pub mod posts_helper; pub mod conversations_helper; -pub mod virtual_directory_helper; \ No newline at end of file +pub mod virtual_directory_helper; +pub mod movies_helper; \ No newline at end of file diff --git a/src/helpers/movies_helper.rs b/src/helpers/movies_helper.rs new file mode 100644 index 0000000..61fd020 --- /dev/null +++ b/src/helpers/movies_helper.rs @@ -0,0 +1,27 @@ +//! # Movies helper +//! +//! @author Pierre Hubert + +use crate::constants::database_tables_names::MOVIES_TABLE; +use crate::data::error::ResultBoxError; +use crate::data::movie::Movie; +use crate::helpers::database; + +/// Get information about a single movie +pub fn get_info(movie_id: u64) -> ResultBoxError { + database::QueryInfo::new(MOVIES_TABLE) + .cond_u64("ID", movie_id) + .query_row(db_to_movie) +} + +/// Turn a database entry into a movie object +fn db_to_movie(row: &database::RowResult) -> ResultBoxError { + Ok(Movie { + id: row.get_u64("ID")?, + user_id: row.get_user_id("ID_user")?, + name: row.get_str("nom_video")?, + uri: row.get_str("URL")?, + file_type: row.get_str("file_type")?, + size: row.get_usize("size")?, + }) +} \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index c611a34..88efab3 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -5,7 +5,7 @@ use crate::constants::database_tables_names::POSTS_TABLE; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; -use crate::data::post::PostKind::{POST_KIND_IMAGE, POST_KIND_PDF, POST_KIND_WEBLINK}; +use crate::data::post::PostKind::{POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_WEBLINK}; use crate::data::user::UserID; use crate::helpers::{database, friends_helper}; use crate::utils::date_utils::time; @@ -170,6 +170,8 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError { "pdf" => post.kind = POST_KIND_PDF(file?), + "video" => post.kind = POST_KIND_MOVIE(res.get_u64("idvideo")?), + _ => {} }