1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-07 17:57:04 +00:00
comunicapiv3/src/helpers/movies_helper.rs

41 lines
1.3 KiB
Rust

//! # 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::data::user::UserID;
use crate::helpers::database;
/// Get the list of movies of the current
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<Movie>> {
database::QueryInfo::new(MOVIES_TABLE)
.cond_user_id("ID_user", user_id)
.set_order("ID DESC")
.exec(db_to_movie)
}
/// Get information about a single movie
pub fn get_info(movie_id: u64) -> ResultBoxError<Movie> {
database::QueryInfo::new(MOVIES_TABLE)
.cond_u64("ID", movie_id)
.query_row(db_to_movie)
}
/// Check out whether a user own a movie or not
pub fn does_user_has(user_id: &UserID, movie_id: u64) -> ResultBoxError<bool> {
Ok(get_info(movie_id).map(|m| &m.user_id == user_id).unwrap_or(false))
}
/// Turn a database entry into a movie object
fn db_to_movie(row: &database::RowResult) -> ResultBoxError<Movie> {
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")?,
})
}