Basic blog creation
This commit is contained in:
+7
-1
@@ -7,6 +7,7 @@ extern crate dotenv;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
extern crate bcrypt;
|
||||
extern crate heck;
|
||||
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
@@ -18,6 +19,7 @@ mod db_conn;
|
||||
mod models;
|
||||
mod schema;
|
||||
mod routes;
|
||||
mod utils;
|
||||
|
||||
use db_conn::DbConn;
|
||||
use models::instance::*;
|
||||
@@ -57,7 +59,11 @@ fn main() {
|
||||
routes::user::create,
|
||||
|
||||
routes::session::new,
|
||||
routes::session::create
|
||||
routes::session::create,
|
||||
|
||||
routes::blogs::details,
|
||||
routes::blogs::new,
|
||||
routes::blogs::create,
|
||||
])
|
||||
.manage(init_pool())
|
||||
.attach(Template::fairing())
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
use rocket::request::Form;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use utils;
|
||||
use db_conn::DbConn;
|
||||
use models::blogs::*;
|
||||
use models::instance::Instance;
|
||||
|
||||
#[get("/~/<name>")]
|
||||
fn details(name: String) -> String {
|
||||
format!("Welcome on ~{}", name)
|
||||
}
|
||||
|
||||
#[get("/blogs/new")]
|
||||
fn new() -> Template {
|
||||
Template::render("blogs/new", HashMap::<String, i32>::new())
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct NewBlogForm {
|
||||
pub title: String
|
||||
}
|
||||
|
||||
#[post("/blogs/new", data = "<data>")]
|
||||
fn create(conn: DbConn, data: Form<NewBlogForm>) -> Redirect {
|
||||
let inst = Instance::get_local(&*conn).unwrap();
|
||||
let form = data.get();
|
||||
let slug = utils::make_actor_id(form.title.to_string());
|
||||
|
||||
Blog::insert(&*conn, NewBlog {
|
||||
actor_id: slug.to_string(),
|
||||
title: form.title.to_string(),
|
||||
summary: String::from(""),
|
||||
outbox_url: Blog::compute_outbox(slug.to_string(), inst.public_domain.to_string()),
|
||||
inbox_url: Blog::compute_inbox(slug.to_string(), inst.public_domain.to_string()),
|
||||
instance_id: inst.id
|
||||
});
|
||||
|
||||
Redirect::to(format!("/~/{}", slug).as_str())
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod blogs;
|
||||
pub mod instance;
|
||||
pub mod session;
|
||||
pub mod user;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
use heck::CamelCase;
|
||||
|
||||
pub fn make_actor_id(name: String) -> String {
|
||||
name.as_str().to_camel_case()
|
||||
}
|
||||
Reference in New Issue
Block a user