86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"connectrpc.com/connect"
|
|
"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
|
|
|
|
// CreateDevice implements v1connect.DeviceServiceHandler.
|
|
func (d *DeviceHandler) CreateDevice(ctx context.Context, request *connect.Request[device.CreateDeviceRequest]) (*connect.Response[device.CreateDeviceResponse], error) {
|
|
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) {
|
|
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
|
|
}
|
|
|
|
// ListDevices implements v1connect.DeviceServiceHandler.
|
|
func (d *DeviceHandler) ListDevices(ctx context.Context, request *connect.Request[device.ListDevicesRequest]) (*connect.Response[device.ListDevicesResponse], error) {
|
|
resp, err := d.API.DevicesList(ctx, request.Msg)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
return connect.NewResponse(resp), nil
|
|
}
|
|
|
|
// UpdateDevice updates a device.
|
|
// Only fields that are set in the request will be updated.
|
|
func (de *DeviceHandler) UpdateDevice(ctx context.Context, request *connect.Request[device.UpdateDeviceRequest]) (*connect.Response[device.UpdateDeviceResponse], error) {
|
|
slug := request.Msg.Slug
|
|
set := convert.DeviceSetterProtoToModelsDeviceSetter(request.Msg.Set)
|
|
err := de.API.DevicesUpdate(ctx, slug, set)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
|
|
resp := &device.UpdateDeviceResponse{
|
|
Slug: slug,
|
|
}
|
|
|
|
return connect.NewResponse(resp), nil
|
|
}
|
|
|
|
// DeviceExists checks if a device exists in the database.
|
|
func (de *DeviceHandler) DeviceExists(ctx context.Context, request *connect.Request[device.DeviceExistsRequest]) (*connect.Response[device.DeviceExistsResponse], error) {
|
|
slug := request.Msg.Slug
|
|
|
|
exists, err := de.API.DevicesExist(ctx, slug)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
|
|
resp := &device.DeviceExistsResponse{
|
|
Exists: exists,
|
|
}
|
|
return connect.NewResponse(resp), nil
|
|
}
|