diff --git a/src/main.rs b/src/main.rs index d0d59a1..13e01a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) diff --git a/src/models/article.rs b/src/models/article.rs index 1a42a1f..d52ea3e 100644 --- a/src/models/article.rs +++ b/src/models/article.rs @@ -21,7 +21,7 @@ pub struct Article { pub url: Option, } -#[derive(Debug, Insertable, AsChangeset)] +#[derive(Debug, Insertable, AsChangeset, Serialize)] #[table_name = "articles"] pub struct NewArticle { pub id: Option, diff --git a/src/routers/admin.rs b/src/routers/admin.rs index fa1748a..5fff963 100644 --- a/src/routers/admin.rs +++ b/src/routers/admin.rs @@ -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) -> 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 { -// 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>, + conn: web::Data, +) -> 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/")] diff --git a/templates/admin/article_add.html b/templates/admin/article_add.html new file mode 100644 index 0000000..9f47cbb --- /dev/null +++ b/templates/admin/article_add.html @@ -0,0 +1,25 @@ + + + + + + + + Document + + + +
+ + + + + + + + + +
+ + + \ No newline at end of file diff --git a/templates/admin/edit.tera b/templates/admin/edit.tera index 2074396..79eb413 100644 --- a/templates/admin/edit.tera +++ b/templates/admin/edit.tera @@ -1,4 +1,3 @@ - @@ -11,7 +10,7 @@
- +