improving depot code for user and static repo examples
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use log::debug;
|
||||
use log::{debug, warn};
|
||||
use rear::depot::prelude::*;
|
||||
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, ModelTrait, Set};
|
||||
use serde_json::Value;
|
||||
@@ -136,8 +136,8 @@ impl DepotRepository for UserRepository {
|
||||
let user: Option<entity::user::Model> = entity::User::find_by_id(id)
|
||||
.one(&self.connection)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut user: entity::user::ActiveModel = user.unwrap().into();
|
||||
.map_err(|e| RepositoryError::DatabaseError(Box::new(e)))?;
|
||||
let mut user: entity::user::ActiveModel = user.ok_or(RepositoryError::ItemNotFound)?.into();
|
||||
|
||||
// should we really allow username change?
|
||||
if let Some(value) = data.get("username") {
|
||||
@@ -170,14 +170,18 @@ impl DepotRepository for UserRepository {
|
||||
}
|
||||
|
||||
// update
|
||||
if let Ok(user) = user.update(&self.connection).await {
|
||||
let id = user.id.to_string();
|
||||
return Ok(RepositoryResponse::ItemOnly(
|
||||
model.build_item(&*id, serde_json::to_value(&user).unwrap()),
|
||||
));
|
||||
match user.update(&self.connection).await {
|
||||
Ok(user) => {
|
||||
let id = user.id.to_string();
|
||||
return Ok(RepositoryResponse::ItemOnly(
|
||||
model.build_item(&*id, serde_json::to_value(&user).unwrap()),
|
||||
));
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Error updating user");
|
||||
return Err(RepositoryError::DatabaseError(Box::new(err)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(RepositoryResponse::NoItem)
|
||||
}
|
||||
|
||||
async fn replace(
|
||||
@@ -197,6 +201,7 @@ impl DepotRepository for UserRepository {
|
||||
.unwrap();
|
||||
if let Some(user) = user {
|
||||
let delete_result = user.delete(&self.connection).await.unwrap();
|
||||
// .ok_or(RepositoryError::DatabaseError(Box::new(err)))?;
|
||||
debug!("deleted rows: {}", delete_result.rows_affected);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user