combined Invites.Count and Invites.CountActive

This commit is contained in:
Tim Nordenfur 2021-06-12 23:43:04 +02:00
parent 922fa34302
commit 5d3ccc7184
3 changed files with 8 additions and 17 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}