-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsAnswer.go
99 lines (90 loc) · 3.19 KB
/
opsAnswer.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type AnswerHolder struct {
Body *models.Answer `json:"answer"`
}
type AnswersHolder struct {
Body []models.Answer `json:"answers"`
}
type NewAnswerInput struct {
Input
Body *models.AnswerRecord `json:"answer"`
}
type answerOperations Operations
func newAnswerOperations() *answerOperations {
return &answerOperations{Endpoint: "answer"}
}
func (o *answerOperations) RegisterGet(api huma.API) {
name := "Get Answer By ID"
description := "Retrieve a specific answer 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 answer." format:"uuid"`
}) (*AnswerHolder, error) {
answer, err := stores.NewAnswerStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if answer == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &AnswerHolder{Body: answer}, nil
})
}
func (o *answerOperations) RegisterAdd(api huma.API) {
name := "Add Answer"
description := "Add a new answer."
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 *NewAnswerInput) (*AnswerHolder, error) {
if input.Body.NextQuestionID != nil && *input.Body.NextQuestionID == input.Body.QuestionID {
return nil, huma.Error400BadRequest("Fields questionID and nextQuestionID must have different values")
}
store := stores.NewQuestionnaireStore(relationalDB)
questionnaires, err := store.GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
var question models.Question
var nextQuestion models.Question
for _, r := range questionnaires {
for _, q := range r.Questions {
if q.ID == input.Body.QuestionID {
question = q
}
if input.Body.NextQuestionID != nil && q.ID == *input.Body.NextQuestionID {
nextQuestion = q
}
}
}
if question.ID == "" {
return nil, generateNotFoundForThisUserError("question", input.Body.QuestionID)
}
if input.Body.NextQuestionID != nil {
if nextQuestion.ID == "" {
return nil, generateNotFoundForThisUserError("question", *input.Body.NextQuestionID)
}
if nextQuestion.QuestionnaireID != question.QuestionnaireID {
return nil, huma.Error400BadRequest("Fields questionID and nextQuestionID must both refer to questions in the same questionnaire.")
}
}
answer, err := stores.NewAnswerStore(relationalDB).Add(input.Body)
if err != nil {
return nil, handleError(err)
}
return &AnswerHolder{Body: answer}, nil
})
}