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"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-08-11 10:23:20 +07:00
|
|
|
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) {
|
2024-08-11 10:23:20 +07:00
|
|
|
dev := deviceConvert.CreateDeviceRequestToModelsDevice(request.Msg)
|
2024-08-08 20:49:18 +07:00
|
|
|
dev, err := d.API.DevicesCreate(ctx, dev)
|
2024-08-05 23:06:32 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.IntoConnectError(err)
|
|
|
|
}
|
2024-08-11 10:23:20 +07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-11 10:23:20 +07:00
|
|
|
devResp := deviceConvert.ModelsDeviceToGetDeviceResponse(dev)
|
2024-08-05 23:06:32 +07:00
|
|
|
return connect.NewResponse(devResp), nil
|
|
|
|
}
|
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) {
|
|
|
|
resp, err := d.API.DevicesList(ctx, request.Msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.IntoConnectError(err)
|
|
|
|
}
|
|
|
|
return connect.NewResponse(resp), nil
|
|
|
|
}
|
2024-08-08 16:13:30 +07:00
|
|
|
|
|
|
|
// 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
|
2024-08-11 10:23:20 +07:00
|
|
|
set := deviceConvert.DeviceSetterProtoToModelsDeviceSetter(request.Msg.Set)
|
2024-08-08 16:13:30 +07:00
|
|
|
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
|
|
|
|
}
|
2024-08-08 20:39:04 +07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|