mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 06:50:18 +00:00
NOISSUE - Add rules ordering by order and name (#265)
Signed-off-by: Musilah <nataleigh.nk@gmail.com>
This commit is contained in:
@@ -55,6 +55,9 @@ func (req listRulesReq) validate() error {
|
||||
if req.Dir != "" && (req.Dir != api.AscDir && req.Dir != api.DescDir) {
|
||||
return apiutil.ErrInvalidDirection
|
||||
}
|
||||
if req.Order != "" && req.Order != "name" && req.Order != "created_at" && req.Order != "updated_at" {
|
||||
return apiutil.ErrInvalidOrder
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+5
-4
@@ -24,11 +24,7 @@ import (
|
||||
|
||||
const (
|
||||
ruleIdKey = "ruleID"
|
||||
reportIdKey = "reportID"
|
||||
inputChannelKey = "input_channel"
|
||||
statusKey = "status"
|
||||
actionKey = "action"
|
||||
defAction = "view"
|
||||
)
|
||||
|
||||
// MakeHandler creates an HTTP handler for the service endpoints.
|
||||
@@ -206,6 +202,10 @@ func decodeListRulesRequest(_ context.Context, r *http.Request) (interface{}, er
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
||||
}
|
||||
order, err := apiutil.ReadStringQuery(r, api.OrderKey, api.DefOrder)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap((apiutil.ErrValidation), err)
|
||||
}
|
||||
st, err := re.ToStatus(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
||||
@@ -223,6 +223,7 @@ func decodeListRulesRequest(_ context.Context, r *http.Request) (interface{}, er
|
||||
InputChannel: ic,
|
||||
Status: st,
|
||||
Dir: dir,
|
||||
Order: order,
|
||||
Tag: tag,
|
||||
},
|
||||
}, nil
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/absmach/magistrala/re"
|
||||
api "github.com/absmach/supermq/api/http"
|
||||
"github.com/absmach/supermq/pkg/errors"
|
||||
repoerr "github.com/absmach/supermq/pkg/errors/repository"
|
||||
"github.com/absmach/supermq/pkg/postgres"
|
||||
@@ -201,11 +202,20 @@ func (repo *PostgresRepository) ListRules(ctx context.Context, pm re.PageMeta) (
|
||||
pgData += " OFFSET :offset"
|
||||
}
|
||||
pq := pageRulesQuery(pm)
|
||||
|
||||
orderClause := ""
|
||||
switch pm.Order {
|
||||
case "name", "created_at", "updated_at":
|
||||
orderClause = fmt.Sprintf("ORDER BY %s", pm.Order)
|
||||
if pm.Dir == api.AscDir || pm.Dir == api.DescDir {
|
||||
orderClause = fmt.Sprintf("%s %s", orderClause, pm.Dir)
|
||||
}
|
||||
}
|
||||
q := fmt.Sprintf(`
|
||||
SELECT id, name, domain_id, tags, input_channel, input_topic, logic_type, logic_value, outputs,
|
||||
start_datetime, time, recurring, recurring_period, created_at, created_by, updated_at, updated_by, status
|
||||
FROM rules r %s %s;
|
||||
`, pq, pgData)
|
||||
FROM rules r %s %s %s;
|
||||
`, pq, orderClause, pgData)
|
||||
rows, err := repo.DB.NamedQueryContext(ctx, q, pm)
|
||||
if err != nil {
|
||||
return re.Page{}, err
|
||||
|
||||
@@ -102,6 +102,7 @@ type PageMeta struct {
|
||||
Offset uint64 `json:"offset" db:"offset"`
|
||||
Limit uint64 `json:"limit" db:"limit"`
|
||||
Dir string `json:"dir" db:"dir"`
|
||||
Order string `json:"order" db:"order"`
|
||||
Name string `json:"name" db:"name"`
|
||||
InputChannel string `json:"input_channel,omitempty" db:"input_channel"`
|
||||
InputTopic *string `json:"input_topic,omitempty" db:"input_topic"`
|
||||
|
||||
Reference in New Issue
Block a user