-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsExperiment.go
130 lines (117 loc) · 4.73 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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type NewExperimentInput struct {
Input
Body *models.ExperimentRecord `json:"experiment"`
}
type UpdateExperimentInput struct {
Input
ID string `query:"ID" format:"uuid" doc:"The ID of the experiment to be updated."`
Body *models.ExperimentRecord `json:"experiment"`
}
type ExperimentHolder struct {
Body *models.Experiment `json:"experiment"`
}
type ExperimentsHolder struct {
Body []models.Experiment `json:"experiments"`
}
type experimentOperations Operations
func newExperimentOperations() *experimentOperations {
return &experimentOperations{Endpoint: "experiment"}
}
/*
func (o *experimentOperations) RegisterGet(api huma.API) {
name := "Get Experiment By ID"
description := "Retrieve a specific experiment by ID."
path := "/" + o.Endpoint + "/{id}"
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 {
ID string `path:"id" doc:"The ID of this experiment." format:"uuid"`
}) (*ExperimentHolder, error) {
experiment, err := stores.NewExperimentStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if experiment == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &ExperimentHolder{Body: experiment}, nil
})
}
*/
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 *NewExperimentInput) (*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 + "/"
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 *UpdateExperimentInput) (*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
})
}