Plume/src/routes/posts.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

2018-04-24 11:21:39 +02:00
use heck::KebabCase;
2018-04-23 16:25:39 +02:00
use rocket::request::Form;
2018-04-24 11:21:39 +02:00
use rocket::response::Redirect;
2018-04-23 16:25:39 +02:00
use rocket_contrib::Template;
use std::collections::HashMap;
use db_conn::DbConn;
use models::blogs::*;
2018-04-23 16:39:06 +02:00
use models::post_authors::*;
2018-04-24 11:21:39 +02:00
use models::posts::*;
2018-04-23 17:19:28 +02:00
use models::users::User;
2018-04-24 11:21:39 +02:00
use utils;
2018-04-23 16:25:39 +02:00
#[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)
}
2018-04-24 11:21:39 +02:00
#[get("/~/<_blog>/new", rank = 1)]
fn new(_blog: String, _user: User) -> Template {
2018-04-23 16:25:39 +02:00
Template::render("posts/new", HashMap::<String, String>::new())
}
2018-04-24 11:21:39 +02:00
#[get("/~/<_blog>/new", rank = 2)]
fn new_auth(_blog: String) -> Redirect {
2018-04-23 16:25:39 +02:00
utils::requires_login()
}
#[derive(FromForm)]
struct NewPostForm {
pub title: String,
pub content: String,
pub license: String
}
#[post("/~/<blog_name>/new", data = "<data>")]
2018-04-23 16:39:06 +02:00
fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) -> Redirect {
2018-04-23 16:25:39 +02:00
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();
2018-04-23 16:39:06 +02:00
let post = Post::insert(&*conn, NewPost {
2018-04-23 16:25:39 +02:00
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()
});
2018-04-23 16:39:06 +02:00
PostAuthor::insert(&*conn, NewPostAuthor {
post_id: post.id,
author_id: user.id
});
2018-04-23 16:25:39 +02:00
Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str())
}