Closed dxps closed 1 year ago
To see the clear difference in a concrete example (of get_current_user
handler), basically, this means:
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()))
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.
Use the approach that exists in
get_user_profile
handler. This means:Json(json!({ "profile": profile })))
UserProfileDTO
is one single struct (instead of two structs, like the case ofUserOutDTO
and itsUserOutDTOUserAttrs
child).