refactor: use from trait for type conversion

This commit is contained in:
EETagent 2022-10-31 11:29:51 +01:00
parent 4d97173176
commit 06553679fb
2 changed files with 8 additions and 8 deletions

View file

@ -1,4 +1,4 @@
use entity::candidate::Model;
use entity::candidate::Model as Candidate;
use portfolio_core::sea_orm::prelude::Uuid;
use portfolio_core::services::candidate_service::CandidateService;
use rocket::http::Status;
@ -7,15 +7,14 @@ use rocket::request::{FromRequest, Request};
use crate::pool::Db;
pub struct SessionAuth(Candidate);
pub struct SessionAuth(Model);
impl SessionAuth {
pub fn model(self) -> Model { // TODO: use into_inner instead?
self.0
impl From<SessionAuth> for Candidate {
fn from(src: SessionAuth) -> Candidate {
src.0
}
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for SessionAuth {
type Error = Option<String>;

View file

@ -50,7 +50,8 @@ async fn create(conn: Connection<'_, Db>, post_form: Json<RegisterRequest>) -> R
#[get("/whoami")]
async fn validate(session: SessionAuth) -> Result<String, Custom<String>> {
Ok(session.model().application.to_string())
let candidate: entity::candidate::Model = session.into();
Ok(candidate.application.to_string())
}
#[post("/login", data = "<login_form>")]