feat: add setting modification form in panel

This commit is contained in:
Kilerd Chan 2018-10-24 13:35:41 +08:00
parent bb57e57f01
commit 64cf647f4a
4 changed files with 26 additions and 3 deletions

View File

@ -45,7 +45,8 @@ fn main() {
admin::article_edit, admin::article_edit,
admin::save_article, admin::save_article,
admin::article_creation, admin::article_creation,
admin::change_password admin::change_password,
admin::change_setting
]) ])
.attach(Template::fairing()) .attach(Template::fairing())
.launch(); .launch();

View File

@ -10,6 +10,7 @@ use request::ArticleEditForm;
use chrono::prelude::*; use chrono::prelude::*;
use schema::users; use schema::users;
use rocket::request::FlashMessage; use rocket::request::FlashMessage;
use schema::setting;
#[derive(Queryable, Debug, Serialize, Insertable, AsChangeset)] #[derive(Queryable, Debug, Serialize, Insertable, AsChangeset)]
pub struct Article { 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 struct Setting {
pub name: String, pub name: String,
pub value: Option<String>, pub value: Option<String>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct SerializeFlashMessage <'a> { pub struct SerializeFlashMessage <'a> {
pub name: &'a str, pub name: &'a str,

View File

@ -20,7 +20,7 @@ use chrono::Utc;
use request::NewPasswordForm; use request::NewPasswordForm;
use rocket::request::FlashMessage; use rocket::request::FlashMessage;
use models::SerializeFlashMessage; use models::SerializeFlashMessage;
use models::Setting;
#[get("/login")] #[get("/login")]
fn admin_login() -> Template { fn admin_login() -> Template {
@ -123,3 +123,12 @@ fn change_password(admin: Admin, conn: DbConn, password_form: Form<NewPasswordFo
let _result: QueryResult<User> = diesel::update(users::table.find(admin_user.id)).set(&admin_user).get_result(&*conn); let _result: QueryResult<User> = diesel::update(users::table.find(admin_user.id)).set(&admin_user).get_result(&*conn);
Flash::new(Redirect::moved("/admin"), "success", "password is changed successfully") Flash::new(Redirect::moved("/admin"), "success", "password is changed successfully")
} }
#[post("/setting", data = "<setting_form>")]
fn change_setting(admin: Admin, conn: DbConn, setting_form: Form<Setting>) -> Flash<Redirect> {
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<Setting> = diesel::update(setting::table.find(&setting_form.get().name)).set(&new_setting).get_result(&*conn);
Flash::new(Redirect::to("/admin"), "success", "setting changed")
}

View File

@ -25,3 +25,12 @@
<button>change</button> <button>change</button>
</form> </form>
<h2>Change setting</h2>
<form action="/admin/setting" method="post">
<input type="text" name="name" placeholder="setting name" required>
<input type="text" name="value" placeholder="value" required>
<button>change</button>
</form>