-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsInteraction.go
123 lines (108 loc) · 4.31 KB
/
opsInteraction.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type NewInteractionInput struct {
Input
Body *models.InteractionRecord `json:"interaction"`
}
type InteractionHolder struct {
Body *models.Interaction `json:"interaction"`
}
type InteractionsHolder struct {
Body []models.Interaction `json:"interactions"`
}
type interactionOperations Operations
func newInteractionOperations() *interactionOperations {
return &interactionOperations{Endpoint: "interaction"}
}
func (o *interactionOperations) RegisterGet(api huma.API) {
name := "Get Interaction By ID"
description := "Retrieve a specific interaction by ID."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{"wildlife-manager", "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 interaction." format:"uuid"`
}) (*InteractionHolder, error) {
interaction, err := stores.NewInteractionStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if interaction == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &InteractionHolder{Body: interaction}, nil
})
}
func (o *interactionOperations) RegisterGetAll(api huma.API) {
name := "Get All Interactions"
description := "Retrieve all interactions."
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{}) (*InteractionsHolder, error) {
interactions, err := stores.NewInteractionStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &InteractionsHolder{Body: interactions}, nil
})
}
func (o *interactionOperations) RegisterAdd(api huma.API) {
name := "Add Interaction"
description := "Submit a new interaction."
path := "/" + o.Endpoint + "/"
scopes := []string{}
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 *NewInteractionInput) (*InteractionHolder, error) {
interaction, err := stores.NewInteractionStore(relationalDB).Add(input.credential.UserID, input.Body)
if err != nil {
return nil, handleError(err)
}
// Add Interaction -> Get Questionnaire.
questionnaire, err := stores.NewQuestionnaireStore(relationalDB).GetRandomByInteraction(interaction)
if err != nil {
return nil, handleError(err)
}
interaction.Questionnaire = questionnaire
// Add Interaction -> Create Alarms.
if interaction.Type.ID == 1 {
ids, err := stores.NewAlarmStore(relationalDB).AddAllFromInteraction(interaction)
if err != nil {
return nil, handleError(err)
}
// From created Alarms -> Create Conveyances
if err := stores.NewConveyanceStore(relationalDB).AddForAlarmIDs(ids); err != nil {
return nil, handleError(err)
}
}
return &InteractionHolder{Body: interaction}, nil
})
}
func (o *interactionOperations) RegisterGetMine(api huma.API) {
name := "Get My Interactions"
description := "Retrieve my interactions."
path := "/" + o.Endpoint + "s/me/"
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 *Input) (*InteractionsHolder, error) {
interactions, err := stores.NewInteractionStore(relationalDB).GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return &InteractionsHolder{Body: interactions}, nil
})
}