MSP-1021 refactor script controller
This commit is contained in:
1 parent
8af19a3e0d
commit
aa8f411b95
1 file changed
+38
-12
+38
-12
@@ -1,14 +1,28 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/service"
|
||||
)
|
||||
|
||||
var Script scriptController
|
||||
type scriptService interface {
|
||||
Execute(req domain.ExecuteRequest) *domain.ScriptResp
|
||||
ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error)
|
||||
BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp
|
||||
BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp
|
||||
}
|
||||
|
||||
type scriptController struct{}
|
||||
type Script struct {
|
||||
service scriptService
|
||||
}
|
||||
|
||||
func NewScript(service scriptService) Script {
|
||||
return Script{service: service}
|
||||
}
|
||||
|
||||
// ExecuteById
|
||||
// @Tags script
|
||||
// @Summary Выполнить скрипт под конкретным ID
|
||||
// @Description Возвращает результат выполнения скрипта и ошибку (если есть)
|
||||
@@ -16,12 +30,22 @@ type scriptController struct{}
|
||||
// @Produce json
|
||||
// @Param body body domain.ExecuteByIdRequest true "идентификатор скрипта"
|
||||
// @Success 200 {object} domain.ScriptResp
|
||||
// @Failure 404 {object} structure.GrpcError
|
||||
// @Failure 404 {object} domain.GrpcError
|
||||
// @Router /script/execute_by_id [POST].
|
||||
func (c scriptController) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
|
||||
return service.Script.ExecuteById(req)
|
||||
func (c Script) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
|
||||
data, err := c.service.ExecuteById(req)
|
||||
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrScriptNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
return data, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Execute
|
||||
// @Tags script
|
||||
// @Summary Выполнить скрипт без учёта идентификатора
|
||||
// @Description Возвращает результат выполнения скрипта и ошибку (если есть)
|
||||
@@ -30,10 +54,11 @@ func (c scriptController) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Sc
|
||||
// @Param body body domain.ExecuteRequest true "Скрипт необходимый к выполнению"
|
||||
// @Success 200 {object} domain.ScriptResp
|
||||
// @Router /script/execute [POST].
|
||||
func (c scriptController) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
return service.Script.Execute(req)
|
||||
func (c Script) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
return c.service.Execute(req)
|
||||
}
|
||||
|
||||
// BatchExecute
|
||||
// @Tags script
|
||||
// @Summary Выполнить набор скриптов под конкретными ID
|
||||
// @Description Возвращает результат выполнения скриптов и ошибок (если есть)
|
||||
@@ -42,10 +67,11 @@ func (c scriptController) Execute(req domain.ExecuteRequest) *domain.ScriptResp
|
||||
// @Param body body []domain.ExecuteByIdRequest true "Набор идентификаторов и аргументов"
|
||||
// @Success 200 {array} domain.ScriptResp
|
||||
// @Router /script/batch_execute [POST].
|
||||
func (c scriptController) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp {
|
||||
return service.Script.BatchExecute(req)
|
||||
func (c Script) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp {
|
||||
return c.service.BatchExecute(req)
|
||||
}
|
||||
|
||||
// BatchExecuteById
|
||||
// @Tags script
|
||||
// @Summary Выполнить набор скриптов под конкретными ID с идентичным аргументом для всех
|
||||
// @Description Возвращает результат выполнения скриптов и ошибок (если есть)
|
||||
@@ -54,6 +80,6 @@ func (c scriptController) BatchExecute(req []domain.ExecuteByIdRequest) []domain
|
||||
// @Param body body domain.BatchExecuteByIdsRequest true "Набор идентификаторов и единый аргумент"
|
||||
// @Success 200 {array} domain.ScriptResp
|
||||
// @Router /script/batch_execute_by_ids [POST].
|
||||
func (c scriptController) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
|
||||
return service.Script.BatchExecuteById(req)
|
||||
func (c Script) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
|
||||
return c.service.BatchExecuteById(req)
|
||||
}
|
||||
Reference in new issue
Block a user