refactor: using match

This commit is contained in:
Gabor Körber 2024-07-05 13:38:30 +02:00
parent e77fc115b6
commit e19d5db7c6

View File

@ -98,32 +98,29 @@ impl AdminRepository for UserRepository {
..Default::default()
};
if let Some(value) = data.get("password") {
user.password = Set(value.as_str().unwrap().to_owned());
}
let keys = [
"password",
"first_name",
"last_name",
"email",
"is_staff",
"is_active",
"is_superuser",
];
if let Some(value) = data.get("first_name") {
user.first_name = Set(value.as_str().map(|s| s.to_owned()));
}
if let Some(value) = data.get("last_name") {
user.last_name = Set(value.as_str().map(|s| s.to_owned()));
}
if let Some(value) = data.get("email") {
user.email = Set(value.as_str().map(|s| s.to_owned()));
}
if let Some(value) = data.get("is_staff") {
user.is_staff = Set(value.as_bool().unwrap_or(false));
}
if let Some(value) = data.get("is_active") {
user.is_active = Set(value.as_bool().unwrap_or(false));
}
if let Some(value) = data.get("is_superuser") {
user.is_superuser = Set(value.as_bool().unwrap_or(false));
for key in &keys {
if let Some(value) = data.get(*key) {
match *key {
"password" => user.password = Set(value.as_str().unwrap().to_owned()),
"first_name" => user.first_name = Set(value.as_str().map(|s| s.to_owned())),
"last_name" => user.last_name = Set(value.as_str().map(|s| s.to_owned())),
"email" => user.email = Set(value.as_str().map(|s| s.to_owned())),
"is_staff" => user.is_staff = Set(value.as_bool().unwrap_or(false)),
"is_active" => user.is_active = Set(value.as_bool().unwrap_or(false)),
"is_superuser" => user.is_superuser = Set(value.as_bool().unwrap_or(false)),
_ => (),
}
}
}
if let Ok(user) = user.insert(&self.connection).await {