Redmage/server/routes/device_list.go

31 lines
771 B
Go
Raw Normal View History

2024-04-14 17:30:04 +07:00
package routes
import (
"encoding/json"
"net/http"
"github.com/tigorlazuardi/redmage/api"
2024-04-24 22:26:04 +07:00
"github.com/tigorlazuardi/redmage/pkg/errs"
2024-04-14 17:30:04 +07:00
"github.com/tigorlazuardi/redmage/pkg/log"
)
func (routes *Routes) APIDeviceList(rw http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "*Routes.APIDeviceList")
defer span.End()
2024-05-06 18:50:43 +07:00
var params api.DevicesListParams
params.FillFromQuery(r.URL.Query())
2024-04-14 17:30:04 +07:00
2024-05-06 18:50:43 +07:00
result, err := routes.API.DevicesList(ctx, params)
2024-04-14 17:30:04 +07:00
if err != nil {
2024-04-24 22:26:04 +07:00
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
_ = json.NewEncoder(rw).Encode(map[string]string{"error": message})
2024-04-14 17:30:04 +07:00
return
}
if err := json.NewEncoder(rw).Encode(result); err != nil {
log.New(ctx).Err(err).Error("failed to marshal json api devices")
}
}