feat: tenants querying
All checks were successful
arcad/emissary/pipeline/pr-master This commit looks good
All checks were successful
arcad/emissary/pipeline/pr-master This commit looks good
This commit is contained in:
@ -15,6 +15,88 @@ type TenantRepository struct {
|
||||
repository
|
||||
}
|
||||
|
||||
// Query implements datastore.TenantRepository.
|
||||
func (r *TenantRepository) Query(ctx context.Context, opts ...datastore.TenantQueryOptionFunc) ([]*datastore.Tenant, int, error) {
|
||||
options := &datastore.TenantQueryOptions{}
|
||||
for _, fn := range opts {
|
||||
fn(options)
|
||||
}
|
||||
|
||||
tenants := make([]*datastore.Tenant, 0)
|
||||
count := 0
|
||||
|
||||
err := r.withTxRetry(ctx, func(tx *sql.Tx) error {
|
||||
query := `SELECT id, label, created_at, updated_at FROM tenants`
|
||||
|
||||
limit := 10
|
||||
if options.Limit != nil {
|
||||
limit = *options.Limit
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if options.Offset != nil {
|
||||
offset = *options.Offset
|
||||
}
|
||||
|
||||
filters := ""
|
||||
paramIndex := 3
|
||||
args := []any{offset, limit}
|
||||
|
||||
if options.IDs != nil && len(options.IDs) > 0 {
|
||||
filter, newArgs, newParamIndex := inFilter("id", paramIndex, options.IDs)
|
||||
filters += filter
|
||||
paramIndex = newParamIndex
|
||||
args = append(args, newArgs...)
|
||||
}
|
||||
|
||||
if filters != "" {
|
||||
filters = ` WHERE ` + filters
|
||||
}
|
||||
|
||||
query += filters + ` LIMIT $2 OFFSET $1`
|
||||
|
||||
logger.Debug(ctx, "executing query", logger.F("query", query), logger.F("args", args))
|
||||
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := rows.Close(); err != nil {
|
||||
err = errors.WithStack(err)
|
||||
logger.Error(ctx, "could not close rows", logger.CapturedE(err))
|
||||
}
|
||||
}()
|
||||
|
||||
for rows.Next() {
|
||||
tenant := &datastore.Tenant{}
|
||||
|
||||
if err := rows.Scan(&tenant.ID, &tenant.Label, &tenant.CreatedAt, &tenant.UpdatedAt); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
tenants = append(tenants, tenant)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
row := tx.QueryRowContext(ctx, `SELECT count(id) FROM tenants `+filters, args...)
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return tenants, count, nil
|
||||
}
|
||||
|
||||
// Create implements datastore.TenantRepository.
|
||||
func (r *TenantRepository) Create(ctx context.Context, label string) (*datastore.Tenant, error) {
|
||||
var tenant datastore.Tenant
|
||||
|
@ -7,6 +7,8 @@ type TenantRepository interface {
|
||||
Get(ctx context.Context, id TenantID) (*Tenant, error)
|
||||
Update(ctx context.Context, id TenantID, updates ...TenantUpdateOptionFunc) (*Tenant, error)
|
||||
Delete(ctx context.Context, id TenantID) error
|
||||
|
||||
Query(ctx context.Context, opts ...TenantQueryOptionFunc) ([]*Tenant, int, error)
|
||||
}
|
||||
|
||||
type TenantUpdateOptionFunc func(*TenantUpdateOptions)
|
||||
@ -20,3 +22,29 @@ func WithTenantUpdateLabel(label string) TenantUpdateOptionFunc {
|
||||
opts.Label = &label
|
||||
}
|
||||
}
|
||||
|
||||
type TenantQueryOptionFunc func(*TenantQueryOptions)
|
||||
|
||||
type TenantQueryOptions struct {
|
||||
Limit *int
|
||||
Offset *int
|
||||
IDs []TenantID
|
||||
}
|
||||
|
||||
func WithTenantQueryLimit(limit int) TenantQueryOptionFunc {
|
||||
return func(opts *TenantQueryOptions) {
|
||||
opts.Limit = &limit
|
||||
}
|
||||
}
|
||||
|
||||
func WithTenantQueryOffset(offset int) TenantQueryOptionFunc {
|
||||
return func(opts *TenantQueryOptions) {
|
||||
opts.Offset = &offset
|
||||
}
|
||||
}
|
||||
|
||||
func WithTenantQueryID(ids ...TenantID) TenantQueryOptionFunc {
|
||||
return func(opts *TenantQueryOptions) {
|
||||
opts.IDs = ids
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user