2020-12-01 21:32:16 +00:00
|
|
|
use crate::db::{self, TenebrousDbConn};
|
|
|
|
use crate::errors::Error;
|
|
|
|
use crate::models::{
|
|
|
|
characters::{CharacterEntry, NewCharacter},
|
|
|
|
User,
|
|
|
|
};
|
2020-12-03 19:45:56 +00:00
|
|
|
use rocket::response::Redirect;
|
2020-12-01 21:32:16 +00:00
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
use serde_derive::Serialize;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
2020-12-03 19:45:56 +00:00
|
|
|
routes![
|
|
|
|
view_character,
|
|
|
|
new_character,
|
|
|
|
new_character_not_logged_in,
|
|
|
|
edit_character
|
|
|
|
]
|
2020-12-01 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO make private -- currently is referenced in homepage route.
|
|
|
|
//or move to common place.
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct TemplateContext {
|
|
|
|
pub characters: Vec<CharacterEntry>,
|
|
|
|
pub user: User,
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO should return result based on whether or not character is publicly viewable.
|
|
|
|
#[get("/<user>/<character_id>")]
|
|
|
|
fn view_character(
|
|
|
|
character_id: i32,
|
|
|
|
user: Option<User>,
|
|
|
|
conn: TenebrousDbConn,
|
|
|
|
) -> Result<Template, Error> {
|
|
|
|
let user = user.ok_or(Error::NotFound)?;
|
|
|
|
let character = db::load_character(conn, character_id)?.ok_or(Error::NotFound)?;
|
|
|
|
|
|
|
|
let mut context = HashMap::new();
|
|
|
|
context.insert("name", character.name);
|
|
|
|
context.insert("username", user.username);
|
|
|
|
Ok(Template::render("view_character", context))
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:45:56 +00:00
|
|
|
#[get("/new")]
|
|
|
|
fn new_character(logged_in_user: User, conn: TenebrousDbConn) -> Result<Template, Error> {
|
|
|
|
let context = HashMap::<String, String>::new();
|
|
|
|
Ok(Template::render("new_character", context))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/new", rank = 2)]
|
|
|
|
fn new_character_not_logged_in() -> Redirect {
|
|
|
|
super::common::redirect_to_login()
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:32:16 +00:00
|
|
|
#[get("/<owner>/<character_id>/edit")]
|
|
|
|
fn edit_character(
|
|
|
|
character_id: i32,
|
|
|
|
owner: Option<User>,
|
|
|
|
logged_in_user: Option<User>,
|
|
|
|
conn: TenebrousDbConn,
|
|
|
|
) -> Result<Template, Error> {
|
|
|
|
let owner = owner.ok_or(Error::NotFound)?;
|
|
|
|
let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?;
|
|
|
|
let character = db::load_character(conn, character_id)?.ok_or(Error::NotFound)?;
|
|
|
|
|
|
|
|
if logged_in_user != owner {
|
|
|
|
return Err(Error::NoPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut context = HashMap::new();
|
|
|
|
context.insert("name", character.name);
|
|
|
|
context.insert("username", owner.username);
|
|
|
|
Ok(Template::render("view_character", context))
|
|
|
|
}
|