1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-09-20 11:58:47 +00:00

Create new groups

This commit is contained in:
2020-06-24 09:08:31 +02:00
parent d38e2ee4ab
commit 5a14aaee31
9 changed files with 139 additions and 5 deletions

View File

@@ -10,8 +10,8 @@ use mysql::prelude::Queryable;
use crate::data::config::DatabaseConfig;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::data::group_id::GroupID;
use crate::data::user::UserID;
/// Database access helper
///
@@ -498,6 +498,12 @@ impl InsertQuery {
self
}
pub fn add_group_id(mut self, key: &str, value: &GroupID) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value.id()));
self
}
/// Legacy database boolean (1 = true / 0 = false)
pub fn add_legacy_bool(mut self, key: &str, value: bool) -> InsertQuery {
let num = match value {
@@ -513,6 +519,12 @@ impl InsertQuery {
pub fn insert(self) -> ResultBoxError<Option<u64>> {
insert(self)
}
/// Process insert, drop the result of the operation
pub fn insert_drop_result(self) -> ResultBoxError<()> {
insert(self)?;
Ok(())
}
}
/// Insert a new entry into the database

View File

@@ -2,11 +2,14 @@
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::GROUPS_LIST_TABLE;
use crate::data::error::ResultBoxError;
use crate::constants::database_tables_names::{GROUPS_LIST_TABLE, GROUPS_MEMBERS_TABLE};
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::GroupVisibilityLevel;
use crate::data::group_id::GroupID;
use crate::data::group_member::{GroupMember, GroupMembershipLevel};
use crate::data::new_group::NewGroup;
use crate::helpers::database;
use crate::utils::date_utils::time;
impl GroupVisibilityLevel {
pub fn to_db(&self) -> u64 {
@@ -18,6 +21,52 @@ impl GroupVisibilityLevel {
}
}
impl GroupMembershipLevel {
pub fn to_db(&self) -> u32 {
match self {
GroupMembershipLevel::ADMINISTRATOR => 0,
GroupMembershipLevel::MODERATOR => 1,
GroupMembershipLevel::MEMBER => 2,
GroupMembershipLevel::INVITED => 3,
GroupMembershipLevel::PENDING => 4,
GroupMembershipLevel::VISITOR => 5,
}
}
}
/// Create a new group. Returns the ID of the new group
pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
// First, create the group
let group_id = database::InsertQuery::new(GROUPS_LIST_TABLE)
.add_u64("time_create", time())
.add_user_id("userid_create", group.owner_id)
.add_str("name", &group.name)
.insert()?.ok_or(ExecError::new("Could not get group ID!"))?;
let group_id = GroupID::new(group_id);
// Insert first member
insert_member(&GroupMember {
id: 0,
user_id: group.owner_id,
group_id: group_id.clone(),
time_create: time(),
level: GroupMembershipLevel::ADMINISTRATOR,
following: true,
})?;
Ok(group_id)
}
/// Insert a new group into the database
pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> {
database::InsertQuery::new(GROUPS_MEMBERS_TABLE)
.add_group_id("groups_id", &m.group_id)
.add_user_id("user_id", m.user_id)
.add_u64("time_create", m.time_create)
.add_u32("level", m.level.to_db())
.insert_drop_result()
}
/// Find a group id by virtual directory
pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<GroupID> {
database::QueryInfo::new(GROUPS_LIST_TABLE)