2024-08-05 23:06:32 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-15 09:36:06 +07:00
|
|
|
"errors"
|
2024-08-05 23:06:32 +07:00
|
|
|
|
|
|
|
"connectrpc.com/connect"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
2024-08-15 09:36:06 +07:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-08-15 09:36:06 +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 {
|
2024-08-15 09:36:06 +07:00
|
|
|
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
|
|
|
}
|
2024-08-15 09:36:06 +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
|
|
|
|
}
|