mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can update like status of user page
This commit is contained in:
@ -7,7 +7,9 @@ use crate::data::error::ResultBoxError;
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::database;
|
||||
use crate::helpers::database::QueryInfo;
|
||||
use crate::utils::date_utils::mysql_date;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum LikeType {
|
||||
USER,
|
||||
POST,
|
||||
@ -48,6 +50,29 @@ pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError<bo
|
||||
.exec_count()? > 0)
|
||||
}
|
||||
|
||||
/// Update like status
|
||||
pub fn update(user_id: &UserID, liking: bool, id: u64, kind: LikeType) -> ResultBoxError {
|
||||
if !liking {
|
||||
return database::DeleteQuery::new(LIKES_TABLE)
|
||||
.cond_user_id("ID_personne", user_id)
|
||||
.cond_u64("ID_type", id)
|
||||
.cond_str("type", &kind.to_db_type())
|
||||
.exec();
|
||||
}
|
||||
|
||||
|
||||
if is_liking(user_id, id, kind)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
database::InsertQuery::new(LIKES_TABLE)
|
||||
.add_user_id("ID_personne", user_id)
|
||||
.add_u64("ID_type", id)
|
||||
.add_str("Date_envoi", &mysql_date())
|
||||
.add_str("type", &kind.to_db_type())
|
||||
.insert_drop_result()
|
||||
}
|
||||
|
||||
/// Delete all the likes associated with a post
|
||||
pub fn delete_all(id: u64, kind: LikeType) -> ResultBoxError {
|
||||
database::DeleteQuery::new(LIKES_TABLE)
|
||||
|
Reference in New Issue
Block a user