Any good ways to handle object contains option<T> on updating? #288
-
pub struct UpdateUserInfo {
pub address: Option<String>,
pub role_id: Option<i32>,
}
pub async fn update_user(
Path(id): Path<i32>,
c: JwtClaims,
Json(payload): Json<UpdateUserInfo>,
) -> ... {
...
let mut v: Vec<db::user::SetParam> = vec![];
if let Some(x) = payload.address{
v.push(db::user::address::set(x));
}
... // for each filed
client
.user()
.update(db::user::id::equals(id), v)
.select(user_out::select())
.exec()
.await?
} Is there a better solution than this? I am working on writing a proc_macro_attribute also. |
Beta Was this translation helpful? Give feedback.
Answered by
Brendonovich
Mar 23, 2023
Replies: 2 comments 34 replies
-
Personally I like this approach: pub struct UpdateUserInfo {
pub address: Option<String>,
pub role_id: Option<i32>,
}
let payload: UpdateUserInfo = ...;
let values = [
payload.address.map(db::user::address::set)
].into_iter().flatten().collect();
client
.user()
.update(db::user::id::equals(id), v)
.select(user_out::select())
.exec()
.await? I've also thought about giving each module a |
Beta Was this translation helpful? Give feedback.
13 replies
-
|
Beta Was this translation helpful? Give feedback.
21 replies
Answer selected by
Dennis-Zhang-SH
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
model::partial!
is now available onmain
if you want to try it out!