60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
|
use crate::db::{self, TenebrousDbConn};
|
||
|
use crate::errors::Error;
|
||
|
use crate::models::{
|
||
|
characters::{CharacterEntry, NewCharacter},
|
||
|
User,
|
||
|
};
|
||
|
use diesel::prelude::*;
|
||
|
use rocket_contrib::templates::Template;
|
||
|
use serde_derive::Serialize;
|
||
|
use std::collections::HashMap;
|
||
|
|
||
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
||
|
routes![view_character, edit_character]
|
||
|
}
|
||
|
|
||
|
//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))
|
||
|
}
|
||
|
|
||
|
#[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))
|
||
|
}
|