dxps / fullstack-rust-axum-dioxus-rwa

A RealWorld app implementation as a fullstack Rust project using Axum (back-end) and Dioxus (front-end).
MIT License
80 stars 6 forks source link

Simplify output DTOs with `Json(json!({ "some": some })` #4

Closed dxps closed 1 year ago

dxps commented 1 year ago

Use the approach that exists in get_user_profile handler. This means:

dxps commented 1 year ago

To see the clear difference in a concrete example (of get_current_user handler), basically, this means:

  1. Instead of having defined UserOutDTO and its UserOutDTOUserAttrs child, and respond like this:
    let out = UserOutDTO {
        user: UserOutDTOUserAttrs {
            email: entry.user.email,
            token: None,
            username: entry.user.username,
            bio: entry.user.bio,
            image: entry.user.image,
        },
    };
    (StatusCode::OK, Json(serde_json::to_value(out).unwrap()))
  2. There is just one UserOutDTO, and the response looks like this:
    respond_with_user_dto(
        email,
        token,
        username,
        bio,
        image,
    ),

    and that respond_with_user_dto function includes:

    let dto = UserOutDTO {
        email,
        token,
        username,
        bio,
        image,
    };
    (StatusCode::OK, Json(json!({ "user": dto })))

:bulb: Idea: Another approach is to have the User and UserEntry structs to implement Axum's IntoResponse trait, and thus only the instances of these structs could be returned and the rest is Axum's logic.