From fbb7f73142b7e6abf351c937b5d9a340a40b30d5 Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Wed, 7 Aug 2024 15:38:52 +0700 Subject: [PATCH] proto: split file --- .../20240805124501_create_devices_table.sql | 5 ++ schemas/proto/device/v1/device.proto | 90 +++++++++++++++---- schemas/proto/device/v1/get.proto | 29 ++++++ 3 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 schemas/proto/device/v1/get.proto diff --git a/schemas/migrations/20240805124501_create_devices_table.sql b/schemas/migrations/20240805124501_create_devices_table.sql index e4cba9b..6bac0d5 100644 --- a/schemas/migrations/20240805124501_create_devices_table.sql +++ b/schemas/migrations/20240805124501_create_devices_table.sql @@ -17,6 +17,8 @@ CREATE TABLE devices ( updated_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now')) ); +INSERT INTO metrics(name) VALUES ('devices.count'); + CREATE UNIQUE INDEX idx_devices_unique_slug ON devices(slug); CREATE TRIGGER devices_update_timestamp AFTER UPDATE ON devices FOR EACH ROW @@ -28,15 +30,18 @@ CREATE TRIGGER devices_insert_create_total_image_metrics AFTER INSERT on devices BEGIN INSERT INTO metrics (name, value) VALUES (CONCAT('devices.', NEW.slug, '.total_images'), 0); + UPDATE metrics SET value = value + 1 WHERE name = 'devices.count'; END; CREATE TRIGGER devices_delete_total_image_metrics AFTER DELETE on devices FOR EACH ROW BEGIN DELETE FROM metrics WHERE name = CONCAT('devices.', OLD.slug, '.total_images'); + UPDATE metrics SET value = value - 1 WHERE name = 'devices.count'; END; -- +goose StatementEnd -- +goose Down -- +goose StatementBegin DROP TABLE devices; +DELETE FROM metrics WHERE name = 'devices.count'; -- +goose StatementEnd diff --git a/schemas/proto/device/v1/device.proto b/schemas/proto/device/v1/device.proto index 3317bea..6edb0e0 100644 --- a/schemas/proto/device/v1/device.proto +++ b/schemas/proto/device/v1/device.proto @@ -3,6 +3,7 @@ syntax = "proto3"; package device.v1; import "buf/validate/validate.proto"; +import "device/v1/get.proto"; option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1"; @@ -12,30 +13,59 @@ service DeviceService { // If the device is not found, an error will be returned. rpc GetDevice(GetDeviceRequest) returns (GetDeviceResponse) {} + // ListDevices gets list of devices. + // + // Returns empty array if no matching query found. + // + // Only returns error if there's an internal error. + rpc ListDevices(ListDevicesRequest) returns (ListDevicesResponse) {} + // CreateDevice creates a new device. rpc CreateDevice(CreateDeviceRequest) returns (CreateDeviceResponse) {} } -message GetDeviceRequest { - // The `slug` is a unique identifier for the device. - string slug = 1 [(buf.validate.field).string.min_len = 1]; +message ListDevicesRequest { + // searches the name and slug of the devices. + // case insensitive. + // + // If empty or not given, lists all devices. + // (Limit, Offset, Order, Sort still applies). + // + // default: empty string. + string search = 1; + + // disabled limit the listing devides only + // to disabled devices. + // + // default: false. + bool disabled = 2; + + // limits the number of results. + // + // if value is 0 or not given, limit is set to 25. + // + // if limit is higher than 100, it is clamped to 100. + uint32 limit = 3; + + // offset for the given data. + // + // If offset is 0 or not given, the query begins from start. + uint32 offset = 4; + + // order_by is the field to order the devices by. + // + // If not given, the default is `created_at`. + OrderBy order_by = 5; + + // sort is the direction of the order_by result. + // + // If not given, the default is `ascending`. + Sort sort = 6; } -message GetDeviceResponse { - string slug = 1; - bool disabled = 2; - string name = 3; - double resolution_x = 4; - double resolution_y = 5; - double aspect_ratio_tolerance = 6; - int32 min_x = 7; - int32 min_y = 8; - int32 max_x = 9; - int32 max_y = 10; - bool nsfw = 11; - bool single_folder_mode = 12; - int64 created_at = 13; - int64 updated_at = 14; +message ListDevicesResponse { + uint32 count = 1; + repeated GetDeviceResponse devices = 2; } message CreateDeviceRequest { @@ -114,3 +144,27 @@ message CreateDeviceResponse { // `slug` is the slug of the created device. string slug = 1; } + +enum OrderBy { + ORDER_BY_UNSPECIFIED = 0; + ORDER_BY_SLUG = 1; + ORDER_BY_DISABLED = 2; + ORDER_BY_NAME = 3; + ORDER_BY_RESOLUTION_X = 4; + ORDER_BY_RESOLUTION_Y = 5; + ORDER_BY_ASPECT_RATIO_TOLERANCE = 6; + ORDER_BY_MIN_X = 7; + ORDER_BY_MIN_Y = 8; + ORDER_BY_MAX_X = 9; + ORDER_BY_MAX_Y = 10; + ORDER_BY_NSFW = 11; + ORDER_BY_SINGLE_FOLDER_MODE = 12; + ORDER_BY_CREATED_AT = 13; + ORDER_BY_UPDATED_AT = 14; +} + +enum Sort { + SORT_UNSPECIFIED = 0; + SORT_ASCENDING = 1; + SORT_DESCENDING = 2; +} diff --git a/schemas/proto/device/v1/get.proto b/schemas/proto/device/v1/get.proto new file mode 100644 index 0000000..c0a99c3 --- /dev/null +++ b/schemas/proto/device/v1/get.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +package device.v1; + +import "buf/validate/validate.proto"; + +option go_package = "github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1"; + +message GetDeviceRequest { + // The `slug` is a unique identifier for the device. + string slug = 1 [(buf.validate.field).string.min_len = 1]; +} + +message GetDeviceResponse { + string slug = 1; + bool disabled = 2; + string name = 3; + double resolution_x = 4; + double resolution_y = 5; + double aspect_ratio_tolerance = 6; + int32 min_x = 7; + int32 min_y = 8; + int32 max_x = 9; + int32 max_y = 10; + bool nsfw = 11; + bool single_folder_mode = 12; + int64 created_at = 13; + int64 updated_at = 14; +}