-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopsMessage.go
139 lines (125 loc) · 5.14 KB
/
opsMessage.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type MessageHolder struct {
Body *models.Message `json:"message"`
}
type MessagesHolder struct {
Body []models.Message `json:"messages"`
}
type MessageAddInput struct {
Input
Body *models.MessageRecord `json:"message"`
}
type MessageDeleteInput struct {
Input
ID string `path:"id" format:"uuid" doc:"The ID of the message to be deleted."`
}
type messageOperations Operations
func newMessageOperations() *messageOperations {
return &messageOperations{Endpoint: "message"}
}
func (o *messageOperations) RegisterGet(api huma.API) {
name := "Get Message By ID"
description := "Retrieve a specific message 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 message." format:"uuid"`
}) (*MessageHolder, error) {
message, err := stores.NewMessageStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if message == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &MessageHolder{Body: message}, nil
})
}
func (o *messageOperations) RegisterAdd(api huma.API) {
name := "Add Message"
description := "Add a new message."
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 *MessageAddInput) (*MessageHolder, error) {
switch input.Body.Trigger {
case "encounter":
if input.Body.SpeciesID == nil || input.Body.EncounterMeters == nil || input.Body.EncounterMinutes == nil || input.Body.AnswerID != nil {
return nil, huma.Error400BadRequest("trigger 'encounter' requires fields speciesID, encounterMeters and encounterMinutes to be non-empty, and field answerID to be empty")
}
case "answer":
if input.Body.SpeciesID != nil || input.Body.EncounterMeters != nil || input.Body.EncounterMinutes != nil || input.Body.AnswerID == nil {
return nil, huma.Error400BadRequest("trigger 'answer' requires field answerID to be non-empty, and fields speciesID, encounterMeters and encounterMinutes to be empty")
}
case "alarm":
if input.Body.SpeciesID == nil || input.Body.EncounterMeters != nil || input.Body.EncounterMinutes != nil || input.Body.AnswerID != nil {
return nil, huma.Error400BadRequest("trigger 'alarm' requires field speciesID to be non-empty, and fields answerID, encounterMeters and encounterMinutes to be empty")
}
}
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)
}
message, err := stores.NewMessageStore(relationalDB).Add(input.Body)
if err != nil {
return nil, handleError(err)
}
return &MessageHolder{Body: message}, nil
})
}
func (o *messageOperations) RegisterDelete(api huma.API) {
name := "Delete Message"
description := "Delete a message."
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 *MessageDeleteInput) (*struct{}, error) {
err := stores.NewMessageStore(relationalDB).Delete(input.ID, input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return nil, nil
})
}
func (o *messageOperations) RegisterGetByExperiment(api huma.API) {
name := "Get Messages By Experiment"
description := "Retrieve all messages 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" doc:"The ID of the experiment to retrieve messages for." format:"uuid"`
}) (*MessagesHolder, error) {
messages, err := stores.NewMessageStore(relationalDB).GetByExperiment(input.ID)
if err != nil {
return nil, handleError(err)
}
return &MessagesHolder{Body: messages}, nil
})
}