2024-12-06 00:47:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-06 02:52:57 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
2024-12-06 02:52:57 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
2024-12-07 02:06:26 +00:00
|
|
|
"github.com/dchest/siphash"
|
2024-12-06 00:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateScript implements createScript operation.
|
|
|
|
//
|
|
|
|
// Create a new script.
|
|
|
|
//
|
|
|
|
// POST /scripts
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*api.ID, error) {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-06 02:52:57 +00:00
|
|
|
return nil, ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-06 02:52:57 +00:00
|
|
|
return nil, ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
script, err := svc.DB.Scripts().Create(ctx, model.Script{
|
|
|
|
ID: 0,
|
2024-12-12 22:29:20 +00:00
|
|
|
Hash: siphash.Hash(0, 0, []byte(req.Source)),
|
2024-12-06 02:52:57 +00:00
|
|
|
Source: req.Source,
|
|
|
|
SubmissionID: req.SubmissionID.Or(0),
|
|
|
|
})
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-06 02:52:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ID{
|
2024-12-12 22:29:20 +00:00
|
|
|
ID: script.ID,
|
2024-12-06 02:52:57 +00:00
|
|
|
}, nil
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// DeleteScript implements deleteScript operation.
|
|
|
|
//
|
|
|
|
// Delete the specified script by ID.
|
|
|
|
//
|
|
|
|
// DELETE /scripts/{ScriptID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) DeleteScript(ctx context.Context, params api.DeleteScriptParams) error {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-06 02:52:57 +00:00
|
|
|
return ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-06 02:52:57 +00:00
|
|
|
return ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.DB.Scripts().Delete(ctx, params.ScriptID)
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// GetScript implements getScript operation.
|
|
|
|
//
|
|
|
|
// Get the specified script by ID.
|
|
|
|
//
|
|
|
|
// GET /scripts/{ScriptID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) GetScript(ctx context.Context, params api.GetScriptParams) (*api.Script, error) {
|
2024-12-10 05:19:38 +00:00
|
|
|
_, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-06 02:52:57 +00:00
|
|
|
return nil, ErrUserInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read permission for scripts only requires you to be logged in
|
|
|
|
|
|
|
|
script, err := svc.DB.Scripts().Get(ctx, params.ScriptID)
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-06 02:52:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.Script{
|
|
|
|
ID: script.ID,
|
2024-12-12 22:29:20 +00:00
|
|
|
Hash: fmt.Sprintf("%x", script.Hash),
|
2024-12-06 02:52:57 +00:00
|
|
|
Source: script.Source,
|
|
|
|
SubmissionID: script.SubmissionID,
|
|
|
|
}, nil
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// UpdateScript implements updateScript operation.
|
|
|
|
//
|
|
|
|
// Update the specified script by ID.
|
|
|
|
//
|
|
|
|
// PATCH /scripts/{ScriptID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) UpdateScript(ctx context.Context, req *api.ScriptUpdate, params api.UpdateScriptParams) error {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-06 02:52:57 +00:00
|
|
|
return ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-06 02:52:57 +00:00
|
|
|
return ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
pmap := datastore.Optional()
|
2024-12-12 22:29:20 +00:00
|
|
|
if source, ok := req.Source.Get(); ok {
|
|
|
|
pmap.Add("source", source)
|
|
|
|
pmap.Add("hash", siphash.Hash(0, 0, []byte(source)))
|
2024-12-06 02:52:57 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
if SubmissionID, ok := req.SubmissionID.Get(); ok {
|
|
|
|
pmap.Add("submission_id", SubmissionID)
|
2024-12-06 02:52:57 +00:00
|
|
|
}
|
|
|
|
return svc.DB.Scripts().Update(ctx, req.ID, pmap)
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|