-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopsExperiment.go
153 lines (137 loc) · 5.63 KB
/
opsExperiment.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type ExperimentHolder struct {
Body *models.Experiment `json:"experiment"`
}
type ExperimentsHolder struct {
Body []models.Experiment `json:"experiments"`
}
type ExperimentNewInput struct {
Input
Body *models.ExperimentRecord `json:"experiment"`
}
type ExperimentUpdateInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the experiment to be updated."`
Body *models.ExperimentRecord `json:"experiment"`
}
type ExperimentDeleteInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the experiment to be deleted."`
}
type ExperimentEndInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the experiment to be ended."`
}
type experimentOperations Operations
func newExperimentOperations() *experimentOperations {
return &experimentOperations{Endpoint: "experiment"}
}
func (o *experimentOperations) RegisterGetAll(api huma.API) {
name := "Get All Experiments"
description := "Retrieve all experiments."
path := "/" + o.Endpoint + "s/"
scopes := []string{"researcher"}
method := http.MethodGet
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *struct{}) (*ExperimentsHolder, error) {
experiments, err := stores.NewExperimentStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &ExperimentsHolder{Body: experiments}, nil
})
}
func (o *experimentOperations) RegisterAdd(api huma.API) {
name := "Add Experiment"
description := "Add a new experiment."
path := "/" + o.Endpoint + "/"
scopes := []string{"researcher"}
method := http.MethodPost
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *ExperimentNewInput) (*ExperimentHolder, error) {
experiment, err := stores.NewExperimentStore(relationalDB).Add(input.credential.UserID, input.Body)
if err != nil {
return nil, handleError(err)
}
return &ExperimentHolder{Body: experiment}, nil
})
}
func (o *experimentOperations) RegisterGetMine(api huma.API) {
name := "Get My Experiments"
description := "Retrieve my experiments."
path := "/" + o.Endpoint + "s/me/"
scopes := []string{"researcher"}
method := http.MethodGet
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *Input) (*ExperimentsHolder, error) {
experiments, err := stores.NewExperimentStore(relationalDB).GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return &ExperimentsHolder{Body: experiments}, nil
})
}
func (o *experimentOperations) RegisterUpdate(api huma.API) {
name := "Update Experiment"
description := "Update an existing experiment that has not started yet."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{"researcher"}
method := http.MethodPut
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *ExperimentUpdateInput) (*ExperimentHolder, error) {
experiment, err := stores.NewExperimentStore(relationalDB).Update(input.credential.UserID, input.ID, input.Body)
if err != nil {
return nil, handleError(err)
}
if experiment == nil {
return nil, generateNotFoundForThisUserError("experiment", input.ID)
}
return &ExperimentHolder{Body: experiment}, nil
})
}
func (o *experimentOperations) RegisterDelete(api huma.API) {
name := "Delete Experiment"
description := "Delete an experiment."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{}
method := http.MethodDelete
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *ExperimentDeleteInput) (*struct{}, error) {
err := stores.NewExperimentStore(relationalDB).Delete(input.ID, input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return nil, nil
})
}
func (o *experimentOperations) RegisterEnd(api huma.API) {
name := "End Experiment"
description := "End a started experiment immediately."
path := "/" + o.Endpoint + "/end/{id}"
scopes := []string{"researcher"}
method := http.MethodPut
huma.Register(api, huma.Operation{
OperationID: name, Summary: name, Path: path, Method: method, Tags: []string{o.Endpoint}, Description: generateDescription(description, scopes), Security: []map[string][]string{{"auth": scopes}},
}, func(ctx context.Context, input *ExperimentEndInput) (*ExperimentHolder, error) {
experiment, err := stores.NewExperimentStore(relationalDB).EndNow(input.credential.UserID, input.ID)
if err != nil {
return nil, handleError(err)
}
if experiment == nil {
return nil, generateNotFoundForThisUserError("experiment", input.ID)
}
return &ExperimentHolder{Body: experiment}, nil
})
}