-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
82 lines (73 loc) · 2.46 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package workspace
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"admini.dev/admini/app"
"admini.dev/admini/app/action"
"admini.dev/admini/app/controller/cutil"
"admini.dev/admini/app/lib/filter"
"admini.dev/admini/app/lib/schema/model"
"admini.dev/admini/app/loader"
"admini.dev/admini/app/source"
)
func processModel(req *cutil.WorkspaceRequest, act *action.Action, srcKey string, m *model.Model, additional []string, as *app.State) (*Result, error) {
if len(additional) == 0 {
return processModelList(req, act, srcKey, m, as)
}
switch additional[0] {
case "new":
if string(req.RC.Method()) == fasthttp.MethodPost {
return processModelAdd(req, act, srcKey, m, additional[1:], as)
}
return processModelNew(req, act, srcKey, m, as)
case "v":
return processModelView(req, act, srcKey, m, additional[1:], as)
case "x":
if string(req.RC.Method()) == fasthttp.MethodPost {
return processModelSave(req, act, srcKey, m, additional[1:], as)
}
return processModelEdit(req, act, srcKey, m, additional[1:], as)
case "d":
return processModelDelete(req, act, srcKey, m, additional[1:], as)
default:
return nil, errors.Errorf("unhandled model parameters [%s]", strings.Join(additional, "/"))
}
}
func getModel(ctx context.Context, m *model.Model, idStrings []string, ld loader.Loader) ([]any, error) {
ids := make([]any, 0, len(idStrings))
for _, x := range idStrings {
ids = append(ids, x)
}
rs, err := ld.Get(ctx, m, ids)
if err != nil {
return nil, errors.Wrapf(err, "unable to retrieve model [%s] with key [%s]", m.Path().String(), strings.Join(idStrings, "/"))
}
switch len(rs.Data) {
case 0:
return nil, errors.Errorf("no [%s] found with key [%s]", m.Name(), strings.Join(idStrings, "/"))
case 1:
return rs.Data[0], nil
default:
return nil, errors.Errorf("multiple [%s] found with key [%s]", m.Name(), strings.Join(idStrings, "/"))
}
}
func loaderFor(req *cutil.WorkspaceRequest, srcKey string, as *app.State) (*source.Source, loader.Loader, error) {
s, err := req.Sources.GetWithError(srcKey)
if err != nil {
return nil, nil, err
}
l, err := as.Services.Loaders.Get(s.Type, s.Key, s.Config)
if err != nil {
return nil, nil, errors.Wrap(err, "no loader available")
}
return s, l, nil
}
func optionsFor(req *cutil.WorkspaceRequest) filter.OptionsMap {
ret := filter.OptionsMap{}
for k, v := range req.PS.Params {
ret[k] = &filter.Options{Sort: nil, Filter: nil, Group: nil, Params: v}
}
return ret
}