From 322f55d202e6417054270e441a3451985ebd389c Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Wed, 13 Nov 2024 22:49:06 +0100 Subject: [PATCH] allow filtering users by year --- database/sql/user.sql | 10 +++ database/user.sql.go | 39 +++++++++- handlers/admin.go | 8 ++- templates/admin.templ | 23 ++++-- templates/admin_templ.go | 145 ++++++++++++++++++++++++++------------ templates/layout.templ | 6 ++ templates/layout_templ.go | 6 ++ 7 files changed, 186 insertions(+), 51 deletions(-) diff --git a/database/sql/user.sql b/database/sql/user.sql index 55690da..6a600e1 100644 --- a/database/sql/user.sql +++ b/database/sql/user.sql @@ -38,10 +38,20 @@ where case when @search::text = '' then true else kthid = @search or first_name ~* @search or family_name ~* @search end +and case + when @year::text = '' then true + else @year = year_tag +end order by kthid limit $1 offset $2; +-- name: GetAllYears :many +select distinct year_tag +from users +where year_tag != '' +order by year_tag; + -- name: UserSetMemberTo :exec update users set member_to = $2 diff --git a/database/user.sql.go b/database/user.sql.go index 8e22fd9..2670c05 100644 --- a/database/user.sql.go +++ b/database/user.sql.go @@ -61,6 +61,33 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error { return err } +const getAllYears = `-- name: GetAllYears :many +select distinct year_tag +from users +where year_tag != '' +order by year_tag +` + +func (q *Queries) GetAllYears(ctx context.Context) ([]string, error) { + rows, err := q.db.Query(ctx, getAllYears) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var year_tag string + if err := rows.Scan(&year_tag); err != nil { + return nil, err + } + items = append(items, year_tag) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getSession = `-- name: GetSession :one update sessions set last_used_at = now() @@ -107,6 +134,10 @@ where case when $3::text = '' then true else kthid = $3 or first_name ~* $3 or family_name ~* $3 end +and case + when $4::text = '' then true + else $4 = year_tag +end order by kthid limit $1 offset $2 @@ -116,10 +147,16 @@ type ListUsersParams struct { Limit int32 Offset int32 Search string + Year string } func (q *Queries) ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error) { - rows, err := q.db.Query(ctx, listUsers, arg.Limit, arg.Offset, arg.Search) + rows, err := q.db.Query(ctx, listUsers, + arg.Limit, + arg.Offset, + arg.Search, + arg.Year, + ) if err != nil { return nil, err } diff --git a/handlers/admin.go b/handlers/admin.go index bf9ad3a..55e70f8 100644 --- a/handlers/admin.go +++ b/handlers/admin.go @@ -88,6 +88,7 @@ func adminUsersForm(s *service.Service, w http.ResponseWriter, r *http.Request) search := r.FormValue("search") offsetStr := r.FormValue("offset") offset, err := strconv.ParseInt(offsetStr, 10, 32) + year := r.FormValue("year") if err != nil && offsetStr != "" { return httputil.BadRequest("Invalid int for offset") } @@ -95,6 +96,7 @@ func adminUsersForm(s *service.Service, w http.ResponseWriter, r *http.Request) Search: search, Limit: 21, Offset: int32(offset), + Year: year, }) if err != nil { return err @@ -104,7 +106,11 @@ func adminUsersForm(s *service.Service, w http.ResponseWriter, r *http.Request) users = users[0:20:20] more = true } - return templates.MemberList(service.DBUsersToModel(users), search, int(offset), more) + years, err := s.DB.GetAllYears(r.Context()) + if err != nil { + return err + } + return templates.MemberList(service.DBUsersToModel(users), search, int(offset), more, years, year) } func invites(s *service.Service, w http.ResponseWriter, r *http.Request) httputil.ToResponse { diff --git a/templates/admin.templ b/templates/admin.templ index 685c686..d2721da 100644 --- a/templates/admin.templ +++ b/templates/admin.templ @@ -22,7 +22,7 @@ templ Members() { @AdminPage() {
- @MemberList([]models.User{}, "", 0, false) + @MemberList([]models.User{}, "", 0, false, []string{}, "")
@uploadForm() @UploadStatus(false) @@ -30,7 +30,7 @@ templ Members() { } } -templ MemberList(users []models.User, search string, offset int, more bool) { +templ MemberList(users []models.User, search string, offset int, more bool, years []string, selectedYear string) {
Username @@ -62,11 +62,26 @@ templ MemberList(users []models.User, search string, offset int, more bool) { autofocus } /> +