1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can check whether two users are friends or not

This commit is contained in:
Pierre HUBERT 2020-05-26 18:25:33 +02:00
parent 305e836d78
commit 11865c2bb4
4 changed files with 43 additions and 1 deletions

View File

@ -14,6 +14,9 @@ pub mod database_tables_names {
/// User table /// User table
pub const USERS_TABLE: &str = "utilisateurs"; pub const USERS_TABLE: &str = "utilisateurs";
/// Friends table
pub const FRIENDS_TABLE: &str = "amis";
} }
/// The account image to show for user who do not have any /// The account image to show for user who do not have any

View File

@ -136,6 +136,17 @@ impl<'a> RowResult<'a> {
} }
} }
/// 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)?);
match value {
None => Err(ExecError::boxed_string(
format!("Could not extract integer field {} !", name))),
Some(s) => Ok(s?)
}
}
/// Find a string included in the request /// Find a string included in the request
pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> { pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> {
let value = self.row.get_opt(self.find_col(name)?); let value = self.row.get_opt(self.find_col(name)?);
@ -245,6 +256,15 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
Ok(res) Ok(res)
} }
/// Count the number of results a query would have produced
pub fn count(mut q: QueryInfo) -> ResultBoxError<usize> {
&q.fields.clear();
&q.fields.push("COUNT(*) as count".to_string());
query(q, |res| res.get_usize("count"))?.pop()
.ok_or(ExecError::boxed_new("database::count did not return a result!"))
}
/// Structure used to execute a insert query /// Structure used to execute a insert query
pub struct InsertQuery { pub struct InsertQuery {
pub table: String, pub table: String,

View File

@ -0,0 +1,18 @@
//! # Friends helper
//!
//! @author Pierre Hubert
use crate::data::user::UserID;
use crate::data::error::ResultBoxError;
use crate::helpers::database;
use crate::constants::database_tables_names::FRIENDS_TABLE;
use crate::helpers::database::QueryInfo;
/// Check out whether two users are friend or not
pub fn are_friend(user_one: UserID, user_two: UserID) -> ResultBoxError<bool> {
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
.cond_i64("ID_personne", user_one)
.cond_i64("ID_amis", user_two)
.cond_i64("actif", 1))? > 0)
}

View File

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