From 5d3ccc7184a4e8ed3494782711481f19ae6a7988 Mon Sep 17 00:00:00 2001 From: Tim Nordenfur Date: Sat, 12 Jun 2021 23:43:04 +0200 Subject: [PATCH] combined Invites.Count and Invites.CountActive --- roomdb/interface.go | 7 ++----- roomdb/sqlite/invites.go | 16 +++++----------- web/handlers/admin/dashboard.go | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/roomdb/interface.go b/roomdb/interface.go index e19ce73..05de72e 100644 --- a/roomdb/interface.go +++ b/roomdb/interface.go @@ -158,11 +158,8 @@ type InvitesService interface { // List returns a list of all the valid invites List(ctx context.Context) ([]Invite, error) - // Count returns the total number of invites. - Count(context.Context) (uint, error) - - // CountActive returns the total number of active invites. - CountActive(context.Context) (uint, error) + // Count returns the total number of invites, optionally excluding inactive invites + Count(ctx context.Context, onlyActive bool) (uint, error) // Revoke removes a active invite and invalidates it for future use. Revoke(ctx context.Context, id int64) error diff --git a/roomdb/sqlite/invites.go b/roomdb/sqlite/invites.go index 9623645..d02ccb8 100644 --- a/roomdb/sqlite/invites.go +++ b/roomdb/sqlite/invites.go @@ -250,18 +250,12 @@ func (i Invites) List(ctx context.Context) ([]roomdb.Invite, error) { return invs, nil } -func (i Invites) Count(ctx context.Context) (uint, error) { - count, err := models.Invites().Count(ctx, i.db) - if err != nil { - return 0, err +func (i Invites) Count(ctx context.Context, onlyActive bool) (uint, error) { + queryMod := qm.Where("1") + if onlyActive { + queryMod = qm.Where("active = true") } - return uint(count), nil -} - -func (i Invites) CountActive(ctx context.Context) (uint, error) { - count, err := models.Invites( - qm.Where("active = true"), - ).Count(ctx, i.db) + count, err := models.Invites(queryMod).Count(ctx, i.db) if err != nil { return 0, err } diff --git a/web/handlers/admin/dashboard.go b/web/handlers/admin/dashboard.go index 4ea3fe1..b790f8b 100644 --- a/web/handlers/admin/dashboard.go +++ b/web/handlers/admin/dashboard.go @@ -77,7 +77,7 @@ func (h dashboardHandler) overview(w http.ResponseWriter, req *http.Request) (in return nil, fmt.Errorf("failed to count members: %w", err) } - inviteCount, err := h.dbs.Invites.CountActive(ctx) + inviteCount, err := h.dbs.Invites.Count(ctx, true) if err != nil { return nil, fmt.Errorf("failed to count active invites: %w", err) }