Add edit and new for users

This commit is contained in:
gabatxo1312
2026-01-29 23:52:55 +01:00
parent 57af399ace
commit cf9a7cf33f
12 changed files with 229 additions and 88 deletions
+16
View File
@@ -1,5 +1,7 @@
use crate::models::book::BookOperator;
use crate::routes::user::UserForm;
use crate::state::AppState;
use crate::state::error::UserSnafu;
use sea_orm::ActiveValue::Set;
use sea_orm::DeleteResult;
use sea_orm::entity::prelude::*;
@@ -74,6 +76,20 @@ impl UserOperator {
user.insert(&self.state.db).await.context(DBSnafu)
}
pub async fn update(&self, id: i32, form: UserForm) -> Result<Model, UserError> {
let user_by_id = Self::find_by_id(&self, id).await.context(UserSnafu);
if let Ok(user) = user_by_id {
let mut user: ActiveModel = user.into();
user.name = Set(form.name);
user.update(&self.state.db).await.context(DBSnafu)
} else {
Err(UserError::NotFound { id })
}
}
pub async fn delete(&self, user_id: i32) -> Result<DeleteResult, UserError> {
let user: Option<Model> = Entity::find_by_id(user_id)
.one(&self.state.db)