Bluemage/go/api/devices_get_by_slug.go

46 lines
1.1 KiB
Go
Raw Normal View History

2024-08-05 23:06:32 +07:00
package api
import (
"context"
"errors"
2024-08-05 23:06:32 +07:00
"connectrpc.com/connect"
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
"github.com/go-jet/jet/v2/qrm"
"github.com/tigorlazuardi/bluemage/go/gen/jet/model"
. "github.com/go-jet/jet/v2/sqlite"
. "github.com/tigorlazuardi/bluemage/go/gen/jet/table"
2024-08-05 23:06:32 +07:00
)
func (api *API) GetDevice(ctx context.Context, slug string) (device model.Devices, err error) {
ctx, span := tracer.Start(ctx, "GetDevice")
defer func() { telemetry.EndWithStatus(span, err) }()
stmt := SELECT(Devices.AllColumns).
FROM(Devices).
WHERE(Devices.Slug.EQ(String(slug)))
err = stmt.QueryContext(ctx, api.DB, &device)
2024-08-05 23:06:32 +07:00
if err != nil {
if errors.Is(err, qrm.ErrNoRows) {
return device, errs.
Wrapf(err, "device '%s' does not exist", slug).
Details(
"slug", slug,
"query", stmt.DebugSql(),
).
Code(connect.CodeNotFound)
2024-08-05 23:06:32 +07:00
}
return device, errs.Wrapf(err, "failed to get device '%s'", slug).
Details(
"slug", slug,
"query", stmt.DebugSql(),
)
2024-08-05 23:06:32 +07:00
}
return device, nil
}