-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsQuestionnaire.go
165 lines (151 loc) · 6.2 KB
/
opsQuestionnaire.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
154
155
156
157
158
159
160
161
162
163
164
165
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type NewQuestionnaireInput struct {
Input
Body *models.QuestionnaireRecord `json:"questionnaire"`
}
type UpdateQuestionnaireInput struct {
Input
ID string `query:"ID" format:"uuid" doc:"The ID of the questionnaire to be updated."`
Body *models.QuestionnaireRecord `json:"questionnaire"`
}
type QuestionnaireHolder struct {
Body *models.Questionnaire `json:"questionnaire"`
}
type QuestionnairesHolder struct {
Body []models.Questionnaire `json:"questionnaires"`
}
type questionnaireOperations Operations
func newQuestionnaireOperations() *questionnaireOperations {
return &questionnaireOperations{Endpoint: "questionnaire"}
}
func (o *questionnaireOperations) RegisterGet(api huma.API) {
name := "Get Questionnaire By ID"
description := "Retrieve a specific questionnaire by ID."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{}
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 questionnaire." format:"uuid"`
}) (*QuestionnaireHolder, error) {
questionnaire, err := stores.NewQuestionnaireStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if questionnaire == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &QuestionnaireHolder{Body: questionnaire}, nil
})
}
func (o *questionnaireOperations) RegisterAdd(api huma.API) {
name := "Add Questionnaire"
description := "Add a new questionnaire."
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 *NewQuestionnaireInput) (*QuestionnaireHolder, error) {
experiments, err := stores.NewExperimentStore(relationalDB).GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
var experiment models.Experiment
for _, e := range experiments {
if e.ID == input.Body.ExperimentID {
experiment = e
break
}
}
if experiment.ID == "" {
return nil, generateNotFoundForThisUserError("experiment", input.Body.ExperimentID)
}
questionnaire, err := stores.NewQuestionnaireStore(relationalDB).Add(input.Body)
if err != nil {
return nil, handleError(err)
}
return &QuestionnaireHolder{Body: questionnaire}, nil
})
}
func (o *questionnaireOperations) RegisterGetByExperiment(api huma.API) {
name := "Get Questionnaires By Experiment"
description := "Retrieve all questionnaires by experimentID."
path := "/" + o.Endpoint + "s/{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 the experiment to retrieve questionnaires for." format:"uuid"`
}) (*QuestionnairesHolder, error) {
questionnaires, err := stores.NewQuestionnaireStore(relationalDB).GetByExperiment(input.ID)
if err != nil {
return nil, handleError(err)
}
return &QuestionnairesHolder{Body: questionnaires}, nil
})
}
func (o *questionnaireOperations) RegisterUpdate(api huma.API) {
name := "Update Questionnaire"
description := "Update an existing questionnaire for which the experiment 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 *UpdateQuestionnaireInput) (*QuestionnaireHolder, error) {
questionnaire, err := stores.NewQuestionnaireStore(relationalDB).Update(input.credential.UserID, input.ID, input.Body)
if err != nil {
return nil, handleError(err)
}
if questionnaire == nil {
return nil, generateNotFoundForThisUserError("questionnaire", input.ID)
}
return &QuestionnaireHolder{Body: questionnaire}, nil
})
}
/*
func (o *questionnaireOperations) RegisterGetAll(api huma.API) {
name := "Get All Questionnaires"
description := "Retrieve all questionnaires."
path := "/" + o.Endpoint + "s/"
scopes := []string{"administrator"}
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{}) (*QuestionnairesHolder, error) {
questionnaires, err := stores.NewQuestionnaireStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &QuestionnairesHolder{Body: questionnaires}, nil
})
}
*/
/*
func (o *questionnaireOperations) RegisterGetMine(api huma.API) {
name := "Get My Questionnaires"
description := "Retrieve my questionnaires."
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) (*QuestionnairesHolder, error) {
questionnaires, err := stores.NewQuestionnaireStore(relationalDB).GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return &QuestionnairesHolder{Body: questionnaires}, nil
})
}
*/