1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 17:05:16 +00:00

Get user custom emojies

This commit is contained in:
2020-05-26 19:45:38 +02:00
parent 2b1161c3f0
commit c4f8e5fa97
9 changed files with 106 additions and 4 deletions

View File

@ -0,0 +1,26 @@
//! # Custom emojies helper
//!
//! @author Pierre Hubert
use crate::data::user::UserID;
use crate::data::error::ResultBoxError;
use crate::data::custom_emoji::CustomEmoji;
use crate::helpers::database;
use crate::constants::database_tables_names::EMOJIS_TABLE;
/// Get the list of emojies of a user
pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<CustomEmoji>> {
database::QueryInfo::new(EMOJIS_TABLE)
.cond_i64("user_id", user_id)
.exec(db_to_custom_emoji)
}
/// Turn a database entry into a [CustomEmoji]
fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError<CustomEmoji> {
Ok(CustomEmoji {
id: row.get_u64("id")?,
user_id: row.get_int64("user_id")?,
shortcut: row.get_str("shortcut")?,
path: row.get_str("path")?
})
}

View File

@ -86,6 +86,11 @@ impl QueryInfo {
self
}
pub fn cond_u64(mut self, key: &str, val: u64) -> QueryInfo {
self.conditions.insert(key.to_string(), val.to_string());
self
}
pub fn cond_i64(mut self, key: &str, val: i64) -> QueryInfo {
self.conditions.insert(key.to_string(), val.to_string());
self
@ -96,6 +101,12 @@ impl QueryInfo {
self.fields.push(key.to_string());
self
}
/// Execute query
pub fn exec<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(self, process_function: F)
-> Result<Vec<E>, Box<dyn Error>> {
query(self, process_function)
}
}
/// Struct used to read the result of a request
@ -136,6 +147,17 @@ impl<'a> RowResult<'a> {
}
}
/// Find an integer included in the request
pub fn get_u64(&self, name: &str) -> Result<u64, Box<dyn Error>> {
let value = self.row.get_opt(self.find_col(name)?);
match value {
None => Err(ExecError::boxed_string(
format!("Could not extract integer field {} !", name))),
Some(s) => Ok(s?)
}
}
/// Find an integer included in the request
pub fn get_usize(&self, name: &str) -> Result<usize, Box<dyn Error>> {
let value = self.row.get_opt(self.find_col(name)?);

View File

@ -3,4 +3,5 @@ pub mod database;
pub mod api_helper;
pub mod account_helper;
pub mod user_helper;
pub mod friends_helper;
pub mod friends_helper;
pub mod custom_emojies_helper;