-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopsQuestionnaire.go
149 lines (135 loc) · 5.57 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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type QuestionnaireHolder struct {
Body *models.Questionnaire `json:"questionnaire"`
}
type QuestionnairesHolder struct {
Body []models.Questionnaire `json:"questionnaires"`
}
type QuestionnaireAddInput struct {
Input
Body *models.QuestionnaireRecord `json:"questionnaire"`
}
type QuestionnaireUpdateInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the questionnaire to be updated."`
Body *models.QuestionnaireRecord `json:"questionnaire"`
}
type QuestionnaireDeleteInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the questionnaire to be deleted."`
}
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" format:"uuid" doc:"The ID of the questionnaire to retrieve."`
}) (*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 *QuestionnaireAddInput) (*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) RegisterUpdate(api huma.API) {
name := "Update Questionnaire"
description := "Update an existing questionnaire for which the experiment 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 *QuestionnaireUpdateInput) (*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) RegisterDelete(api huma.API) {
name := "Delete Questionnaire"
description := "Delete a questionnaire."
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 *QuestionnaireDeleteInput) (*struct{}, error) {
err := stores.NewQuestionnaireStore(relationalDB).Delete(input.ID, input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return nil, nil
})
}
func (o *questionnaireOperations) RegisterGetByExperiment(api huma.API) {
name := "Get Questionnaires By Experiment"
description := "Retrieve all questionnaires for a specific experiment."
path := "/" + o.Endpoint + "s/experiment/{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" format:"uuid" doc:"The ID of the experiment to retrieve questionnaires for."`
}) (*QuestionnairesHolder, error) {
questionnaires, err := stores.NewQuestionnaireStore(relationalDB).GetByExperiment(input.ID)
if err != nil {
return nil, handleError(err)
}
return &QuestionnairesHolder{Body: questionnaires}, nil
})
}