feat: new article page

This commit is contained in:
Kilerd Chan 2019-04-19 14:23:07 +08:00
parent 546423a85c
commit 616d84d05a
5 changed files with 61 additions and 37 deletions

View File

@ -76,7 +76,8 @@ fn main() -> std::io::Result<()> {
web::scope("/admin/")
.service(routers::admin::admin_panel)
.service(routers::admin::admin_login)
.service(routers::admin::admin_authentication),
.service(routers::admin::admin_authentication)
.service(routers::admin::article_creation),
)
.service(routers::rss::rss_page)
.service(routers::article::get_article_by_url)

View File

@ -21,7 +21,7 @@ pub struct Article {
pub url: Option<String>,
}
#[derive(Debug, Insertable, AsChangeset)]
#[derive(Debug, Insertable, AsChangeset, Serialize)]
#[table_name = "articles"]
pub struct NewArticle {
pub id: Option<i32>,

View File

@ -22,7 +22,7 @@
//use rocket_contrib::templates::Template;
//
use crate::models::article::Article;
use crate::models::article::{Article, NewArticle};
use crate::models::setting::Setting;
use crate::models::user::User;
@ -32,6 +32,7 @@ use crate::routers::RubbleResponder;
use actix_web::middleware::identity::Identity;
use actix_web::web::Form;
use actix_web::{get, post, web, Either, HttpResponse, Responder};
use chrono::{NaiveDateTime, Utc};
use serde::Deserialize;
use std::sync::Arc;
use tera::{Context, Tera};
@ -104,38 +105,36 @@ pub fn admin_authentication(
Err(_) => RubbleResponder::Redirect("/admin/login".into()),
}
}
//
//
//#[get("/")]
//pub fn admin_index(admin: Admin, conn: DbConn, flash: Option<FlashMessage>) -> Template {
// let mut context = Context::new();
//
// let articles = Article::load_all(true, &conn);
//
// context.insert("admin", &admin);
// context.insert("articles", &articles);
// context.insert("flash", &SerializeFlashMessage::from(&flash));
// Template::render("admin/index", &context)
//}
//
//
//#[get("/article/new")]
//pub fn article_creation(_admin: Admin) -> Result<Template, Status> {
// let mut context = Context::new();
//
// let article = Article {
// id: -1,
// title: String::new(),
// body: String::new(),
// published: true,
// user_id: 0,
// publish_at: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0),
// url: None,
// };
//
// context.insert("article", &article);
// Ok(Template::render("admin/edit", context))
//}
#[get("/article/new")]
pub fn article_creation(
id: Identity,
tera: web::Data<Arc<Tera>>,
conn: web::Data<Pool>,
) -> impl Responder {
if id.identity().is_none() {
return RubbleResponder::Redirect("/admin/login".into());
}
let connection = conn.get().unwrap();
let admin = User::find_by_username(&*connection, &id.identity().unwrap())
.expect("cannot found this user");
let mut context = Context::new();
let article = NewArticle {
id: None,
title: String::new(),
body: String::new(),
published: true,
user_id: admin.id,
publish_at: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0),
url: None,
};
context.insert("article", &article);
RubbleResponder::Html(tera.render("admin/article_add.html", &context).unwrap())
}
//
//
//#[get("/article/<article_id>")]

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="/admin/article" method="post">
<input type="hidden" name="id" value="{{ article.id }}">
<input type="text" name="title" placeholder="title" required value="{{ article.title }}">
<input type="text" name="url" placeholder="url" value="{{ article.url }}">
<textarea name="body" id="body" cols="30" rows="10" placeholder="body">{{ article.body }}</textarea>
<input type="datetime-local" name="publish_at" id="publish_at" value="{{ article.publish_at }}">
<input type="checkbox" name="published">
<button>submit</button>
</form>
</body>
</html>

View File

@ -1,4 +1,3 @@
<!DOCTYPE html>
<html lang="en">
<head>
@ -11,7 +10,7 @@
<form action="/admin/article" method="post">
<input type="hidden" name="id" value="{{ article.id }}">
<input type="hidden" name="id" value="">
<input type="text" name="title" placeholder="title" required value="{{ article.title }}">
<input type="text" name="url" placeholder="url" value="{{ article.url }}">
<textarea name="body" id="" cols="30" rows="10" placeholder="body">{{ article.body }}</textarea>