feat: base structure of admin panel.

This commit is contained in:
Kilerd Chan 2018-10-10 23:55:00 +08:00
parent a519cfa5e4
commit 89088d3952
9 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;

View File

@ -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');

View File

@ -0,0 +1 @@
-- This file should undo anything in `up.sql`

View File

@ -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");

25
src/admin.rs Normal file
View File

@ -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 = "<user>")]
fn admin_authentication(user: Form<LoginForm>) -> Result<Redirect, Failure> {
println!("{:?}", user);
Ok(Redirect::to("/admin/login"))
}

0
src/lib.rs Normal file
View File

View File

@ -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();
}

View File

@ -4,5 +4,25 @@ table! {
title -> Varchar,
body -> Text,
published -> Bool,
user_id -> Nullable<Int4>,
publish_at -> Nullable<Timestamp>,
url -> Nullable<Text>,
}
}
table! {
users (id) {
id -> Int4,
username -> Nullable<Text>,
password -> Nullable<Text>,
create_at -> Nullable<Timestamp>,
last_login_at -> Nullable<Timestamp>,
}
}
joinable!(posts -> users (user_id));
allow_tables_to_appear_in_same_query!(
posts,
users,
);

6
static/admin/login.tera Normal file
View File

@ -0,0 +1,6 @@
<form method="POST">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<button>login</button>
</form>