Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Reduce Repeated Code #6

Merged
merged 8 commits into from
Feb 10, 2024
Merged

enhance: Reduce Repeated Code #6

merged 8 commits into from
Feb 10, 2024

Conversation

bennjii
Copy link
Collaborator

@bennjii bennjii commented Feb 5, 2024

Due to the early-adoption of 0.4 and 0.5 beta features in rocket, middlewares were not explored in depth (FromRequest, ..). These are now included, as well as an improvement to the Database middleware, adding a Session middleware and re-forming the return types to be more uniform and generic. Thus greatly reducing the complexity of each rocket function from an average of 8 lines to 2.

Further work is being done to reduce this to 1 line through the use of procedure macros to inline the use of validations.

The example from the Kiosk set is shown below.

// NEW
pub async fn get(db: InternalDb, id: &str, session: Session) -> Convert<Kiosk> {
    check_permissions!(session.clone(), Action::FetchKiosk);
    Kiosk::fetch_by_id(id, session, &db.0).await.into()
}
// OLD
pub async fn get(
    conn: Connection<Db>,
    id: &str,
    cookies: &CookieJar<'_>,
) -> Result<Json<Kiosk>, Error> {
    let db = conn.into_inner();

    let session = cookie_status_wrapper(&db, cookies).await?;
    check_permissions!(session.clone(), Action::FetchEmployee);

    match Kiosk::fetch_by_id(id, session, &db).await {
        Ok(employee) => Ok(Json(employee)),
        Err(err) => Err(ErrorResponse::db_err(err)),
    }
}

@bennjii bennjii self-assigned this Feb 5, 2024
@bennjii bennjii merged commit d76cf0a into master Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant