diff --git a/src/main.rs b/src/main.rs index d15f9004..904c6fa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,6 +66,11 @@ fn main() { routes::blogs::details, routes::blogs::new, routes::blogs::create, + + routes::posts::details, + routes::posts::new, + routes::posts::new_auth, + routes::posts::create ]) .manage(init_pool()) .attach(Template::fairing()) diff --git a/src/models/post.rs b/src/models/post.rs index 378c0051..6c9b49f9 100644 --- a/src/models/post.rs +++ b/src/models/post.rs @@ -39,4 +39,12 @@ impl Post { .expect("Error loading post by id") .into_iter().nth(0) } + + pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option { + posts::table.filter(posts::slug.eq(slug)) + .limit(1) + .load::(conn) + .expect("Error loading post by slug") + .into_iter().nth(0) + } } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index d064d9f4..e9076b5a 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,4 +1,5 @@ pub mod blogs; pub mod instance; pub mod session; +pub mod posts; pub mod user; diff --git a/src/routes/posts.rs b/src/routes/posts.rs new file mode 100644 index 00000000..d61f5968 --- /dev/null +++ b/src/routes/posts.rs @@ -0,0 +1,51 @@ +use rocket::response::Redirect; +use rocket::request::Form; +use rocket_contrib::Template; +use std::collections::HashMap; +use heck::KebabCase; + +use utils; +use db_conn::DbConn; +use models::blogs::*; +use models::post::*; +use models::user::User; + +#[get("/~//", rank = 3)] +fn details(blog: String, slug: String, conn: DbConn) -> String { + let blog = Blog::find_by_actor_id(&*conn, blog).unwrap(); + let post = Post::find_by_slug(&*conn, slug).unwrap(); + format!("{} in {}", post.title, blog.title) +} + +#[get("/~//new", rank = 1)] +fn new(blog: String, user: User) -> Template { + Template::render("posts/new", HashMap::::new()) +} + +#[get("/~//new", rank = 2)] +fn new_auth(blog: String) -> Redirect { + utils::requires_login() +} + +#[derive(FromForm)] +struct NewPostForm { + pub title: String, + pub content: String, + pub license: String +} + +#[post("/~//new", data = "")] +fn create(blog_name: String, data: Form, _user: User, conn: DbConn) -> Redirect { + let blog = Blog::find_by_actor_id(&*conn, blog_name.to_string()).unwrap(); + let form = data.get(); + let slug = form.title.to_string().to_kebab_case(); + Post::insert(&*conn, NewPost { + blog_id: blog.id, + slug: slug.to_string(), + title: form.title.to_string(), + content: form.content.to_string(), + published: true, + license: form.license.to_string() + }); + Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str()) +} diff --git a/src/utils.rs b/src/utils.rs index 1b87bf66..8e3b9bbf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use heck::CamelCase; +use rocket::response::Redirect; /// Remove non alphanumeric characters and CamelCase a string pub fn make_actor_id(name: String) -> String { @@ -9,3 +10,7 @@ pub fn make_actor_id(name: String) -> String { .filter(|c| c.is_alphanumeric()) .collect() } + +pub fn requires_login() -> Redirect { + Redirect::to("/login") +} diff --git a/templates/posts/new.tera b/templates/posts/new.tera new file mode 100644 index 00000000..d3c0e588 --- /dev/null +++ b/templates/posts/new.tera @@ -0,0 +1,22 @@ + + + + + New post + + +

Create a post

+
+ + + + + + + + + + +
+ +