2020-06-03 13:28:28 +02:00
|
|
|
//! # Groups helper
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-06-23 18:55:23 +02:00
|
|
|
use crate::constants::database_tables_names::GROUPS_LIST_TABLE;
|
2020-06-03 13:28:28 +02:00
|
|
|
use crate::data::error::ResultBoxError;
|
2020-06-23 18:55:23 +02:00
|
|
|
use crate::data::group::GroupVisibilityLevel;
|
|
|
|
use crate::data::group_id::GroupID;
|
2020-06-03 13:28:28 +02:00
|
|
|
use crate::helpers::database;
|
2020-06-23 18:55:23 +02:00
|
|
|
|
|
|
|
impl GroupVisibilityLevel {
|
|
|
|
pub fn to_db(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
GroupVisibilityLevel::OPEN_GROUP => 0,
|
|
|
|
GroupVisibilityLevel::PRIVATE_GROUP => 1,
|
|
|
|
GroupVisibilityLevel::SECRETE_GROUP => 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 13:28:28 +02:00
|
|
|
|
|
|
|
/// Find a group id by virtual directory
|
|
|
|
pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
|
|
|
|
database::QueryInfo::new(GROUPS_LIST_TABLE)
|
|
|
|
.cond("virtual_directory", dir)
|
|
|
|
.add_field("id")
|
|
|
|
.query_row(|res| res.get_u64("id"))
|
2020-06-23 18:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Search for group
|
|
|
|
pub fn search_group(query: &str, limit: u64) -> ResultBoxError<Vec<GroupID>> {
|
|
|
|
database::QueryInfo::new(GROUPS_LIST_TABLE)
|
|
|
|
.set_custom_where("name LIKE ? AND visibility != ?")
|
|
|
|
.add_custom_where_argument_str(format!("%{}%", query).as_str())
|
|
|
|
.add_custom_where_argument_u64(GroupVisibilityLevel::SECRETE_GROUP.to_db())
|
|
|
|
.set_limit(limit)
|
|
|
|
.add_field("id")
|
|
|
|
.exec(|row| row.get_group_id("id"))
|
2020-06-03 13:28:28 +02:00
|
|
|
}
|