diff --git a/assembly/locator.go b/assembly/locator.go index c8a5e46..06a4e33 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -156,7 +156,7 @@ func (l Locator) Config() *Config { ) configApiController := api.NewConfig(configApiService) - configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo) + configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo, moduleRepo) configSchemaController := api.NewConfigSchema(configSchemaApiService) controllers := routes.Controllers{ diff --git a/controller/api/config_schema.go b/controller/api/config_schema.go index 6efde60..1594871 100644 --- a/controller/api/config_schema.go +++ b/controller/api/config_schema.go @@ -3,14 +3,16 @@ package api import ( "context" - "github.com/pkg/errors" - "github.com/txix-open/isp-kit/grpc/apierrors" "isp-config-service/domain" "isp-config-service/entity" + + "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" ) type ConfigSchemaService interface { SchemaByModuleId(ctx context.Context, moduleId string) (*domain.ConfigSchema, error) + UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error } type ConfigSchema struct { @@ -49,3 +51,30 @@ func (c ConfigSchema) SchemaByModuleId(ctx context.Context, request domain.GetBy return schema, nil } } + +// UpdateSchemaByModuleName +// @Summary Метод обновления схемы конфигурации модуля +// @Description Меняет схему конфигурации модуля на переданную +// @Tags Схема +// @Accept json +// @Produce json +// @Param body body domain.UpdateSchemaRequest true "название модуля и схема конфига" +// @Success 200 {object} any +// @Failure 400 {object} apierrors.Error "`errorCode: 2001` - модуль не найден" +// @Failure 500 {object} apierrors.Error +// @Router /schema/update_by_module_name [POST] +func (c ConfigSchema) UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error { + err := c.service.UpdateSchemaByModuleName(ctx, request) + switch { + case errors.Is(err, entity.ErrModuleNotFound): + return apierrors.NewBusinessError( + domain.ErrorCodeModuleNotFound, + "module not found", + err, + ) + case err != nil: + return apierrors.NewInternalServiceError(err) + default: + return nil + } +} diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 82aabba..607faf4 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -235,6 +235,16 @@ definitions: - id - newConfigName type: object + domain.UpdateSchemaRequest: + properties: + moduleName: + type: string + schema: + type: object + required: + - moduleName + - schema + type: object domain.UpdateVariableRequest: properties: description: @@ -784,6 +794,35 @@ paths: summary: Метод получения схемы конфигурации модуля tags: - Схема + /schema/update_by_module_name: + post: + consumes: + - application/json + description: Меняет схему конфигурации модуля на переданную + parameters: + - description: название модуля и схема конфига + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.UpdateSchemaRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: {} + "400": + description: '`errorCode: 2001` - модуль не найден' + schema: + $ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error' + summary: Метод обновления схемы конфигурации модуля + tags: + - Схема /variable/all: post: consumes: diff --git a/domain/request.go b/domain/request.go index 53889d5..c1fbcfd 100644 --- a/domain/request.go +++ b/domain/request.go @@ -21,6 +21,11 @@ type GetByModuleNameRequest struct { ModuleName string `validate:"required"` } +type UpdateSchemaRequest struct { + ModuleName string `validate:"required"` + Schema json.RawMessage `validate:"required" swaggertype:"object"` +} + type CreateUpdateConfigRequest struct { Id string Name string `validate:"required"` diff --git a/repository/config_schema.go b/repository/config_schema.go index 9bf141e..08402c6 100644 --- a/repository/config_schema.go +++ b/repository/config_schema.go @@ -69,6 +69,23 @@ func (r ConfigSchema) GetByModuleId(ctx context.Context, moduleId string) (*enti return selectRow[entity.ConfigSchema](ctx, r.db, query, args...) } +func (r ConfigSchema) UpdateByModuleId(ctx context.Context, moduleId string, data []byte) error { + ctx = sql_metrics.OperationLabelToContext(ctx, "ConfigSchema.UpdateByModuleId") + query, args, err := squirrel.Update(Table("config_schema")). + Set("data", data). + Where(squirrel.Eq{"module_id": moduleId}). + ToSql() + if err != nil { + return errors.WithMessage(err, "build query") + } + + _, err = r.db.Exec(ctx, query, args...) + if err != nil { + return errors.WithMessagef(err, "exec: %s", query) + } + return nil +} + //nolint:nilnil func selectRow[T any](ctx context.Context, db db.DB, query string, args ...interface{}) (*T, error) { var result T diff --git a/routes/routes.go b/routes/routes.go index ce365f2..9edddfd 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -133,6 +133,10 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor { Path: "config/schema/get_by_module_id", Inner: true, Handler: c.ConfigSchemaApi.SchemaByModuleId, + }, { + Path: "config/schema/update_by_module_name", + Inner: true, + Handler: c.ConfigSchemaApi.UpdateSchemaByModuleName, }, // variables { diff --git a/service/api/config_schema.go b/service/api/config_schema.go index 1365cee..4bc2e45 100644 --- a/service/api/config_schema.go +++ b/service/api/config_schema.go @@ -4,18 +4,21 @@ import ( "context" "time" - "github.com/pkg/errors" "isp-config-service/domain" "isp-config-service/entity" + + "github.com/pkg/errors" ) type ConfigSchema struct { schemaRepo SchemaRepo + moduleRepo ModuleRepo } -func NewConfigSchema(schemaRepo SchemaRepo) ConfigSchema { +func NewConfigSchema(schemaRepo SchemaRepo, moduleRepo ModuleRepo) ConfigSchema { return ConfigSchema{ schemaRepo: schemaRepo, + moduleRepo: moduleRepo, } } @@ -39,3 +42,20 @@ func (s ConfigSchema) SchemaByModuleId(ctx context.Context, moduleId string) (*d return &result, nil } + +func (s ConfigSchema) UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error { + modules, err := s.moduleRepo.GetByNames(ctx, []string{request.ModuleName}) + if err != nil { + return errors.WithMessage(err, "get modules by name") + } + if len(modules) == 0 { + return entity.ErrModuleNotFound + } + + err = s.schemaRepo.UpdateByModuleId(ctx, modules[0].Id, request.Schema) + if err != nil { + return errors.WithMessage(err, "update schema by module id") + } + + return nil +} diff --git a/service/api/module.go b/service/api/module.go index 596c43f..3bae988 100644 --- a/service/api/module.go +++ b/service/api/module.go @@ -27,6 +27,7 @@ type BackendsRepo interface { type SchemaRepo interface { All(ctx context.Context) ([]entity.ConfigSchema, error) GetByModuleId(ctx context.Context, moduleId string) (*entity.ConfigSchema, error) + UpdateByModuleId(ctx context.Context, moduleId string, data []byte) error } type Module struct {