-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsQuestion.go
111 lines (100 loc) · 3.61 KB
/
opsQuestion.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
package wildlifenl
import (
"context"
"net/http"
"regexp"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type NewQuestionInput struct {
Input
Body *models.QuestionRecord `json:"question"`
}
type QuestionHolder struct {
Body *models.Question `json:"question"`
}
type QuestionsHolder struct {
Body []models.Question `json:"questions"`
}
type QuestionDeleteInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the question to be deleted."`
}
type questionOperations Operations
func newQuestionOperations() *questionOperations {
return &questionOperations{Endpoint: "question"}
}
func (o *questionOperations) RegisterGet(api huma.API) {
name := "Get Question By ID"
description := "Retrieve a specific question 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 question." format:"uuid"`
}) (*QuestionHolder, error) {
question, err := stores.NewQuestionStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if question == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &QuestionHolder{Body: question}, nil
})
}
func (o *questionOperations) RegisterAdd(api huma.API) {
name := "Add Question"
description := "Add a new question."
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 *NewQuestionInput) (*QuestionHolder, error) {
if input.Body.OpenResponseFormat != nil {
if _, err := regexp.Compile(*input.Body.OpenResponseFormat); err != nil {
return nil, huma.Error400BadRequest("Field openResponseFormat must either be not present or contain a regular expression")
}
}
store := stores.NewQuestionnaireStore(relationalDB)
questionnaires, err := store.GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
var questionnaire models.Questionnaire
for _, q := range questionnaires {
if q.ID == input.Body.QuestionnaireID {
questionnaire = q
break
}
}
if questionnaire.ID == "" {
return nil, generateNotFoundForThisUserError("questionnaire", input.Body.QuestionnaireID)
}
question, err := stores.NewQuestionStore(relationalDB).Add(input.Body)
if err != nil {
return nil, handleError(err)
}
return &QuestionHolder{Body: question}, nil
})
}
func (o *questionOperations) RegisterDelete(api huma.API) {
name := "Delete Question"
description := "Delete a question."
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 *QuestionDeleteInput) (*struct{}, error) {
err := stores.NewQuestionStore(relationalDB).Delete(input.ID, input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return nil, nil
})
}