• 9 Posts
  • 7 Comments
Joined 1 year ago
cake
Cake day: June 25th, 2023

help-circle




  • I managed to get this working, but there has to be a better way. How else could I write this?

      pub async fn insert_or_return_user(
            db: &DbConn,
            partial_user: Auth0UserPart,
        ) -> Result {
            let user = users::ActiveModel {
                email: Set(partial_user.email.to_owned()),
                email_verified: Set(partial_user.email_verified.to_owned()),
                auth0_sub: Set(partial_user.sub.to_owned()),
                ..Default::default()
            };
    
            let result = user.clone().insert(db).await;
    
            match result {
                Ok(u) => {
                    println!("{u:#?}");
                    Ok(u.try_into_model().unwrap() as UsersModel)
                }
                Err(error) => {
                    let user = Users::find()
                        .filter(users::Column::Auth0Sub.eq(&partial_user.sub))
                        .one(db)
                        .await?;
    
                    Ok(user.unwrap() as UsersModel)
                }
            }
        }
    






  • Also, move out special types to types.rs, error types to errors.rs to keep the area with the actual algorithms more clear.

    Ok this is totally something my code base needs. Very actionable feedback.

    And yeah that's one of the things I love about rust; it will tell me everywhere things are out of wack. It's such a different experience from back when I had large JavaScript code bases. Make changes and pray lol.