allow sorting of user

This commit is contained in:
Philipp Hofer 2025-02-11 20:23:18 +01:00
parent 8917629613
commit da793fec2d
2 changed files with 26 additions and 13 deletions

View File

@ -591,18 +591,27 @@ WHERE lower(name)=?
} }
pub async fn all(db: &SqlitePool) -> Vec<Self> { pub async fn all(db: &SqlitePool) -> Vec<Self> {
sqlx::query_as!( Self::all_with_order(db, "last_access", true).await
Self, }
"
SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token pub async fn all_with_order(db: &SqlitePool, sort: &str, asc: bool) -> Vec<Self> {
FROM user let mut query = format!(
WHERE deleted = 0
ORDER BY last_access DESC
" "
) SELECT id, name, pw, deleted, last_access, dob, weight, sex, member_since_date, birthdate, mail, nickname, notes, phone, address, family_id, user_token
.fetch_all(db) FROM user
.await WHERE deleted = 0
.unwrap() ORDER BY {}
",
sort
);
if !asc {
query.push_str(" DESC");
}
sqlx::query_as::<_, User>(&query)
.fetch_all(db)
.await
.unwrap()
} }
pub async fn all_with_role(db: &SqlitePool, role: &Role) -> Vec<Self> { pub async fn all_with_role(db: &SqlitePool, role: &Role) -> Vec<Self> {

View File

@ -43,13 +43,17 @@ impl<'r> FromRequest<'r> for Referer {
} }
} }
#[get("/user")] #[get("/user?<sort>&<asc>")]
async fn index( async fn index(
db: &State<SqlitePool>, db: &State<SqlitePool>,
user: VorstandUser, user: VorstandUser,
flash: Option<FlashMessage<'_>>, flash: Option<FlashMessage<'_>>,
sort: Option<String>,
asc: bool,
) -> Template { ) -> Template {
let user_futures: Vec<_> = User::all(db) let sort_column = sort.unwrap_or_else(|| "last_access".to_string());
let user_futures: Vec<_> = User::all_with_order(db, &sort_column, asc)
.await .await
.into_iter() .into_iter()
.map(|u| async move { UserWithRolesAndMembershipPdf::from_user(db, u).await }) .map(|u| async move { UserWithRolesAndMembershipPdf::from_user(db, u).await })