2024-05-06 18:50:43 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
"github.com/tigorlazuardi/redmage/views"
|
2024-05-08 14:09:00 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/views/devices"
|
2024-05-06 18:50:43 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) PageDevices(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx, start := tracer.Start(req.Context(), "*Routes.PageDevices")
|
|
|
|
defer start.End()
|
|
|
|
|
|
|
|
vc := views.NewContext(routes.Config, req)
|
2024-05-08 14:09:00 +07:00
|
|
|
var data devices.Data
|
2024-05-06 18:50:43 +07:00
|
|
|
data.Params.FillFromQuery(req.URL.Query())
|
|
|
|
|
|
|
|
result, err := routes.API.DevicesList(ctx, data.Params)
|
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to query devices")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
data.Error = message
|
2024-05-08 14:09:00 +07:00
|
|
|
if err := devices.View(vc, data).Render(ctx, rw); err != nil {
|
2024-05-06 18:50:43 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render devices error view")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.Devices = result.Devices
|
|
|
|
data.Total = result.Total
|
|
|
|
|
2024-05-08 14:09:00 +07:00
|
|
|
if err := devices.View(vc, data).Render(ctx, rw); err != nil {
|
2024-05-06 18:50:43 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render devices view")
|
|
|
|
}
|
|
|
|
}
|