Redmage/api/devices_list.go

120 lines
2.8 KiB
Go
Raw Normal View History

2024-04-14 17:30:04 +07:00
package api
import (
"context"
2024-05-06 18:50:43 +07:00
"strconv"
2024-04-14 17:30:04 +07:00
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
"github.com/stephenafamo/bob/dialect/sqlite/sm"
"github.com/tigorlazuardi/redmage/models"
2024-04-14 17:30:04 +07:00
"github.com/tigorlazuardi/redmage/pkg/errs"
)
type DevicesListParams struct {
2024-05-06 18:50:43 +07:00
Q string
Status int
Limit int64
Offset int64
OrderBy string
Sort string
2024-04-14 17:30:04 +07:00
}
2024-05-06 18:50:43 +07:00
func (dlp *DevicesListParams) FillFromQuery(query Queryable) {
dlp.Status, _ = strconv.Atoi(query.Get("status"))
if dlp.Status > 2 {
dlp.Status = 2
2024-04-26 22:13:04 +07:00
}
2024-05-06 18:50:43 +07:00
dlp.Q = query.Get("q")
2024-04-26 22:13:04 +07:00
2024-05-06 18:50:43 +07:00
dlp.Limit, _ = strconv.ParseInt(query.Get("limit"), 10, 64)
if dlp.Limit < 1 {
dlp.Limit = 20
}
2024-05-06 18:50:43 +07:00
dlp.Offset, _ = strconv.ParseInt(query.Get("offset"), 10, 64)
if dlp.Offset < 0 {
dlp.Offset = 0
}
dlp.OrderBy = query.Get("order_by")
dlp.Sort = query.Get("sort")
}
func (dlp DevicesListParams) Query() (expr []bob.Mod[*dialect.SelectQuery]) {
expr = append(expr, dlp.CountQuery()...)
2024-04-14 17:30:04 +07:00
if dlp.Limit > 0 {
expr = append(expr, sm.Limit(dlp.Limit))
}
2024-04-14 17:30:04 +07:00
if dlp.Offset > 0 {
expr = append(expr, sm.Offset(dlp.Offset))
}
2024-04-14 17:30:04 +07:00
if dlp.OrderBy != "" {
order := sm.OrderBy(sqlite.Quote(dlp.OrderBy))
if dlp.Sort == "desc" {
expr = append(expr, order.Desc())
} else {
expr = append(expr, order.Asc())
2024-04-14 17:30:04 +07:00
}
} else {
expr = append(expr, sm.OrderBy(models.DeviceColumns.Name).Asc())
2024-04-14 17:30:04 +07:00
}
return expr
}
func (dlp DevicesListParams) CountQuery() (expr []bob.Mod[*dialect.SelectQuery]) {
2024-05-06 18:50:43 +07:00
if dlp.Status > 0 {
expr = append(expr, models.SelectWhere.Devices.Enable.EQ(int32(dlp.Status-1)))
2024-04-26 22:13:04 +07:00
}
if dlp.Q != "" {
arg := sqlite.Arg("%" + dlp.Q + "%")
expr = append(expr,
sm.Where(
models.DeviceColumns.Name.Like(arg).Or(models.DeviceColumns.Slug.Like(arg)),
),
)
2024-04-14 17:30:04 +07:00
}
return expr
}
type DevicesListResult struct {
Devices models.DeviceSlice `json:"devices"`
Total int64 `json:"total"`
}
func (api *API) DevicesList(ctx context.Context, params DevicesListParams) (result DevicesListResult, err error) {
ctx, span := tracer.Start(ctx, "*API.DevicesList")
defer span.End()
result.Devices, err = api.GetDevices(ctx, params)
2024-04-14 17:30:04 +07:00
if err != nil {
return result, errs.Wrapw(err, "failed to query devices", "params", params)
2024-04-14 17:30:04 +07:00
}
2024-04-25 13:28:35 +07:00
result.Total, err = models.Devices.Query(ctx, api.db, params.CountQuery()...).Count()
2024-04-14 17:30:04 +07:00
if err != nil {
return result, errs.Wrapw(err, "failed to count devices", "params", params)
2024-04-14 17:30:04 +07:00
}
return result, nil
}
func (api *API) GetDevices(ctx context.Context, params DevicesListParams) (result models.DeviceSlice, err error) {
ctx, span := tracer.Start(ctx, "*API.GetDevices")
defer span.End()
result, err = models.Devices.Query(ctx, api.db, params.Query()...).All()
if err != nil {
return result, errs.Wrapw(err, "failed to query devices", "params", params)
}
return result, nil
}