package server import ( "context" "connectrpc.com/connect" "github.com/bufbuild/protovalidate-go" "github.com/tigorlazuardi/bluemage/go/api" "github.com/tigorlazuardi/bluemage/go/gen/converter" device "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1" "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1/v1connect" "github.com/tigorlazuardi/bluemage/go/pkg/errs" ) var _ v1connect.DeviceServiceHandler = (*DeviceHandler)(nil) type DeviceHandler struct { API *api.API v1connect.UnimplementedDeviceServiceHandler } var ( convert converter.DeviceConverterImpl validate, _ = protovalidate.New() ) // CreateDevice implements v1connect.DeviceServiceHandler. func (d *DeviceHandler) CreateDevice(ctx context.Context, request *connect.Request[device.CreateDeviceRequest]) (*connect.Response[device.CreateDeviceResponse], error) { err := validate.Validate(request.Msg) if err != nil { return nil, connect.NewError(connect.CodeInvalidArgument, err) } dev := convert.CreateDeviceRequestToModelsDevice(request.Msg) dev, err = d.API.DevicesCreate(ctx, dev) if err != nil { return nil, errs.IntoConnectError(err) } devResp := convert.ModelsDeviceToCreateDeviceResponse(dev) return connect.NewResponse(devResp), nil } // GetDevice implements v1connect.DeviceServiceHandler. func (d *DeviceHandler) GetDevice(ctx context.Context, request *connect.Request[device.GetDeviceRequest]) (*connect.Response[device.GetDeviceResponse], error) { err := validate.Validate(request.Msg) if err != nil { return nil, connect.NewError(connect.CodeInvalidArgument, err) } dev, err := d.API.GetDevice(ctx, request.Msg.Slug) if err != nil { return nil, errs.IntoConnectError(err) } devResp := convert.ModelsDeviceToGetDeviceResponse(dev) return connect.NewResponse(devResp), nil }