restructure code

This commit is contained in:
philipp 2023-04-06 08:06:50 +02:00
parent 051d20e627
commit 82c4680684
2 changed files with 42 additions and 44 deletions

View File

@ -20,46 +20,6 @@ pub struct User {
is_guest: bool,
}
pub struct AdminUser {
user: User,
}
impl TryFrom<User> for AdminUser {
type Error = LoginError;
fn try_from(user: User) -> Result<Self, Self::Error> {
if user.is_admin {
Ok(AdminUser { user })
} else {
Err(LoginError::NotAnAdmin)
}
}
}
pub struct CoxUser {
user: User,
}
impl Deref for CoxUser {
type Target = User;
fn deref(&self) -> &Self::Target {
&self.user
}
}
impl TryFrom<User> for CoxUser {
type Error = LoginError;
fn try_from(user: User) -> Result<Self, Self::Error> {
if user.is_cox {
Ok(CoxUser { user })
} else {
Err(LoginError::NotACox)
}
}
}
#[derive(Debug)]
pub enum LoginError {
SqlxError(sqlx::Error),
@ -202,8 +162,32 @@ impl<'r> FromRequest<'r> for User {
}
}
pub struct CoxUser {
user: User,
}
impl Deref for CoxUser {
type Target = User;
fn deref(&self) -> &Self::Target {
&self.user
}
}
impl TryFrom<User> for CoxUser {
type Error = LoginError;
fn try_from(user: User) -> Result<Self, Self::Error> {
if user.is_cox {
Ok(CoxUser { user })
} else {
Err(LoginError::NotACox)
}
}
}
#[async_trait]
impl<'r> FromRequest<'r> for AdminUser {
impl<'r> FromRequest<'r> for CoxUser {
type Error = LoginError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
@ -220,8 +204,24 @@ impl<'r> FromRequest<'r> for AdminUser {
}
}
pub struct AdminUser {
user: User,
}
impl TryFrom<User> for AdminUser {
type Error = LoginError;
fn try_from(user: User) -> Result<Self, Self::Error> {
if user.is_admin {
Ok(AdminUser { user })
} else {
Err(LoginError::NotAnAdmin)
}
}
}
#[async_trait]
impl<'r> FromRequest<'r> for CoxUser {
impl<'r> FromRequest<'r> for AdminUser {
type Error = LoginError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {

View File

@ -65,8 +65,6 @@ async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash
#[get("/remove/<trip_details_id>")]
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
//TODO: Check if > 2 hrs to event
UserTrip::delete(db, user.id, trip_details_id).await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")