code: creation function now takes all fields

This commit is contained in:
Gabor Körber 2024-07-03 16:53:24 +02:00
parent bce450f744
commit e77fc115b6
2 changed files with 37 additions and 2 deletions

View File

@ -63,4 +63,6 @@ These errors should encompass:
- Wrong Data / Not Authorized and other 400 base - Wrong Data / Not Authorized and other 400 base
- Not Found Error: for 404s - Not Found Error: for 404s
- Internal Errors: for 500s - Internal Errors: for 500s
Repository functions also need to be redesigned to be Results.

View File

@ -34,7 +34,16 @@ impl AdminRepository for UserRepository {
name: "User", name: "User",
lookup_key: "id", lookup_key: "id",
display_list: &["id", "username"], display_list: &["id", "username"],
fields: &["username", "description"], fields: &[
"username",
"first_name",
"last_name",
"email",
"is_staff",
"is_active",
"is_superuser",
],
// fields_readonly: &["last_login", "date_joined"]
} }
.into() .into()
} }
@ -93,6 +102,30 @@ impl AdminRepository for UserRepository {
user.password = Set(value.as_str().unwrap().to_owned()); user.password = Set(value.as_str().unwrap().to_owned());
} }
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));
}
if let Ok(user) = user.insert(&self.connection).await { if let Ok(user) = user.insert(&self.connection).await {
let id = user.id.to_string(); let id = user.id.to_string();
return Some(model.build_item(&*id, serde_json::to_value(&user).unwrap())); return Some(model.build_item(&*id, serde_json::to_value(&user).unwrap()));