63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
|
package service_internal
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strconv"
|
||
|
|
||
|
"git.itzana.me/strafesnet/maps-service/pkg/internal"
|
||
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
||
|
)
|
||
|
|
||
|
// CreateScriptPolicy implements createScriptPolicy operation.
|
||
|
//
|
||
|
// Create a new script policy.
|
||
|
//
|
||
|
// POST /script-policy
|
||
|
func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolicyCreate) (*api.ID, error) {
|
||
|
from_script, err := svc.DB.Scripts().Get(ctx, req.FromScriptID)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// the existence of ToScriptID does not need to be validated because it's checked by a foreign key constraint.
|
||
|
|
||
|
script, err := svc.DB.ScriptPolicy().Create(ctx, model.ScriptPolicy{
|
||
|
ID: 0,
|
||
|
FromScriptHash: from_script.Hash,
|
||
|
ToScriptID: req.ToScriptID,
|
||
|
Policy: model.Policy(req.Policy),
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &api.ID{
|
||
|
ID: script.ID,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation.
|
||
|
//
|
||
|
// Get the policy for the given hash of script source code.
|
||
|
//
|
||
|
// GET /script-policy/hash/{FromScriptHash}
|
||
|
func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetScriptPolicyFromHashParams) (*api.ScriptPolicy, error) {
|
||
|
// parse hash from hex
|
||
|
hash, err := strconv.ParseUint(params.FromScriptHash, 16, 64)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &api.ScriptPolicy{
|
||
|
ID: policy.ID,
|
||
|
FromScriptHash: model.HashFormat(policy.FromScriptHash),
|
||
|
ToScriptID: policy.ToScriptID,
|
||
|
Policy: int32(policy.Policy),
|
||
|
}, nil
|
||
|
}
|