2026-01-23 13:56:30 +01:00
|
|
|
use askama::Template;
|
|
|
|
|
use askama_web::WebTemplate;
|
2026-01-27 01:33:21 +01:00
|
|
|
use axum::{
|
|
|
|
|
Form,
|
|
|
|
|
extract::{Path, State},
|
|
|
|
|
response::Redirect,
|
|
|
|
|
};
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use snafu::prelude::*;
|
2026-01-23 13:56:30 +01:00
|
|
|
|
2026-01-27 01:33:21 +01:00
|
|
|
use crate::{
|
|
|
|
|
models::user::{self, UserOperator},
|
|
|
|
|
state::{
|
|
|
|
|
AppState,
|
|
|
|
|
error::{AppStateError, UserSnafu},
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-01-23 13:56:30 +01:00
|
|
|
|
|
|
|
|
#[derive(Template, WebTemplate)]
|
|
|
|
|
#[template(path = "users/index.html")]
|
2026-01-27 01:33:21 +01:00
|
|
|
struct UsersIndexTemplate {
|
|
|
|
|
users: Vec<user::Model>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn index(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
|
|
|
|
let users = UserOperator::new(state).list().await.context(UserSnafu)?;
|
|
|
|
|
|
|
|
|
|
Ok(UsersIndexTemplate { users })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct UserForm {
|
|
|
|
|
pub name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn create(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Form(form): Form<UserForm>,
|
|
|
|
|
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
|
|
|
|
let _user = UserOperator::new(state)
|
|
|
|
|
.create(form)
|
|
|
|
|
.await
|
|
|
|
|
.context(UserSnafu)?;
|
|
|
|
|
|
|
|
|
|
Ok(Redirect::to("/users"))
|
|
|
|
|
}
|
2026-01-23 13:56:30 +01:00
|
|
|
|
2026-01-27 01:33:21 +01:00
|
|
|
pub async fn delete(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Path(id): Path<i32>,
|
|
|
|
|
) -> Result<impl axum::response::IntoResponse, AppStateError> {
|
|
|
|
|
let _user = UserOperator::new(state)
|
|
|
|
|
.delete(id)
|
|
|
|
|
.await
|
|
|
|
|
.context(UserSnafu)?;
|
2026-01-23 13:56:30 +01:00
|
|
|
|
2026-01-27 01:33:21 +01:00
|
|
|
Ok(Redirect::to("/users"))
|
2026-01-23 13:56:30 +01:00
|
|
|
}
|