Bluemage/go/server/device_handlers.go

109 lines
3.6 KiB
Go
Raw Normal View History

2024-08-05 23:06:32 +07:00
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"
2024-08-16 11:44:31 +07:00
v1connect "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1/devicev1connect"
2024-08-05 23:06:32 +07:00
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
)
var _ v1connect.DeviceServiceHandler = (*DeviceHandler)(nil)
type DeviceHandler struct {
API *api.API
v1connect.UnimplementedDeviceServiceHandler
}
var deviceConvert converter.DeviceConverterImpl
2024-08-05 23:06:32 +07:00
// 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)
2024-08-05 23:06:32 +07:00
if err != nil {
return nil, errs.IntoConnectError(err)
}
devResp := deviceConvert.ModelsDeviceToCreateDeviceResponse(dev)
2024-08-05 23:06:32 +07:00
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
2024-08-05 23:06:32 +07:00
}
2024-08-07 23:59:29 +07:00
// 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)
2024-08-07 23:59:29 +07:00
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))
}
2024-08-07 23:59:29 +07:00
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
}
2024-08-08 22:51:53 +07:00
// 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
}