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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateScript implements createScript operation.
|
|
|
|
//
|
|
|
|
// Create a new script.
|
|
|
|
//
|
|
|
|
// POST /scripts
|
|
|
|
func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*api.ID, error) {
|
|
|
|
script, err := svc.DB.Scripts().Create(ctx, model.Script{
|
|
|
|
ID: 0,
|
|
|
|
Name: req.Name,
|
2024-12-20 00:49:22 +00:00
|
|
|
Hash: int64(model.HashSource(req.Source)),
|
2024-12-18 02:22:53 +00:00
|
|
|
Source: req.Source,
|
|
|
|
SubmissionID: req.SubmissionID.Or(0),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ID{
|
|
|
|
ID: script.ID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-12-18 23:22:23 +00:00
|
|
|
// 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
|
|
|
|
}
|
2024-12-20 00:49:22 +00:00
|
|
|
filter.AddNotNil("hash", int64(hash)) // No type safety!
|
2024-12-18 23:22:23 +00:00
|
|
|
}
|
|
|
|
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,
|
2024-12-20 00:49:22 +00:00
|
|
|
Hash: model.HashFormat(uint64(items[i].Hash)),
|
2024-12-18 23:22:23 +00:00
|
|
|
Source: items[i].Source,
|
|
|
|
SubmissionID: items[i].SubmissionID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-12-18 02:22:53 +00:00
|
|
|
// GetScript implements getScript operation.
|
|
|
|
//
|
|
|
|
// Get the specified script by ID.
|
|
|
|
//
|
|
|
|
// GET /scripts/{ScriptID}
|
|
|
|
func (svc *Service) GetScript(ctx context.Context, params api.GetScriptParams) (*api.Script, error) {
|
|
|
|
script, err := svc.DB.Scripts().Get(ctx, params.ScriptID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.Script{
|
|
|
|
ID: script.ID,
|
|
|
|
Name: script.Name,
|
2024-12-20 00:49:22 +00:00
|
|
|
Hash: model.HashFormat(uint64(script.Hash)),
|
2024-12-18 02:22:53 +00:00
|
|
|
Source: script.Source,
|
|
|
|
SubmissionID: script.SubmissionID,
|
|
|
|
}, nil
|
|
|
|
}
|