1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00

Save new group settings in the database

This commit is contained in:
Pierre HUBERT 2020-06-26 09:27:32 +02:00
parent 4f2216d835
commit 3f3388cb9a
4 changed files with 34 additions and 4 deletions

View File

@ -102,7 +102,7 @@ pub fn set_settings(r: &mut HttpRequestHandler) -> RequestResult {
url: r.post_url_opt("url", false)?, url: r.post_url_opt("url", false)?,
}; };
println!("New settings: {:#?}", new_settings); groups_helper::set_settings(&new_settings)?;
r.success("complete implementation") r.success("Group settings have been successfully updated!")
} }

View File

@ -1,8 +1,9 @@
use core::fmt; use core::fmt;
use serde::export::Formatter;
use std::error; use std::error;
use std::error::Error; use std::error::Error;
use serde::export::Formatter;
/// Simple rust error /// Simple rust error
/// ///
/// @author Pierre Hubert /// @author Pierre Hubert
@ -10,7 +11,7 @@ use std::error::Error;
/// Simple result type /// Simple result type
pub type ResultExecError<E> = Result<E, ExecError>; pub type ResultExecError<E> = Result<E, ExecError>;
pub type ResultBoxError<E> = Result<E, Box<dyn Error>>; pub type ResultBoxError<E = ()> = Result<E, Box<dyn Error>>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ExecError(pub String); pub struct ExecError(pub String);

View File

@ -671,6 +671,12 @@ impl UpdateInfo {
self self
} }
/// Filter with a group id
pub fn cond_group_id(mut self, name: &str, val: &GroupID) -> UpdateInfo {
self.cond.insert(name.to_string(), Value::UInt(val.id()));
self
}
/// Filter with an unsigned integer /// Filter with an unsigned integer
pub fn cond_u64(mut self, name: &str, val: u64) -> UpdateInfo { pub fn cond_u64(mut self, name: &str, val: u64) -> UpdateInfo {
self.cond.insert(name.to_string(), Value::UInt(val)); self.cond.insert(name.to_string(), Value::UInt(val));
@ -727,6 +733,11 @@ impl UpdateInfo {
} }
/// Set a new unsigned number /// Set a new unsigned number
pub fn set_u32(mut self, name: &str, val: u32) -> UpdateInfo {
self.set.insert(name.to_string(), Value::UInt(val as u64));
self
}
pub fn set_u64(mut self, name: &str, val: u64) -> UpdateInfo { pub fn set_u64(mut self, name: &str, val: u64) -> UpdateInfo {
self.set.insert(name.to_string(), Value::UInt(val)); self.set.insert(name.to_string(), Value::UInt(val));
self self

View File

@ -282,6 +282,24 @@ pub fn check_directory_availability(dir: &str, group_id: Option<GroupID>) -> Res
} }
} }
/// Set new settings to the group, except group logo
pub fn set_settings(g: &Group) -> ResultBoxError {
database::UpdateInfo::new(GROUPS_LIST_TABLE)
.cond_group_id("id", &g.id)
// Updates
.set_str("name", &g.name)
.set_u64("visibility", g.visibility.to_db())
.set_u32("registration_level", g.registration_level.to_db())
.set_u32("posts_level", g.posts_creation_level.to_db())
.set_opt_str("virtual_directory", g.virtual_directory.clone())
.set_opt_str("description", g.description.clone())
.set_opt_str("url", g.url.clone())
.exec()
}
/// Turn a database entry into a group struct /// Turn a database entry into a group struct
fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> { fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> {
let group_id = row.get_group_id("id")?; let group_id = row.get_group_id("id")?;