46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"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"
|
|
)
|
|
|
|
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)
|
|
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)
|
|
}
|
|
return device, errs.Wrapf(err, "failed to get device '%s'", slug).
|
|
Details(
|
|
"slug", slug,
|
|
"query", stmt.DebugSql(),
|
|
)
|
|
}
|
|
|
|
return device, nil
|
|
}
|