tenebrous-sheets/src/routes/characters.rs

67 lines
1.8 KiB
Rust
Raw Normal View History

use crate::db::{Dao, TenebrousDbConn};
2020-12-01 21:32:16 +00:00
use crate::errors::Error;
use crate::models::characters::{Character, CharacterDataType, DynCharacterData, Visibility};
use crate::models::users::User;
2020-12-01 21:32:16 +00:00
use rocket_contrib::templates::Template;
mod edit;
mod new;
2020-12-01 21:32:16 +00:00
pub(crate) fn routes() -> Vec<rocket::Route> {
routes![
view_character,
new::new_character_page,
new::new_character_submit,
new::new_character_not_logged_in,
edit::edit_character_page
]
2020-12-01 21:32:16 +00:00
}
#[derive(Serialize)]
2020-12-27 21:49:08 +00:00
struct ViewCharacterContext<'a> {
pub id: i32,
pub name: &'a str,
pub username: &'a str,
pub data_type: &'a CharacterDataType,
pub sheet: Box<DynCharacterData>,
}
fn view_character_template(user: &User, character: Character) -> Result<Template, Error> {
let character = character.uprade()?;
2020-12-01 21:32:16 +00:00
2020-12-27 21:49:08 +00:00
let context = ViewCharacterContext {
id: character.id,
name: &character.character_name,
username: &user.username,
data_type: &character.data_type,
sheet: character.dyn_deserialize()?,
};
use CharacterDataType::*;
let template = match character.data_type {
ChroniclesOfDarkness => Template::render("characters/view_character", context),
Changeling => Template::render("characters/view_changeling_character", context),
};
2020-12-01 21:32:16 +00:00
Ok(template)
}
#[get("/<username>/<character_id>")]
async fn view_character(
character_id: i32,
username: String,
conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>,
) -> Result<Template, Error> {
let user = &conn.load_user(&username).await?.ok_or(Error::NotFound)?;
let character = conn
.load_character(character_id)
.await?
.and_then(|c| c.as_visible_for(logged_in_user))
.ok_or(Error::NotFound)?;
let template = view_character_template(user, character)?;
Ok(template)
}