From 3f3388cb9a8477b42bea360b727bd2f23f0ac592 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 26 Jun 2020 09:27:32 +0200 Subject: [PATCH] Save new group settings in the database --- src/controllers/groups_controller.rs | 4 ++-- src/data/error.rs | 5 +++-- src/helpers/database.rs | 11 +++++++++++ src/helpers/groups_helper.rs | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 40a619c..41953d4 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -102,7 +102,7 @@ pub fn set_settings(r: &mut HttpRequestHandler) -> RequestResult { 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!") } \ No newline at end of file diff --git a/src/data/error.rs b/src/data/error.rs index 0974f49..269c1b6 100644 --- a/src/data/error.rs +++ b/src/data/error.rs @@ -1,8 +1,9 @@ use core::fmt; -use serde::export::Formatter; use std::error; use std::error::Error; +use serde::export::Formatter; + /// Simple rust error /// /// @author Pierre Hubert @@ -10,7 +11,7 @@ use std::error::Error; /// Simple result type pub type ResultExecError = Result; -pub type ResultBoxError = Result>; +pub type ResultBoxError = Result>; #[derive(Debug, Clone)] pub struct ExecError(pub String); diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 382e4b2..a9633df 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -671,6 +671,12 @@ impl UpdateInfo { 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 pub fn cond_u64(mut self, name: &str, val: u64) -> UpdateInfo { self.cond.insert(name.to_string(), Value::UInt(val)); @@ -727,6 +733,11 @@ impl UpdateInfo { } /// 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 { self.set.insert(name.to_string(), Value::UInt(val)); self diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 479f6f9..9722f65 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -282,6 +282,24 @@ pub fn check_directory_availability(dir: &str, group_id: Option) -> 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 fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?;