1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-30 22:13:01 +00:00
comunicapiv3/src/helpers/comments_helper.rs

29 lines
893 B
Rust
Raw Normal View History

2020-07-05 19:32:28 +02:00
//! # Comments helper
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::COMMENTS_TABLE;
use crate::data::comment::Comment;
use crate::data::error::ResultBoxError;
use crate::helpers::database;
/// Get the comments of a post
pub fn get(post_id: u64) -> ResultBoxError<Vec<Comment>> {
database::QueryInfo::new(COMMENTS_TABLE)
.cond_u64("ID_texte", post_id)
.set_order("ID")
.exec(db_to_comment)
}
/// Turn a database entry into a comment object
fn db_to_comment(row: &database::RowResult) -> ResultBoxError<Comment> {
Ok(Comment {
id: row.get_u64("ID")?,
time_sent: row.get_u64("time_insert").unwrap_or(0),
user_id: row.get_user_id("ID_personne")?,
post_id: row.get_u64("ID_texte")?,
content: row.get_str("commentaire")?,
image_path: row.get_optional_str("image_commentaire")?,
})
}