devices: implemented DeviceExists service

This commit is contained in:
Tigor Hutasuhut 2024-08-08 20:39:04 +07:00
parent 87fb2c45a1
commit 649be571d2
5 changed files with 42 additions and 3 deletions

View file

@ -4,8 +4,11 @@ import (
"sync"
"github.com/stephenafamo/bob"
"github.com/tigorlazuardi/bluemage/go/gen/converter"
)
var convert converter.DeviceConverterImpl
type API struct {
mu sync.Mutex
DB bob.Executor

View file

@ -7,15 +7,12 @@ import (
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
"github.com/stephenafamo/bob/dialect/sqlite/sm"
"github.com/tigorlazuardi/bluemage/go/gen/converter"
"github.com/tigorlazuardi/bluemage/go/gen/models"
device "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1"
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
"golang.org/x/net/context"
)
var convert converter.DeviceConverterImpl
func queryFromListDeviceRequest(req *device.ListDevicesRequest) (expr []bob.Mod[*dialect.SelectQuery]) {
expr = countQueryFromListDeviceRequest(req)

View file

@ -81,3 +81,22 @@ func (de *DeviceHandler) UpdateDevice(ctx context.Context, request *connect.Requ
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
if err := validate.Validate(request.Msg); err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
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
}

View file

@ -3,6 +3,7 @@ syntax = "proto3";
package device.v1;
import "device/v1/create.proto";
import "device/v1/exists.proto";
import "device/v1/get.proto";
import "device/v1/list.proto";
import "device/v1/update.proto";
@ -29,4 +30,7 @@ service DeviceService {
//
// Only fields that are set in the request will be updated.
rpc UpdateDevice(UpdateDeviceRequest) returns (UpdateDeviceResponse) {}
// DeviceExists checks if a device exists in the database.
rpc DeviceExists(DeviceExistsRequest) returns (DeviceExistsResponse) {}
}

View file

@ -0,0 +1,16 @@
syntax = "proto3";
package device.v1;
import "buf/validate/validate.proto";
option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1";
message DeviceExistsRequest {
// The `slug` is a unique identifier for the device.
string slug = 1 [(buf.validate.field).string.min_len = 1];
}
message DeviceExistsResponse {
bool exists = 1;
}