mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-12-02 02:06:27 +00:00
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
//! # Likes helper
|
|
//!
|
|
//! Module dedicated to likes management
|
|
|
|
use crate::data::error::ResultBoxError;
|
|
use crate::helpers::database::QueryInfo;
|
|
use crate::constants::database_tables_names::LIKES_TABLE;
|
|
use crate::data::user::UserID;
|
|
|
|
pub enum LikeType {
|
|
USER,
|
|
POST,
|
|
COMMENT,
|
|
GROUP
|
|
}
|
|
|
|
impl LikeType {
|
|
|
|
/// Get matching database type
|
|
pub fn to_db_type(&self) -> String {
|
|
match self {
|
|
LikeType::USER => "page".to_string(),
|
|
LikeType::POST => "texte".to_string(),
|
|
LikeType::COMMENT => "commentaire".to_string(),
|
|
LikeType::GROUP => "group".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Count the number of likes over an element
|
|
pub fn count(id: u64, kind: LikeType) -> ResultBoxError<usize> {
|
|
QueryInfo::new(LIKES_TABLE)
|
|
.cond_u64("ID_type", id)
|
|
.cond("type", kind.to_db_type().as_ref())
|
|
.exec_count()
|
|
}
|
|
|
|
/// Check if a user likes an element or not
|
|
pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError<bool> {
|
|
if !user_id.is_valid() {
|
|
return Ok(false);
|
|
}
|
|
|
|
Ok(QueryInfo::new(LIKES_TABLE)
|
|
.cond_u64("ID_type", id)
|
|
.cond_user_id("ID_personne", user_id)
|
|
.cond("type", kind.to_db_type().as_ref())
|
|
.exec_count()? > 0)
|
|
} |