109 lines
3.6 KiB
Go
109 lines
3.6 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"
|
|
v1connect "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1/devicev1connect"
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
|
)
|
|
|
|
var _ v1connect.DeviceServiceHandler = (*DeviceHandler)(nil)
|
|
|
|
type DeviceHandler struct {
|
|
API *api.API
|
|
|
|
v1connect.UnimplementedDeviceServiceHandler
|
|
}
|
|
|
|
var deviceConvert converter.DeviceConverterImpl
|
|
|
|
// CreateDevice implements v1connect.DeviceServiceHandler.
|
|
func (d *DeviceHandler) CreateDevice(ctx context.Context, request *connect.Request[device.CreateDeviceRequest]) (*connect.Response[device.CreateDeviceResponse], error) {
|
|
set := deviceConvert.CreateDeviceRequestToModelsDeviceSetter(request.Msg)
|
|
dev, err := d.API.DevicesCreate(ctx, set)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
devResp := deviceConvert.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)
|
|
}
|
|
|
|
data := deviceConvert.JetModelDeviceToProtoDevice(dev)
|
|
resp := &device.GetDeviceResponse{
|
|
Device: data,
|
|
}
|
|
return connect.NewResponse(resp), nil
|
|
}
|
|
|
|
// ListDevices implements v1connect.DeviceServiceHandler.
|
|
func (d *DeviceHandler) ListDevices(ctx context.Context, request *connect.Request[device.ListDevicesRequest]) (*connect.Response[device.ListDevicesResponse], error) {
|
|
listRequest := deviceConvert.ProtoListDevicesRequestToAPIListDevicesRequest(request.Msg)
|
|
devices, err := d.API.DevicesList(ctx, listRequest)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
resp := &device.ListDevicesResponse{
|
|
Devices: make([]*device.Device, 0, len(devices)),
|
|
}
|
|
for _, device := range devices {
|
|
resp.Devices = append(resp.Devices, deviceConvert.JetModelDeviceToProtoDevice(device))
|
|
}
|
|
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 := deviceConvert.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
|
|
}
|
|
|
|
// CountDevices count the number of devices.
|
|
func (de *DeviceHandler) CountDevices(ctx context.Context, request *connect.Request[device.CountDevicesRequest]) (*connect.Response[device.CountDevicesResponse], error) {
|
|
count, err := de.API.DevicesCount(ctx, request.Msg)
|
|
if err != nil {
|
|
return nil, errs.IntoConnectError(err)
|
|
}
|
|
|
|
resp := &device.CountDevicesResponse{
|
|
Count: count,
|
|
}
|
|
return connect.NewResponse(resp), nil
|
|
}
|