Bluemage/go/api/devices_get_by_slug.go

28 lines
696 B
Go
Raw Normal View History

2024-08-05 23:06:32 +07:00
package api
import (
"context"
"connectrpc.com/connect"
"github.com/tigorlazuardi/bluemage/go/gen/models"
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
2024-08-08 13:41:18 +07:00
"github.com/tigorlazuardi/bluemage/go/pkg/log"
2024-08-05 23:06:32 +07:00
)
func (api *API) GetDevice(ctx context.Context, slug string) (device *models.Device, err error) {
2024-08-08 13:41:18 +07:00
ctx, coll := log.WithQueryCollector(ctx)
device, err = models.FindDevice(ctx, api.DB, slug)
2024-08-05 23:06:32 +07:00
if err != nil {
if err.Error() == "sql: no rows in result set" {
2024-08-08 13:41:18 +07:00
return device, errs.Wrapw(err, "device not found",
"slug", slug,
"query", coll,
).Code(connect.CodeNotFound)
2024-08-05 23:06:32 +07:00
}
return device, errs.Wrapw(err, "failed to find device", "slug", slug)
2024-08-05 23:06:32 +07:00
}
return device, nil
}