Add edit and new for users
This commit is contained in:
parent
57af399ace
commit
cf9a7cf33f
12 changed files with 229 additions and 88 deletions
|
|
@ -24,7 +24,16 @@ use crate::{
|
|||
#[derive(Template, WebTemplate)]
|
||||
#[template(path = "users/index.html")]
|
||||
struct UsersIndexTemplate {
|
||||
user_with_books_number: Vec<(user::Model, usize, usize)>,
|
||||
user_with_books_number: Vec<UserWithBookNumber>,
|
||||
}
|
||||
|
||||
pub struct UserWithBookNumber {
|
||||
/// the user model
|
||||
pub user: user::Model,
|
||||
/// the number of books owned by this user
|
||||
pub owner_book_number: usize,
|
||||
/// the number of books borrowed by this user
|
||||
pub borrowed_book_number: usize,
|
||||
}
|
||||
|
||||
pub async fn index(
|
||||
|
|
@ -40,7 +49,7 @@ pub async fn index(
|
|||
.await
|
||||
.context(BookSnafu)?;
|
||||
|
||||
let mut result: Vec<(user::Model, usize, usize)> = vec![];
|
||||
let mut result: Vec<UserWithBookNumber> = Vec::with_capacity(users.len());
|
||||
|
||||
let mut owner_books: HashMap<i32, usize> = HashMap::new();
|
||||
let mut borrowed_books: HashMap<i32, usize> = HashMap::new();
|
||||
|
|
@ -56,7 +65,11 @@ pub async fn index(
|
|||
let owner_books_size = owner_books.get(&user.id).unwrap_or(&0);
|
||||
let borrowed_books_size = borrowed_books.get(&user.id).unwrap_or(&0);
|
||||
|
||||
result.push((user, *owner_books_size, *borrowed_books_size));
|
||||
result.push(UserWithBookNumber {
|
||||
user,
|
||||
owner_book_number: *owner_books_size,
|
||||
borrowed_book_number: *borrowed_books_size,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(UsersIndexTemplate {
|
||||
|
|
@ -73,7 +86,7 @@ pub async fn create(
|
|||
State(state): State<AppState>,
|
||||
Form(form): Form<UserForm>,
|
||||
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
||||
let _user = UserOperator::new(state)
|
||||
let _ = UserOperator::new(state)
|
||||
.create(form)
|
||||
.await
|
||||
.context(UserSnafu)?;
|
||||
|
|
@ -81,6 +94,19 @@ pub async fn create(
|
|||
Ok(Redirect::to("/users"))
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<i32>,
|
||||
Form(form): Form<UserForm>,
|
||||
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
||||
let _ = UserOperator::new(state)
|
||||
.update(id, form)
|
||||
.await
|
||||
.context(UserSnafu)?;
|
||||
|
||||
Ok(Redirect::to("/users"))
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<i32>,
|
||||
|
|
@ -92,3 +118,29 @@ pub async fn delete(
|
|||
|
||||
Ok(Redirect::to("/users"))
|
||||
}
|
||||
|
||||
#[derive(Template, WebTemplate)]
|
||||
#[template(path = "users/edit.html")]
|
||||
struct EditTemplate {
|
||||
user: user::Model,
|
||||
}
|
||||
|
||||
pub async fn edit(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
||||
let user = UserOperator::new(state)
|
||||
.find_by_id(id)
|
||||
.await
|
||||
.context(UserSnafu)?;
|
||||
|
||||
Ok(EditTemplate { user })
|
||||
}
|
||||
|
||||
#[derive(Template, WebTemplate)]
|
||||
#[template(path = "users/new.html")]
|
||||
struct NewTemplate {}
|
||||
|
||||
pub async fn new() -> impl axum::response::IntoResponse {
|
||||
NewTemplate {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue