Bluemage/go/api/devices_get_by_slug.go

23 lines
578 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"
)
func (api *API) GetDevice(ctx context.Context, slug string) (device *models.Device, err error) {
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" {
return device, errs.Wrapw(err, "device not found", "slug", slug).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
}