2024-12-18 02:22:53 +00:00
|
|
|
package service_internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-12-18 23:22:23 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
2024-12-18 02:22:53 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-12-18 23:22:23 +00:00
|
|
|
// ListScriptPolicy implements listScriptPolicy operation.
|
2024-12-18 02:22:53 +00:00
|
|
|
//
|
2024-12-18 23:22:23 +00:00
|
|
|
// Get list of script policies.
|
2024-12-18 02:22:53 +00:00
|
|
|
//
|
2024-12-18 23:22:23 +00:00
|
|
|
// GET /script-policy
|
|
|
|
func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptPolicyParams) ([]api.ScriptPolicy, error) {
|
|
|
|
filter := datastore.Optional()
|
|
|
|
|
|
|
|
if params.FromScriptHash.IsSet(){
|
|
|
|
hash, err := model.HashParse(params.FromScriptHash.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filter.AddNotNil("from_script_hash", hash)
|
|
|
|
}
|
|
|
|
if params.ToScriptID.IsSet(){
|
|
|
|
filter.AddNotNil("to_script_id", params.ToScriptID.Value)
|
|
|
|
}
|
|
|
|
if params.Policy.IsSet(){
|
|
|
|
filter.AddNotNil("policy", params.Policy.Value)
|
2024-12-18 02:22:53 +00:00
|
|
|
}
|
|
|
|
|
2024-12-18 23:22:23 +00:00
|
|
|
items, err := svc.DB.ScriptPolicy().List(ctx, filter, model.Page{
|
|
|
|
Number: params.Page,
|
|
|
|
Size: params.Limit,
|
|
|
|
})
|
2024-12-18 02:22:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-18 23:22:23 +00:00
|
|
|
var resp []api.ScriptPolicy
|
|
|
|
for i := 0; i < len(items); i++ {
|
|
|
|
resp = append(resp, api.ScriptPolicy{
|
|
|
|
ID: items[i].ID,
|
|
|
|
FromScriptHash: model.HashFormat(items[i].FromScriptHash),
|
|
|
|
ToScriptID: items[i].ToScriptID,
|
|
|
|
Policy: int32(items[i].Policy),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2024-12-18 02:22:53 +00:00
|
|
|
}
|