Skip to content

Commit 92f1f26

Browse files
committed
refactor(api): introduce a generic query option
1 parent 62ddabb commit 92f1f26

File tree

9 files changed

+266
-35
lines changed

9 files changed

+266
-35
lines changed

api/services/device.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *service) ListDevices(ctx context.Context, req *requests.DeviceList) ([]
4444
}
4545

4646
if req.TenantID != "" {
47-
ns, err := s.store.NamespaceGet(ctx, req.TenantID, s.store.Options().CountAcceptedDevices())
47+
ns, err := s.store.NamespaceGet(ctx, req.TenantID)
4848
if err != nil {
4949
return nil, 0, NewErrNamespaceNotFound(req.TenantID, err)
5050
}
@@ -184,7 +184,7 @@ func (s *service) OfflineDevice(ctx context.Context, uid models.UID) error {
184184

185185
// UpdateDeviceStatus updates the device status.
186186
func (s *service) UpdateDeviceStatus(ctx context.Context, tenant string, uid models.UID, status models.DeviceStatus) error {
187-
namespace, err := s.store.NamespaceGet(ctx, tenant, s.store.Options().CountAcceptedDevices())
187+
namespace, err := s.store.NamespaceGet(ctx, tenant)
188188
if err != nil {
189189
return NewErrNamespaceNotFound(tenant, err)
190190
}

api/services/member.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (s *service) AddNamespaceMember(ctx context.Context, req *requests.Namespac
105105
}
106106
}
107107

108-
return s.store.NamespaceGet(ctx, req.TenantID, s.store.Options().CountAcceptedDevices(), s.store.Options().EnrichMembersData())
108+
return s.store.NamespaceGet(ctx, req.TenantID)
109109
}
110110

111111
// addMember returns a transaction callback that adds a member and sends an invite if the instance is cloud.
@@ -228,7 +228,7 @@ func (s *service) RemoveNamespaceMember(ctx context.Context, req *requests.Names
228228
Error("failed to uncache the token")
229229
}
230230

231-
return s.store.NamespaceGet(ctx, req.TenantID, s.store.Options().CountAcceptedDevices(), s.store.Options().EnrichMembersData())
231+
return s.store.NamespaceGet(ctx, req.TenantID)
232232
}
233233

234234
func (s *service) LeaveNamespace(ctx context.Context, req *requests.LeaveNamespace) (*models.UserAuthResponse, error) {

api/services/namespace.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *service) CreateNamespace(ctx context.Context, req *requests.NamespaceCr
9898
}
9999

100100
func (s *service) ListNamespaces(ctx context.Context, req *requests.NamespaceList) ([]models.Namespace, int, error) {
101-
namespaces, count, err := s.store.NamespaceList(ctx, req.Paginator, req.Filters, s.store.Options().CountAcceptedDevices(), s.store.Options().EnrichMembersData())
101+
namespaces, count, err := s.store.NamespaceList(ctx, s.store.Options().Filter(req.Filters), s.store.Options().Paginate(req.Paginator))
102102
if err != nil {
103103
return nil, 0, NewErrNamespaceList(err)
104104
}
@@ -112,7 +112,7 @@ func (s *service) ListNamespaces(ctx context.Context, req *requests.NamespaceLis
112112
//
113113
// GetNamespace returns a models.Namespace and an error. When error is not nil, the models.Namespace is nil.
114114
func (s *service) GetNamespace(ctx context.Context, tenantID string) (*models.Namespace, error) {
115-
namespace, err := s.store.NamespaceGet(ctx, tenantID, s.store.Options().CountAcceptedDevices(), s.store.Options().EnrichMembersData())
115+
namespace, err := s.store.NamespaceGet(ctx, tenantID)
116116
if err != nil || namespace == nil {
117117
return nil, NewErrNamespaceNotFound(tenantID, err)
118118
}
@@ -127,7 +127,7 @@ func (s *service) GetNamespace(ctx context.Context, tenantID string) (*models.Na
127127
// When cloud and billing is enabled, it will try to delete the namespace's billing information from the billing
128128
// service if it exists.
129129
func (s *service) DeleteNamespace(ctx context.Context, tenantID string) error {
130-
ns, err := s.store.NamespaceGet(ctx, tenantID, s.store.Options().CountAcceptedDevices())
130+
ns, err := s.store.NamespaceGet(ctx, tenantID)
131131
if err != nil {
132132
return NewErrNamespaceNotFound(tenantID, err)
133133
}
@@ -161,7 +161,7 @@ func (s *service) EditNamespace(ctx context.Context, req *requests.NamespaceEdit
161161
}
162162
}
163163

164-
return s.store.NamespaceGet(ctx, req.Tenant, s.store.Options().CountAcceptedDevices(), s.store.Options().EnrichMembersData())
164+
return s.store.NamespaceGet(ctx, req.Tenant)
165165
}
166166

167167
// EditSessionRecordStatus defines if the sessions will be recorded.

api/store/namespace.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package store
33
import (
44
"context"
55

6-
"github.com/shellhub-io/shellhub/pkg/api/query"
76
"github.com/shellhub-io/shellhub/pkg/models"
87
)
98

@@ -15,24 +14,24 @@ type NamespaceStore interface {
1514
//
1615
// It returns the list of namespaces, the total count of matching documents (ignoring pagination), and
1716
// an error if any.
18-
NamespaceList(ctx context.Context, paginator query.Paginator, filters query.Filters, opts ...NamespaceQueryOption) ([]models.Namespace, int, error)
17+
NamespaceList(ctx context.Context, opts ...QueryOption) ([]models.Namespace, int, error)
1918

2019
// NamespaceGet retrieves a namespace identified by the given tenantID. A list of options can be
2120
// passed to inject additional data into the namespace.
2221
//
2322
// It returns the namespace or an error if any.
24-
NamespaceGet(ctx context.Context, tenantID string, opts ...NamespaceQueryOption) (*models.Namespace, error)
23+
NamespaceGet(ctx context.Context, tenantID string, opts ...QueryOption) (*models.Namespace, error)
2524

2625
// NamespaceGetByName retrieves a namespace by its name, similar to [Store.NamespaceGet], but matches by name instead
2726
// of tenantID.
28-
NamespaceGetByName(ctx context.Context, name string, opts ...NamespaceQueryOption) (*models.Namespace, error)
27+
NamespaceGetByName(ctx context.Context, name string, opts ...QueryOption) (*models.Namespace, error)
2928

3029
// NamespaceGetPreferred retrieves the user's preferred namespace. If the user has no preferred namespace it returns
3130
// the first namespace where the user is a member (typically the first one the user was added to). A list of options
3231
// can be passed via `opts` to inject additional data into the namespace.
3332
//
3433
// It returns the namespace or an error if any.
35-
NamespaceGetPreferred(ctx context.Context, userID string, opts ...NamespaceQueryOption) (*models.Namespace, error)
34+
NamespaceGetPreferred(ctx context.Context, userID string, opts ...QueryOption) (*models.Namespace, error)
3635

3736
NamespaceCreate(ctx context.Context, namespace *models.Namespace) (*models.Namespace, error)
3837

api/store/pg/namespace.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ import (
44
"context"
55

66
"github.com/shellhub-io/shellhub/api/store"
7-
"github.com/shellhub-io/shellhub/pkg/api/query"
87
"github.com/shellhub-io/shellhub/pkg/models"
98
)
109

1110
func (pg *pg) NamespaceCreate(ctx context.Context, namespace *models.Namespace) (*models.Namespace, error) {
1211
return nil, nil
1312
}
1413

15-
func (pg *pg) NamespaceList(ctx context.Context, paginator query.Paginator, filters query.Filters, opts ...store.NamespaceQueryOption) ([]models.Namespace, int, error) {
14+
func (pg *pg) NamespaceList(ctx context.Context, opts ...store.QueryOption) ([]models.Namespace, int, error) {
1615
return nil, 0, nil
1716
}
1817

19-
func (pg *pg) NamespaceGet(ctx context.Context, tenantID string, opts ...store.NamespaceQueryOption) (*models.Namespace, error) {
18+
func (pg *pg) NamespaceGet(ctx context.Context, tenantID string, opts ...store.QueryOption) (*models.Namespace, error) {
2019
return nil, nil
2120
}
2221

23-
func (pg *pg) NamespaceGetByName(ctx context.Context, name string, opts ...store.NamespaceQueryOption) (*models.Namespace, error) {
22+
func (pg *pg) NamespaceGetByName(ctx context.Context, name string, opts ...store.QueryOption) (*models.Namespace, error) {
2423
// TODO: unify get methods
2524
return nil, nil
2625
}
2726

28-
func (pg *pg) NamespaceGetPreferred(ctx context.Context, userID string, opts ...store.NamespaceQueryOption) (*models.Namespace, error) {
27+
func (pg *pg) NamespaceGetPreferred(ctx context.Context, userID string, opts ...store.QueryOption) (*models.Namespace, error) {
2928
// TODO: unify get methods
3029
return nil, nil
3130
}

api/store/pg/pg.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import (
1414
"github.com/uptrace/bun/dialect/pgdialect"
1515
)
1616

17+
type queryOptions struct{}
18+
1719
type pg struct {
18-
driver *bun.DB
20+
driver *bun.DB
21+
options *queryOptions
1922
}
2023

2124
func URI(host, port, user, password, db string) string {
@@ -35,7 +38,7 @@ func New(ctx context.Context, uri string, opts ...options.Option) (store.Store,
3538
return nil, err
3639
}
3740

38-
pg := &pg{driver: bun.NewDB(stdlib.OpenDBFromPool(pool), pgdialect.New())}
41+
pg := &pg{driver: bun.NewDB(stdlib.OpenDBFromPool(pool), pgdialect.New()), options: &queryOptions{}}
3942
if err := pg.driver.Ping(); err != nil {
4043
return nil, err
4144
}

0 commit comments

Comments
 (0)