mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can create custom emoji
This commit is contained in:
@ -2,11 +2,12 @@
|
||||
//!
|
||||
//! @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;
|
||||
use crate::data::custom_emoji::CustomEmoji;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::new_custom_emoji::NewCustomEmoji;
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::database;
|
||||
|
||||
/// Get the list of emojies of a user
|
||||
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<CustomEmoji>> {
|
||||
@ -15,12 +16,30 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<CustomEmoji>> {
|
||||
.exec(db_to_custom_emoji)
|
||||
}
|
||||
|
||||
/// Check if a given user already as an emoji shortcut or not
|
||||
pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError<bool> {
|
||||
database::QueryInfo::new(EMOJIS_TABLE)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond("shortcut", shortcut)
|
||||
.exec_count()
|
||||
.map(|r| r > 0)
|
||||
}
|
||||
|
||||
/// Insert a new emoji in the database
|
||||
pub fn insert(emoji: &NewCustomEmoji) -> ResultBoxError<u64> {
|
||||
database::InsertQuery::new(EMOJIS_TABLE)
|
||||
.add_user_id("user_id", &emoji.user_id)
|
||||
.add_str("shortcut", &emoji.shortcut)
|
||||
.add_str("path", &emoji.path)
|
||||
.insert_expect_result()
|
||||
}
|
||||
|
||||
/// 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_user_id("user_id")?,
|
||||
shortcut: row.get_str("shortcut")?,
|
||||
path: row.get_str("path")?
|
||||
path: row.get_str("path")?,
|
||||
})
|
||||
}
|
@ -138,7 +138,7 @@ impl QueryInfo {
|
||||
}
|
||||
|
||||
pub fn add_conditions(mut self, map: &HashMap<String, mysql::Value>) -> QueryInfo {
|
||||
for (k,v) in map {
|
||||
for (k, v) in map {
|
||||
self.conditions.insert(k.to_string(), v.clone());
|
||||
}
|
||||
self
|
||||
@ -617,6 +617,12 @@ impl InsertQuery {
|
||||
insert(self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Process insert, excepting an ID in the response
|
||||
pub fn insert_expect_result(self) -> ResultBoxError<u64> {
|
||||
let res = insert(self)?;
|
||||
res.ok_or(ExecError::boxed_new("Expected an ID in insert result!"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a new entry into the database
|
||||
|
Reference in New Issue
Block a user