Repository: go-gitea/gitea
File: models/user/user.go (Go)

Code before:
// IsLastAdminUser check whether user is the last admin
func IsLastAdminUser(ctx context.Context, user *User) bool {
	if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: optional.Some(true)}) <= 1 {
		return true
	}
	return false
}

// CountUserFilter represent optional filters for CountUsers
type CountUserFilter struct {
	LastLoginSince *int64
	IsAdmin        optional.Option[bool]
	IsActive       optional.Option[bool]
}

// HasUsers returns true if any user exists in the database.
// It performs a much more efficient check than counting all users.
func HasUsers(ctx context.Context) (bool, error) {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	exists, err := sess.Where(cond).Limit(1).Exist(new(User))
	if err != nil {
		return false, fmt.Errorf("error checking user existence: %w", err)
	}

	return exists, nil
}

// CountUsers returns number of users.
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
	return countUsers(ctx, opts)
}

func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	if opts != nil {
		if opts.LastLoginSince != nil {
			cond = cond.And(builder.Gte{"last_login_unix": *opts.LastLoginSince})
		}

		if opts.IsAdmin.Has() {
			cond = cond.And(builder.Eq{"is_admin": opts.IsAdmin.Value()})
		}

		if opts.IsActive.Has() {
			cond = cond.And(builder.Eq{"is_active": opts.IsActive.Value()})

Review comment (bug):
I maybe incorrect, but from looking at the quickstart [docs](https://pkg.go.dev/xorm.io/xorm#section-readme) could this be simplified to the following?

```go
has, err := sess.Exist(&User{
		Type: UserTypeIndividual,
	})

if err != nil {
	return false, fmt.Errorf("error checking user existence: %w", err)
}
```

It looks like `Exists()` will add `LIMIT 1` to the SQL statement 

Code after:
// IsLastAdminUser check whether user is the last admin
func IsLastAdminUser(ctx context.Context, user *User) bool {
	if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: optional.Some(true)}) <= 1 {
		return true
	}
	return false
}

// CountUserFilter represent optional filters for CountUsers
type CountUserFilter struct {
	LastLoginSince *int64
	IsAdmin        optional.Option[bool]
	IsActive       optional.Option[bool]
}

// HasUsers checks whether there are any users in the database, or only one user exists.
func HasUsers(ctx context.Context) (ret struct {
	HasAnyUser, HasOnlyOneUser bool
}, err error,
) {
	res, err := db.GetEngine(ctx).Table(&User{}).Cols("id").Limit(2).Query()
	if err != nil {
		return ret, fmt.Errorf("error checking user existence: %w", err)
	}
	ret.HasAnyUser = len(res) != 0
	ret.HasOnlyOneUser = len(res) == 1
	return ret, nil
}

// CountUsers returns number of users.
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
	return countUsers(ctx, opts)
}

func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	if opts != nil {
		if opts.LastLoginSince != nil {
			cond = cond.And(builder.Gte{"last_login_unix": *opts.LastLoginSince})
		}

		if opts.IsAdmin.Has() {
			cond = cond.And(builder.Eq{"is_admin": opts.IsAdmin.Value()})
		}

		if opts.IsActive.Has() {
			cond = cond.And(builder.Eq{"is_active": opts.IsActive.Value()})
		}