120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package validator_controller
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.itzana.me/strafesnet/go-grpc/validator"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/service"
|
|
)
|
|
|
|
type Scripts struct {
|
|
*validator.UnimplementedValidatorScriptServiceServer
|
|
inner *service.Service
|
|
}
|
|
func NewScriptsController(
|
|
inner *service.Service,
|
|
) Scripts {
|
|
return Scripts{
|
|
inner: inner,
|
|
}
|
|
}
|
|
|
|
// CreateScript implements createScript operation.
|
|
//
|
|
// Create a new script.
|
|
//
|
|
// POST /scripts
|
|
func (svc *Scripts) Create(ctx context.Context, req *validator.ScriptCreate) (*validator.ScriptID, error) {
|
|
ResourceID := int64(0)
|
|
if req.ResourceID != nil {
|
|
ResourceID = int64(*req.ResourceID)
|
|
}
|
|
script, err := svc.inner.CreateScript(ctx, model.Script{
|
|
ID: 0,
|
|
Name: req.Name,
|
|
Hash: int64(model.HashSource(req.Source)),
|
|
Source: req.Source,
|
|
ResourceType: model.ResourceType(req.ResourceType),
|
|
ResourceID: ResourceID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &validator.ScriptID{
|
|
ID: uint64(script.ID),
|
|
}, nil
|
|
}
|
|
|
|
// ListScripts implements listScripts operation.
|
|
//
|
|
// Get list of scripts.
|
|
//
|
|
// GET /scripts
|
|
func (svc *Scripts) List(ctx context.Context, params *validator.ScriptListRequest) (*validator.ScriptListResponse, error) {
|
|
filter := service.NewScriptFilter()
|
|
if params.Filter.Hash != nil {
|
|
filter.SetHash(int64(*params.Filter.Hash))
|
|
}
|
|
if params.Filter.Name != nil {
|
|
filter.SetName(*params.Filter.Name)
|
|
}
|
|
if params.Filter.Source != nil {
|
|
filter.SetSource(*params.Filter.Source)
|
|
}
|
|
if params.Filter.ResourceType != nil {
|
|
filter.SetResourceType(int32(*params.Filter.ResourceType))
|
|
}
|
|
if params.Filter.ResourceID != nil {
|
|
filter.SetResourceID(int64(*params.Filter.ResourceID))
|
|
}
|
|
|
|
items, err := svc.inner.ListScripts(ctx, filter, model.Page{
|
|
Number: int32(params.Page.Number),
|
|
Size: int32(params.Page.Size),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := validator.ScriptListResponse{}
|
|
resp.Scripts = make([]*validator.Script, len(items))
|
|
for i, item := range items {
|
|
resource_id := uint64(item.ResourceID)
|
|
resp.Scripts[i] = &validator.Script{
|
|
ID: uint64(item.ID),
|
|
Name: item.Name,
|
|
Hash: uint64(item.Hash),
|
|
Source: item.Source,
|
|
ResourceType: validator.ResourceType(item.ResourceType),
|
|
ResourceID: &resource_id,
|
|
}
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
// GetScript implements getScript operation.
|
|
//
|
|
// Get the specified script by ID.
|
|
//
|
|
// GET /scripts/{ScriptID}
|
|
func (svc *Scripts) Get(ctx context.Context, params *validator.ScriptID) (*validator.Script, error) {
|
|
ScriptID := int64(params.ID)
|
|
script, err := svc.inner.GetScript(ctx, ScriptID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ResourceID := uint64(script.ResourceID)
|
|
return &validator.Script{
|
|
ID: uint64(script.ID),
|
|
Name: script.Name,
|
|
Hash: uint64(script.Hash),
|
|
Source: script.Source,
|
|
ResourceType: validator.ResourceType(script.ResourceType),
|
|
ResourceID: &ResourceID,
|
|
}, nil
|
|
}
|