2020-12-27 21:03:10 +00:00
|
|
|
use crate::db::{Dao, TenebrousDbConn};
|
|
|
|
use crate::errors::Error;
|
2021-01-03 22:05:28 +00:00
|
|
|
use crate::models::characters::Character;
|
2020-12-27 21:03:10 +00:00
|
|
|
use crate::models::users::User;
|
2021-01-09 19:39:35 +00:00
|
|
|
|
|
|
|
mod cofd;
|
2020-12-27 21:03:10 +00:00
|
|
|
|
|
|
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
|
|
|
routes![
|
|
|
|
cofd::update_basic_info,
|
2021-01-05 19:32:03 +00:00
|
|
|
cofd::update_attribute_value,
|
2021-01-09 20:02:24 +00:00
|
|
|
cofd::update_skill,
|
2021-01-02 14:51:24 +00:00
|
|
|
cofd::update_skill_value,
|
2020-12-27 21:03:10 +00:00
|
|
|
cofd::add_condition,
|
|
|
|
cofd::remove_condition
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:01:07 +00:00
|
|
|
/// Load the character belonging to the given user, as long as they're
|
|
|
|
/// the owner of that character. Returns an error if user is not
|
|
|
|
/// logged in, the owner of the character is not found, or the logged
|
|
|
|
/// in user does not have the permission to access this character.
|
|
|
|
async fn load_character(
|
2020-12-29 23:06:41 +00:00
|
|
|
conn: &TenebrousDbConn<'_>,
|
2020-12-29 14:01:07 +00:00
|
|
|
logged_in_user: Option<&User>,
|
2021-01-02 22:03:10 +00:00
|
|
|
owner: &str,
|
2020-12-29 14:01:07 +00:00
|
|
|
character_id: i32,
|
|
|
|
) -> Result<Character, Error> {
|
|
|
|
let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?;
|
2020-12-31 20:19:45 +00:00
|
|
|
|
2020-12-29 14:01:07 +00:00
|
|
|
let character: Character = conn
|
|
|
|
.load_character(character_id)
|
|
|
|
.await?
|
|
|
|
.ok_or(Error::NotFound)?;
|
|
|
|
|
2021-01-02 22:03:10 +00:00
|
|
|
if &logged_in_user.username != owner {
|
2020-12-29 14:01:07 +00:00
|
|
|
return Err(Error::NoPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(character)
|
|
|
|
}
|