diff --git a/src/main.rs b/src/main.rs index 6f49a96..d9e7936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,8 @@ fn main() { admin::article_edit, admin::save_article, admin::article_creation, - admin::change_password + admin::change_password, + admin::change_setting ]) .attach(Template::fairing()) .launch(); diff --git a/src/models.rs b/src/models.rs index 3739473..542d916 100644 --- a/src/models.rs +++ b/src/models.rs @@ -10,6 +10,7 @@ use request::ArticleEditForm; use chrono::prelude::*; use schema::users; use rocket::request::FlashMessage; +use schema::setting; #[derive(Queryable, Debug, Serialize, Insertable, AsChangeset)] pub struct Article { @@ -103,12 +104,15 @@ impl User { } } -#[derive(Queryable, Debug, Serialize)] +#[derive_FromForm] +#[derive(Queryable, Debug, Serialize, Insertable, AsChangeset)] +#[table_name = "setting"] pub struct Setting { pub name: String, pub value: Option, } + #[derive(Debug, Serialize)] pub struct SerializeFlashMessage <'a> { pub name: &'a str, diff --git a/src/routers/admin.rs b/src/routers/admin.rs index 6374741..1c8cc34 100644 --- a/src/routers/admin.rs +++ b/src/routers/admin.rs @@ -20,7 +20,7 @@ use chrono::Utc; use request::NewPasswordForm; use rocket::request::FlashMessage; use models::SerializeFlashMessage; - +use models::Setting; #[get("/login")] fn admin_login() -> Template { @@ -122,4 +122,13 @@ fn change_password(admin: Admin, conn: DbConn, password_form: Form = diesel::update(users::table.find(admin_user.id)).set(&admin_user).get_result(&*conn); Flash::new(Redirect::moved("/admin"), "success", "password is changed successfully") +} + +#[post("/setting", data = "")] +fn change_setting(admin: Admin, conn: DbConn, setting_form: Form) -> Flash { + use schema::{setting, setting::dsl::*}; + + let new_setting = Setting { name: setting_form.get().name.clone(), value: setting_form.get().value.clone() }; + let fetched_setting: QueryResult = diesel::update(setting::table.find(&setting_form.get().name)).set(&new_setting).get_result(&*conn); + Flash::new(Redirect::to("/admin"), "success", "setting changed") } \ No newline at end of file diff --git a/static/admin/index.tera b/static/admin/index.tera index 30a466c..67a2195 100644 --- a/static/admin/index.tera +++ b/static/admin/index.tera @@ -23,5 +23,14 @@ + + + +

Change setting

+ +
+ + +
\ No newline at end of file