From 89088d3952e93564d97e1590c2c9ad37b2e67aa9 Mon Sep 17 00:00:00 2001 From: Kilerd Chan Date: Wed, 10 Oct 2018 23:55:00 +0800 Subject: [PATCH] feat: base structure of admin panel. --- .../down.sql | 3 +++ .../up.sql | 11 ++++++++ .../down.sql | 1 + .../up.sql | 7 ++++++ src/admin.rs | 25 +++++++++++++++++++ src/lib.rs | 0 src/main.rs | 3 +++ src/schema.rs | 20 +++++++++++++++ static/admin/login.tera | 6 +++++ 9 files changed, 76 insertions(+) create mode 100644 migrations/2018-10-10-134403_create user table/down.sql create mode 100644 migrations/2018-10-10-134403_create user table/up.sql create mode 100644 migrations/2018-10-10-135830_link post and user/down.sql create mode 100644 migrations/2018-10-10-135830_link post and user/up.sql create mode 100644 src/admin.rs create mode 100644 src/lib.rs create mode 100644 static/admin/login.tera diff --git a/migrations/2018-10-10-134403_create user table/down.sql b/migrations/2018-10-10-134403_create user table/down.sql new file mode 100644 index 0000000..97b7752 --- /dev/null +++ b/migrations/2018-10-10-134403_create user table/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` + +DROP TABLE users; \ No newline at end of file diff --git a/migrations/2018-10-10-134403_create user table/up.sql b/migrations/2018-10-10-134403_create user table/up.sql new file mode 100644 index 0000000..21c55d9 --- /dev/null +++ b/migrations/2018-10-10-134403_create user table/up.sql @@ -0,0 +1,11 @@ +-- Your SQL goes here +CREATE TABLE users ( + "id" serial, + "username" text, + "password" text, + "create_at" timestamp without time zone, + "last_login_at" timestamp without time zone, + PRIMARY KEY ("id") +); + +INSERT INTO "public"."users"("id", "username", "password", "create_at", "last_login_at") VALUES(1, 'admin', 'admin', '2018-10-10 14:01:35', '2018-10-10 14:01:35'); diff --git a/migrations/2018-10-10-135830_link post and user/down.sql b/migrations/2018-10-10-135830_link post and user/down.sql new file mode 100644 index 0000000..291a97c --- /dev/null +++ b/migrations/2018-10-10-135830_link post and user/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/migrations/2018-10-10-135830_link post and user/up.sql b/migrations/2018-10-10-135830_link post and user/up.sql new file mode 100644 index 0000000..8329bf5 --- /dev/null +++ b/migrations/2018-10-10-135830_link post and user/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here + +ALTER TABLE "public"."posts" + ADD COLUMN "user_id" integer DEFAULT '1', + ADD COLUMN "publish_at" timestamp without time zone, +ADD COLUMN "url" text, +ADD CONSTRAINT "user_id" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id"); diff --git a/src/admin.rs b/src/admin.rs new file mode 100644 index 0000000..08b325f --- /dev/null +++ b/src/admin.rs @@ -0,0 +1,25 @@ +use rocket_contrib::Template; +use tera::Context; +use rocket::response::Redirect; +use rocket::response::Failure; +use rocket::request::Form; + +#[derive_FromForm] +#[derive(Debug)] +struct LoginForm { + username: String, + password: String, +} + +#[get("/login")] +fn admin_login() -> Template { + let context = Context::new(); + Template::render("admin/login", &context) +} + + +#[post("/login", data = "")] +fn admin_authentication(user: Form) -> Result { + println!("{:?}", user); + Ok(Redirect::to("/admin/login")) +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index d8b2b0c..f870b5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate diesel; extern crate dotenv; extern crate pulldown_cmark; extern crate r2d2; +#[macro_use] extern crate rocket; #[macro_use] extern crate rocket_contrib; @@ -30,6 +31,7 @@ use tera::Context; mod pg_pool; mod schema; mod models; +mod admin; #[get("/")] @@ -90,6 +92,7 @@ fn main() { .catch(catchers![not_found_catcher]) .manage(pg_pool::init(&database_url)) .mount("/", routes![index, single_archives, static_content]) + .mount("/admin", routes![admin::admin_login, admin::admin_authentication]) .attach(Template::fairing()) .launch(); } diff --git a/src/schema.rs b/src/schema.rs index 3124ea5..1f0e0ab 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,5 +4,25 @@ table! { title -> Varchar, body -> Text, published -> Bool, + user_id -> Nullable, + publish_at -> Nullable, + url -> Nullable, } } + +table! { + users (id) { + id -> Int4, + username -> Nullable, + password -> Nullable, + create_at -> Nullable, + last_login_at -> Nullable, + } +} + +joinable!(posts -> users (user_id)); + +allow_tables_to_appear_in_same_query!( + posts, + users, +); diff --git a/static/admin/login.tera b/static/admin/login.tera new file mode 100644 index 0000000..f30dbea --- /dev/null +++ b/static/admin/login.tera @@ -0,0 +1,6 @@ +
+ + + + +
\ No newline at end of file