1
0
Fork 0

Split routes into separate files

This commit is contained in:
Alex Kotov 2020-10-16 04:27:03 +05:00
parent 612b7061e2
commit 05d0d001a8
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 90 additions and 93 deletions

View file

@ -1,3 +1,6 @@
mod home;
mod users;
pub fn routes() -> Vec<rocket::Route> {
routes![
home::index,
@ -5,96 +8,3 @@ pub fn routes() -> Vec<rocket::Route> {
users::sign_up,
]
}
mod home {
use crate::database;
use crate::models;
use rocket_contrib::templates::Template;
#[get("/")]
pub fn index(db_conn: database::DbConn) -> Result<Template, IndexResponse> {
let all_users = models::User::all(db_conn)?;
Ok(Template::render("index", &IndexTemplateContext {
layout: "site",
users: all_users,
}))
}
#[derive(Debug, rocket::response::Responder)]
#[response(content_type = "text/html")]
pub enum IndexResponse {
#[response(status = 500)]
UnknownError(()),
}
#[derive(Serialize)]
struct IndexTemplateContext {
layout: &'static str,
users: Vec<models::User>,
}
impl From<diesel::result::Error> for IndexResponse {
fn from(_: diesel::result::Error) -> Self {
Self::UnknownError(())
}
}
}
mod users {
use crate::database;
use crate::models;
use crate::forms;
use rocket::response::Redirect;
use rocket::request::Form;
use rocket_contrib::templates::Template;
#[get("/sign_up")]
pub fn sign_up_show() -> Template {
Template::render("sign_up", &BasicTemplateContext {
layout: "site",
})
}
#[post("/users", data = "<form>")]
pub fn sign_up(
db_conn: database::DbConn,
form: Form<forms::UserSignUp>,
) -> Result<Redirect, UserSignUpResponse>
{
models::NewUser::from_form(form.0)?
.save(db_conn)?;
Ok(Redirect::to(uri!(super::home::index)))
}
#[derive(Debug, rocket::response::Responder)]
#[response(content_type = "text/html")]
pub enum UserSignUpResponse {
#[response(status = 422)]
InvalidForm(Template),
#[response(status = 500)]
UnknownError(()),
}
#[derive(Serialize)]
struct BasicTemplateContext {
layout: &'static str,
}
impl From<validator::ValidationErrors> for UserSignUpResponse {
fn from(_validation_errors: validator::ValidationErrors) -> Self {
Self::InvalidForm(Template::render("sign_up", &BasicTemplateContext {
layout: "site",
}))
}
}
impl From<diesel::result::Error> for UserSignUpResponse {
fn from(_: diesel::result::Error) -> Self {
Self::UnknownError(())
}
}
}

33
src/routes/home.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::database;
use crate::models;
use rocket_contrib::templates::Template;
#[get("/")]
pub fn index(db_conn: database::DbConn) -> Result<Template, IndexResponse> {
let all_users = models::User::all(db_conn)?;
Ok(Template::render("index", &IndexTemplateContext {
layout: "site",
users: all_users,
}))
}
#[derive(Debug, rocket::response::Responder)]
#[response(content_type = "text/html")]
pub enum IndexResponse {
#[response(status = 500)]
UnknownError(()),
}
#[derive(Serialize)]
struct IndexTemplateContext {
layout: &'static str,
users: Vec<models::User>,
}
impl From<diesel::result::Error> for IndexResponse {
fn from(_: diesel::result::Error) -> Self {
Self::UnknownError(())
}
}

54
src/routes/users.rs Normal file
View file

@ -0,0 +1,54 @@
use crate::database;
use crate::models;
use crate::forms;
use rocket::response::Redirect;
use rocket::request::Form;
use rocket_contrib::templates::Template;
#[get("/sign_up")]
pub fn sign_up_show() -> Template {
Template::render("sign_up", &BasicTemplateContext {
layout: "site",
})
}
#[post("/users", data = "<form>")]
pub fn sign_up(
db_conn: database::DbConn,
form: Form<forms::UserSignUp>,
) -> Result<Redirect, UserSignUpResponse>
{
models::NewUser::from_form(form.0)?
.save(db_conn)?;
Ok(Redirect::to(uri!(super::home::index)))
}
#[derive(Debug, rocket::response::Responder)]
#[response(content_type = "text/html")]
pub enum UserSignUpResponse {
#[response(status = 422)]
InvalidForm(Template),
#[response(status = 500)]
UnknownError(()),
}
#[derive(Serialize)]
struct BasicTemplateContext {
layout: &'static str,
}
impl From<validator::ValidationErrors> for UserSignUpResponse {
fn from(_validation_errors: validator::ValidationErrors) -> Self {
Self::InvalidForm(Template::render("sign_up", &BasicTemplateContext {
layout: "site",
}))
}
}
impl From<diesel::result::Error> for UserSignUpResponse {
fn from(_: diesel::result::Error) -> Self {
Self::UnknownError(())
}
}