1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00

Can update like status of user page

This commit is contained in:
Pierre HUBERT 2020-07-10 10:24:39 +02:00
parent d23411dca7
commit 4de337421a
5 changed files with 73 additions and 3 deletions

View File

@ -39,7 +39,7 @@ struct APIAdvancedInfo {
backgroundImage: String,
number_friends: usize,
pageLikes: usize,
user_page_like: bool,
user_like_page: bool,
can_post_texts: bool,
}
@ -89,7 +89,7 @@ impl APIUserInfo {
backgroundImage: background_image_helper::get_url(&info.id),
number_friends,
pageLikes: likes_helper::count(info.id.id(), LikeType::USER)?,
user_page_like: likes_page,
user_like_page: likes_page,
can_post_texts: user_helper::can_create_posts(&curr_user_id, &info.id)?,
});

View File

@ -0,0 +1,41 @@
//! # Likes controller
//!
//! @author Pierre Hubert
use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::error::ExecError;
use crate::helpers::likes_helper::LikeType;
use crate::helpers::{user_helper, likes_helper};
struct LikeTarget(u64, LikeType);
/// Update like status
pub fn update(r: &mut HttpRequestHandler) -> RequestResult {
let req_type = r.post_string("type")?;
let is_liking = r.post_bool("like")?;
let target = match req_type.as_str() {
// In case of user
"user" => {
let user_id = r.post_user_id("id")?;
if !user_helper::can_see_user_page(&r.user_id_or_invalid(), &user_id)? {
r.forbidden("You cannot access this user information!".to_string())?;
}
LikeTarget(user_id.id(), LikeType::USER)
}
_ => {
r.internal_error(ExecError::boxed_new("Unsupported like type!"))?;
unreachable!();
}
};
likes_helper::update(r.user_id_ref()?, is_liking, target.0, target.1)?;
r.success("")
}

View File

@ -10,5 +10,6 @@ pub mod search_controller;
pub mod groups_controller;
pub mod posts_controller;
pub mod comments_controller;
pub mod likes_controller;
pub mod movies_controller;
pub mod virtual_directory_controller;

View File

@ -1,6 +1,6 @@
use std::error::Error;
use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, movies_controller, posts_controller, search_controller, server_controller, user_controller, virtual_directory_controller};
use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, posts_controller, search_controller, server_controller, user_controller, virtual_directory_controller};
use crate::controllers::routes::Method::{GET, POST};
use crate::data::http_request_handler::HttpRequestHandler;
@ -228,6 +228,9 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/comments/delete", Box::new(comments_controller::delete)),
// Likes controller
Route::post("/likes/update", Box::new(likes_controller::update)),
// Movies controller
Route::post("/movies/get_list", Box::new(movies_controller::get_list)),

View File

@ -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)