Post creation
This commit is contained in:
parent
268607da0e
commit
e506cd21b7
|
@ -66,6 +66,11 @@ fn main() {
|
||||||
routes::blogs::details,
|
routes::blogs::details,
|
||||||
routes::blogs::new,
|
routes::blogs::new,
|
||||||
routes::blogs::create,
|
routes::blogs::create,
|
||||||
|
|
||||||
|
routes::posts::details,
|
||||||
|
routes::posts::new,
|
||||||
|
routes::posts::new_auth,
|
||||||
|
routes::posts::create
|
||||||
])
|
])
|
||||||
.manage(init_pool())
|
.manage(init_pool())
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
|
|
|
@ -39,4 +39,12 @@ impl Post {
|
||||||
.expect("Error loading post by id")
|
.expect("Error loading post by id")
|
||||||
.into_iter().nth(0)
|
.into_iter().nth(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option<Post> {
|
||||||
|
posts::table.filter(posts::slug.eq(slug))
|
||||||
|
.limit(1)
|
||||||
|
.load::<Post>(conn)
|
||||||
|
.expect("Error loading post by slug")
|
||||||
|
.into_iter().nth(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod blogs;
|
pub mod blogs;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
|
pub mod posts;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
|
@ -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("/~/<blog>/<slug>", 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("/~/<blog>/new", rank = 1)]
|
||||||
|
fn new(blog: String, user: User) -> Template {
|
||||||
|
Template::render("posts/new", HashMap::<String, String>::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/~/<blog>/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("/~/<blog_name>/new", data = "<data>")]
|
||||||
|
fn create(blog_name: String, data: Form<NewPostForm>, _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())
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
use heck::CamelCase;
|
use heck::CamelCase;
|
||||||
|
use rocket::response::Redirect;
|
||||||
|
|
||||||
/// Remove non alphanumeric characters and CamelCase a string
|
/// Remove non alphanumeric characters and CamelCase a string
|
||||||
pub fn make_actor_id(name: String) -> 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())
|
.filter(|c| c.is_alphanumeric())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn requires_login() -> Redirect {
|
||||||
|
Redirect::to("/login")
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>New post</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Create a post</h1>
|
||||||
|
<form method="post">
|
||||||
|
<label for="title">Title</label>
|
||||||
|
<input name="title">
|
||||||
|
|
||||||
|
<label for="content">Content</label>
|
||||||
|
<textarea name="content"></textarea>
|
||||||
|
|
||||||
|
<label for="license">License</label>
|
||||||
|
<input name="license">
|
||||||
|
|
||||||
|
<input type="submit" value="Publish"/>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue