completed the logic to add new posts from the admin panel

pull/1/head
kirbylife 2019-12-18 03:20:00 -06:00
parent efc22e7eb4
commit 27cac09a08
4 changed files with 29 additions and 17 deletions

View File

@ -1,5 +1,6 @@
use crate::controllers::posts; use crate::controllers::posts;
use crate::misc::get_context; use crate::misc::get_context;
use crate::models::NewPost;
use rocket::http::{Cookie, Cookies}; use rocket::http::{Cookie, Cookies};
use rocket::request::Form; use rocket::request::Form;
use rocket::response::Redirect; use rocket::response::Redirect;
@ -14,13 +15,6 @@ struct Login {
password: String, password: String,
} }
#[derive(FromForm, Debug)]
struct Post {
pub title: String,
pub content: String,
pub published: bool,
}
#[get("/")] #[get("/")]
fn index(mut cookies: Cookies<'_>) -> Result<Redirect, Template> { fn index(mut cookies: Cookies<'_>) -> Result<Redirect, Template> {
let password = env::var("admin_pass").expect("admin_pass not setted"); let password = env::var("admin_pass").expect("admin_pass not setted");
@ -92,22 +86,21 @@ fn add_post(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
} }
#[post("/add_post", data = "<post>")] #[post("/add_post", data = "<post>")]
fn write_post(mut cookies: Cookies<'_>, post: Form<Post>) -> Result<Template, Redirect> { fn write_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> Redirect {
let password = env::var("admin_pass").expect("admin_pass not setted"); let password = env::var("admin_pass").expect("admin_pass not setted");
match cookies.get_private("user") { match cookies.get_private("user") {
Some(cookie) => { Some(cookie) => {
if cookie.value() == password { if cookie.value() == password {
let context = get_context(); match posts::add_post(&post) {
Ok(post) => Redirect::to(format!("/post/{}", post.id)),
println!("{:?}", post); Err(_) => Redirect::to("admin/add_post"),
}
Ok(Template::render("admin/add_post", context))
} else { } else {
Err(Redirect::to("/admin")) Redirect::to("/admin")
} }
} }
None => Err(Redirect::to("/admin")), None => Redirect::to("/admin"),
} }
} }

View File

@ -15,7 +15,7 @@ fn establish_connection() -> PgConnection {
pub mod posts { pub mod posts {
use crate::controllers::*; use crate::controllers::*;
use crate::models::Post; use crate::models::{NewPost, Post};
pub const MAX_POSTS_PER_PAGE: u64 = 20; pub const MAX_POSTS_PER_PAGE: u64 = 20;
@ -48,4 +48,13 @@ pub mod posts {
post post
} }
pub fn add_post(new_post: &NewPost) -> Result<Post, Error> {
use crate::schema::posts;
let connection = establish_connection();
diesel::insert_into(posts::table)
.values(new_post)
.get_result(&connection)
}
} }

View File

@ -1,6 +1,8 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::Serialize; use serde::Serialize;
use super::schema::posts;
#[derive(Queryable, Serialize)] #[derive(Queryable, Serialize)]
pub struct Post { pub struct Post {
pub id: i32, pub id: i32,
@ -10,3 +12,11 @@ pub struct Post {
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
#[derive(FromForm, Insertable, Debug)]
#[table_name = "posts"]
pub struct NewPost {
pub title: String,
pub content: String,
pub published: bool,
}