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;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
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-03 19:45:56 +00:00
|
|
|
edit_character
|
|
|
|
]
|
2020-12-01 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 08:47:34 +00:00
|
|
|
#[derive(Serialize)]
|
2020-12-08 21:47:01 +00:00
|
|
|
struct ViewCharacterTemplate<'a> {
|
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-08 21:47:01 +00:00
|
|
|
let context = ViewCharacterTemplate {
|
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 {
|
|
|
|
ChroniclesOfDarknessV1 => Template::render("characters/view_character", context),
|
|
|
|
ChangelingV1 => Template::render("characters/view_character", context),
|
|
|
|
};
|
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>")]
|
|
|
|
fn view_character(
|
|
|
|
character_id: i32,
|
|
|
|
username: String,
|
2020-12-07 20:32:02 +00:00
|
|
|
conn: TenebrousDbConn,
|
2020-12-08 22:26:47 +00:00
|
|
|
logged_in_user: Option<&User>,
|
|
|
|
) -> Result<Template, Error> {
|
|
|
|
let user = &conn.load_user(&username)?.ok_or(Error::NotFound)?;
|
2020-12-07 20:32:02 +00:00
|
|
|
|
2020-12-08 22:26:47 +00:00
|
|
|
let character = conn
|
|
|
|
.load_character(character_id)?
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
2020-12-01 21:32:16 +00:00
|
|
|
#[get("/<owner>/<character_id>/edit")]
|
|
|
|
fn edit_character(
|
|
|
|
character_id: i32,
|
2020-12-05 13:07:33 +00:00
|
|
|
owner: String,
|
|
|
|
logged_in_user: Option<&User>,
|
2020-12-01 21:32:16 +00:00
|
|
|
conn: TenebrousDbConn,
|
|
|
|
) -> Result<Template, Error> {
|
2020-12-05 13:07:33 +00:00
|
|
|
let owner = conn.load_user(&owner)?.ok_or(Error::NotFound)?;
|
2020-12-01 21:32:16 +00:00
|
|
|
let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?;
|
2020-12-05 13:07:33 +00:00
|
|
|
let character = conn.load_character(character_id)?.ok_or(Error::NotFound)?;
|
2020-12-01 21:32:16 +00:00
|
|
|
|
2020-12-05 13:07:33 +00:00
|
|
|
if logged_in_user != &owner {
|
2020-12-01 21:32:16 +00:00
|
|
|
return Err(Error::NoPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut context = HashMap::new();
|
2020-12-07 20:32:02 +00:00
|
|
|
context.insert("name", character.character_name);
|
2020-12-01 21:32:16 +00:00
|
|
|
context.insert("username", owner.username);
|
|
|
|
Ok(Template::render("view_character", context))
|
|
|
|
}
|