feat: base structure of admin panel.
This commit is contained in:
parent
a519cfa5e4
commit
89088d3952
3
migrations/2018-10-10-134403_create user table/down.sql
Normal file
3
migrations/2018-10-10-134403_create user table/down.sql
Normal file
@ -0,0 +1,3 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
DROP TABLE users;
|
11
migrations/2018-10-10-134403_create user table/up.sql
Normal file
11
migrations/2018-10-10-134403_create user table/up.sql
Normal 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');
|
1
migrations/2018-10-10-135830_link post and user/down.sql
Normal file
1
migrations/2018-10-10-135830_link post and user/down.sql
Normal file
@ -0,0 +1 @@
|
||||
-- This file should undo anything in `up.sql`
|
7
migrations/2018-10-10-135830_link post and user/up.sql
Normal file
7
migrations/2018-10-10-135830_link post and user/up.sql
Normal 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
25
src/admin.rs
Normal 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
0
src/lib.rs
Normal 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();
|
||||
}
|
||||
|
@ -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
6
static/admin/login.tera
Normal 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>
|
Loading…
Reference in New Issue
Block a user