2020-12-05 13:07:33 +00:00
|
|
|
use crate::db::{Dao, TenebrousDbConn};
|
2020-12-01 21:32:16 +00:00
|
|
|
use crate::errors::Error;
|
2020-12-08 22:26:47 +00:00
|
|
|
use crate::models::characters::{Character, CharacterDataType, DynCharacterData, Visibility};
|
2020-12-05 20:15:14 +00:00
|
|
|
use crate::models::users::User;
|
2020-12-01 21:32:16 +00:00
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
|
2020-12-27 21:03:10 +00:00
|
|
|
mod edit;
|
2020-12-08 22:26:47 +00:00
|
|
|
mod new;
|
|
|
|
|
2020-12-01 21:32:16 +00:00
|
|
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
2020-12-03 19:45:56 +00:00
|
|
|
routes![
|
|
|
|
view_character,
|
2020-12-09 08:58:49 +00:00
|
|
|
new::new_character_page,
|
|
|
|
new::new_character_submit,
|
2020-12-08 22:26:47 +00:00
|
|
|
new::new_character_not_logged_in,
|
2020-12-27 21:03:10 +00:00
|
|
|
edit::edit_character_page
|
2020-12-03 19:45:56 +00:00
|
|
|
]
|
2020-12-01 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 08:47:34 +00:00
|
|
|
#[derive(Serialize)]
|
2020-12-27 21:49:08 +00:00
|
|
|
struct ViewCharacterContext<'a> {
|
|
|
|
pub id: i32,
|
2020-12-08 08:47:34 +00:00
|
|
|
pub name: &'a str,
|
|
|
|
pub username: &'a str,
|
2020-12-08 22:26:47 +00:00
|
|
|
pub data_type: &'a CharacterDataType,
|
2020-12-08 21:47:01 +00:00
|
|
|
pub sheet: Box<DynCharacterData>,
|
2020-12-08 08:47:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
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,
|
2020-12-08 08:47:34 +00:00
|
|
|
name: &character.character_name,
|
|
|
|
username: &user.username,
|
2020-12-08 22:26:47 +00:00
|
|
|
data_type: &character.data_type,
|
2020-12-08 21:47:01 +00:00
|
|
|
sheet: character.dyn_deserialize()?,
|
2020-12-08 08:47:34 +00:00
|
|
|
};
|
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
use CharacterDataType::*;
|
|
|
|
let template = match character.data_type {
|
2021-01-01 22:56:41 +00:00
|
|
|
ChroniclesOfDarkness => Template::render("characters/view_character", context),
|
|
|
|
Changeling => Template::render("characters/view_changeling_character", context),
|
2020-12-08 22:26:47 +00:00
|
|
|
};
|
2020-12-01 21:32:16 +00:00
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
Ok(template)
|
2020-12-03 19:45:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
#[get("/<username>/<character_id>")]
|
2020-12-13 13:57:50 +00:00
|
|
|
async fn view_character(
|
2020-12-08 22:26:47 +00:00
|
|
|
character_id: i32,
|
|
|
|
username: String,
|
2020-12-29 23:06:41 +00:00
|
|
|
conn: TenebrousDbConn<'_>,
|
2020-12-08 22:26:47 +00:00
|
|
|
logged_in_user: Option<&User>,
|
|
|
|
) -> Result<Template, Error> {
|
2020-12-29 23:06:41 +00:00
|
|
|
let user = &conn.load_user(&username).await?.ok_or(Error::NotFound)?;
|
2020-12-07 20:32:02 +00:00
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
let character = conn
|
2020-12-13 13:57:50 +00:00
|
|
|
.load_character(character_id)
|
|
|
|
.await?
|
2020-12-08 22:26:47 +00:00
|
|
|
.and_then(|c| c.as_visible_for(logged_in_user))
|
|
|
|
.ok_or(Error::NotFound)?;
|
2020-12-07 20:32:02 +00:00
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
let template = view_character_template(user, character)?;
|
|
|
|
Ok(template)
|
2020-12-03 19:45:56 +00:00
|
|
|
}
|