From 621edbdbe06784dbd5670dbdf28ebe08a5f939a0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 18 Dec 2024 15:22:23 -0800 Subject: [PATCH] submissions: normalize get from hash as list requests --- pkg/datastore/datastore.go | 1 + pkg/model/script.go | 8 +++++ pkg/service/script_policy.go | 45 +++++------------------- pkg/service/scripts.go | 46 ++++++++++++++++++++++++ pkg/service_internal/script_policy.go | 50 ++++++++++++++++++--------- pkg/service_internal/scripts.go | 47 +++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 53 deletions(-) diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index e5bd32a..fede681 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -33,6 +33,7 @@ type Scripts interface { Create(ctx context.Context, smap model.Script) (model.Script, error) Update(ctx context.Context, id int64, values OptionalMap) error Delete(ctx context.Context, id int64) error + List(ctx context.Context, filters OptionalMap, page model.Page) ([]model.Script, error) } type ScriptPolicy interface { diff --git a/pkg/model/script.go b/pkg/model/script.go index f35f839..5989bee 100644 --- a/pkg/model/script.go +++ b/pkg/model/script.go @@ -2,19 +2,27 @@ package model import ( "fmt" + "strconv" "time" "github.com/dchest/siphash" ) +// compute the hash of a source code string func HashSource(source string) uint64{ return siphash.Hash(0, 0, []byte(source)) } +// format a hash value as a hexidecimal string func HashFormat(hash uint64) string{ return fmt.Sprintf("%016x", hash) } +// parse a hexidecimal hash string +func HashParse(hash string) (uint64, error){ + return strconv.ParseUint(hash, 16, 64) +} + type Script struct { ID int64 `gorm:"primaryKey"` Name string diff --git a/pkg/service/script_policy.go b/pkg/service/script_policy.go index 7e14939..9655cc2 100644 --- a/pkg/service/script_policy.go +++ b/pkg/service/script_policy.go @@ -2,7 +2,6 @@ package service import ( "context" - "strconv" "git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/datastore" @@ -56,7 +55,11 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptP filter := datastore.Optional() if params.FromScriptHash.IsSet(){ - filter.AddNotNil("from_script_hash", params.FromScriptHash.Value) + 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) @@ -90,7 +93,7 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, params api.ListScriptP // // Delete the specified script policy by ID. // -// DELETE /script-policy/id/{ScriptPolicyID} +// DELETE /script-policy/{ScriptPolicyID} func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScriptPolicyParams) error { userInfo, ok := ctx.Value("UserInfo").(UserInfo) if !ok { @@ -108,7 +111,7 @@ func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScr // // Get the specified script policy by ID. // -// GET /script-policy/id/{ScriptPolicyID} +// GET /script-policy/{ScriptPolicyID} func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPolicyParams) (*api.ScriptPolicy, error) { _, ok := ctx.Value("UserInfo").(UserInfo) if !ok { @@ -130,43 +133,11 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol }, 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) { - _, ok := ctx.Value("UserInfo").(UserInfo) - if !ok { - return nil, ErrUserInfo - } - - // Read permission for script policy only requires you to be logged in - - // 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 -} - // UpdateScriptPolicy implements updateScriptPolicy operation. // // Update the specified script policy by ID. // -// PATCH /script-policy/id/{ScriptPolicyID} +// POST /script-policy/{ScriptPolicyID} func (svc *Service) UpdateScriptPolicy(ctx context.Context, req *api.ScriptPolicyUpdate, params api.UpdateScriptPolicyParams) error { userInfo, ok := ctx.Value("UserInfo").(UserInfo) if !ok { diff --git a/pkg/service/scripts.go b/pkg/service/scripts.go index dac9e62..6dfa8ce 100644 --- a/pkg/service/scripts.go +++ b/pkg/service/scripts.go @@ -39,6 +39,52 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a }, nil } +// ListScripts implements listScripts operation. +// +// Get list of scripts. +// +// GET /scripts +func (svc *Service) ListScripts(ctx context.Context, params api.ListScriptsParams) ([]api.Script, error) { + filter := datastore.Optional() + + if params.Hash.IsSet(){ + hash, err := model.HashParse(params.Hash.Value) + if err != nil { + return nil, err + } + filter.AddNotNil("hash", hash) + } + if params.Name.IsSet(){ + filter.AddNotNil("name", params.Name.Value) + } + if params.Source.IsSet(){ + filter.AddNotNil("source", params.Source.Value) + } + if params.SubmissionID.IsSet(){ + filter.AddNotNil("submission_id", params.SubmissionID.Value) + } + + items, err := svc.DB.Scripts().List(ctx, filter, model.Page{ + Number: params.Page, + Size: params.Limit, + }) + if err != nil { + return nil, err + } + + var resp []api.Script + for i := 0; i < len(items); i++ { + resp = append(resp, api.Script{ + ID: items[i].ID, + Hash: model.HashFormat(items[i].Hash), + Source: items[i].Source, + SubmissionID: items[i].SubmissionID, + }) + } + + return resp, nil +} + // DeleteScript implements deleteScript operation. // // Delete the specified script by ID. diff --git a/pkg/service_internal/script_policy.go b/pkg/service_internal/script_policy.go index 925026d..f2a7160 100644 --- a/pkg/service_internal/script_policy.go +++ b/pkg/service_internal/script_policy.go @@ -2,8 +2,8 @@ package service_internal import ( "context" - "strconv" + "git.itzana.me/strafesnet/maps-service/pkg/datastore" "git.itzana.me/strafesnet/maps-service/pkg/internal" "git.itzana.me/strafesnet/maps-service/pkg/model" ) @@ -36,27 +36,45 @@ func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolic }, nil } -// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation. +// ListScriptPolicy implements listScriptPolicy operation. // -// Get the policy for the given hash of script source code. +// Get list of script policies. // -// 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) +// 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) + } + + items, err := svc.DB.ScriptPolicy().List(ctx, filter, model.Page{ + Number: params.Page, + Size: params.Limit, + }) if err != nil { return nil, err } - policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash) - if err != nil { - return nil, err + 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 &api.ScriptPolicy{ - ID: policy.ID, - FromScriptHash: model.HashFormat(policy.FromScriptHash), - ToScriptID: policy.ToScriptID, - Policy: int32(policy.Policy), - }, nil + return resp, nil } diff --git a/pkg/service_internal/scripts.go b/pkg/service_internal/scripts.go index 497bf1c..1680f57 100644 --- a/pkg/service_internal/scripts.go +++ b/pkg/service_internal/scripts.go @@ -3,6 +3,7 @@ package service_internal import ( "context" + "git.itzana.me/strafesnet/maps-service/pkg/datastore" "git.itzana.me/strafesnet/maps-service/pkg/internal" "git.itzana.me/strafesnet/maps-service/pkg/model" ) @@ -29,6 +30,52 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a }, nil } +// ListScripts implements listScripts operation. +// +// Get list of scripts. +// +// GET /scripts +func (svc *Service) ListScripts(ctx context.Context, params api.ListScriptsParams) ([]api.Script, error) { + filter := datastore.Optional() + + if params.Hash.IsSet(){ + hash, err := model.HashParse(params.Hash.Value) + if err != nil { + return nil, err + } + filter.AddNotNil("hash", hash) + } + if params.Name.IsSet(){ + filter.AddNotNil("name", params.Name.Value) + } + if params.Source.IsSet(){ + filter.AddNotNil("source", params.Source.Value) + } + if params.SubmissionID.IsSet(){ + filter.AddNotNil("submission_id", params.SubmissionID.Value) + } + + items, err := svc.DB.Scripts().List(ctx, filter, model.Page{ + Number: params.Page, + Size: params.Limit, + }) + if err != nil { + return nil, err + } + + var resp []api.Script + for i := 0; i < len(items); i++ { + resp = append(resp, api.Script{ + ID: items[i].ID, + Hash: model.HashFormat(items[i].Hash), + Source: items[i].Source, + SubmissionID: items[i].SubmissionID, + }) + } + + return resp, nil +} + // GetScript implements getScript operation. // // Get the specified script by ID.