Skip to content

Commit

Permalink
Refactor list things
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Jul 2, 2024
1 parent 7a83a49 commit a259358
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/clients/postgres/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func PageQuery(pm clients.Page) (string, error) {
query = append(query, "id ILIKE '%' || :id || '%'")
}
if pm.Tag != "" {
query = append(query, ":tag = ANY(c.tags)")
query = append(query, "EXISTS (SELECT 1 FROM unnest(tags) AS tag WHERE tag ILIKE '%' || :tag || '%')")
}
if pm.Status != clients.AllStatus {
query = append(query, "c.status = :status")
Expand Down Expand Up @@ -569,6 +569,9 @@ func ConstructThingSearchQuery(pm clients.Page) (string, string) {
if pm.Tag != "" {
query = append(query, "EXISTS (SELECT 1 FROM unnest(tags) AS tag WHERE tag ILIKE '%' || :tag || '%')")
}
if len(pm.IDs) != 0 {
query = append(query, fmt.Sprintf("id IN ('%s')", strings.Join(pm.IDs, "','")))
}

if len(query) > 0 {
emq = fmt.Sprintf("WHERE %s", strings.Join(query, " AND "))
Expand Down
5 changes: 5 additions & 0 deletions things/api/http/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func decodeListClients(_ context.Context, r *http.Request) (interface{}, error)
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}
id, err := apiutil.ReadStringQuery(r, api.IDOrder, "")
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}
p, err := apiutil.ReadStringQuery(r, api.PermissionKey, api.DefPermission)
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
Expand All @@ -212,6 +216,7 @@ func decodeListClients(_ context.Context, r *http.Request) (interface{}, error)
permission: p,
listPerms: lp,
userID: chi.URLParam(r, "userID"),
id: id,
}
return req, nil
}
Expand Down
1 change: 1 addition & 0 deletions things/api/http/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func listClientsEndpoint(svc things.Service) endpoint.Endpoint {
Metadata: req.metadata,
ListPerms: req.listPerms,
Role: mgclients.AllRole, // retrieve all things since things don't have roles
Id: req.id,
}
page, err := svc.ListClients(ctx, req.token, req.userID, pm)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions things/api/http/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type listClientsReq struct {
userID string
listPerms bool
metadata mgclients.Metadata
id string
}

func (req listClientsReq) validate() error {
Expand Down
2 changes: 1 addition & 1 deletion things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (svc service) ListClients(ctx context.Context, token, reqUserID string, pm

pm.IDs = ids

tp, err := svc.clients.RetrieveAllByIDs(ctx, pm)
tp, err := svc.clients.SearchBasicInfo(ctx, pm)
if err != nil {
return mgclients.ClientsPage{}, errors.Wrap(svcerr.ErrViewEntity, err)
}
Expand Down

0 comments on commit a259358

Please sign in to comment.