-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopsZone.go
More file actions
135 lines (121 loc) · 4.91 KB
/
opsZone.go
File metadata and controls
135 lines (121 loc) · 4.91 KB
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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type ZoneHolder struct {
Body *models.Zone `json:"zone"`
}
type ZonesHolder struct {
Body []models.Zone `json:"zones"`
}
type ZoneAddInput struct {
Input
Body *models.ZoneRecord `json:"zone"`
}
type SpeciesForZoneUpdateInput struct {
Input
Body *struct {
ZoneID string `json:"zoneID" format:"uuid" doc:"The ID of the zone"`
SpeciesID string `json:"speciesID" format:"uuid" doc:"The ID of the species"`
}
}
type ZoneDeactivateInput struct {
Input
ID string `path:"id" doc:"The ID of this zone." format:"uuid"`
}
type zoneOperations Operations
func newZoneOperations() *zoneOperations {
return &zoneOperations{Endpoint: "zone"}
}
func (o *zoneOperations) RegisterAdd(api huma.API) {
name := "Add Zone"
description := "Add a new zone."
path := "/" + o.Endpoint + "/"
scopes := []string{"land-user", "wildlife-manager"}
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 *ZoneAddInput) (*ZoneHolder, error) {
if len(input.Body.Definition) < 3 {
return nil, huma.Error400BadRequest("definition must contain 3 or more points")
}
zone, err := stores.NewZoneStore(relationalDB).Add(input.credential.UserID, input.Body)
if err != nil {
return nil, handleError(err)
}
return &ZoneHolder{Body: zone}, nil
})
}
func (o *zoneOperations) RegisterGetMine(api huma.API) {
name := "Get My Zones"
description := "Retrieve my zones."
path := "/" + o.Endpoint + "s/me/"
scopes := []string{"land-user", "wildlife-manager"}
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) (*ZonesHolder, error) {
zones, err := stores.NewZoneStore(relationalDB).GetByUser(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return &ZonesHolder{Body: zones}, nil
})
}
func (o *zoneOperations) RegisterAddSpeciesToZone(api huma.API) {
name := "Add a Species to a Zone"
description := "Add a species for which this zone should create alarms."
path := "/" + o.Endpoint + "/species/"
scopes := []string{"land-user", "wildlife-manager"}
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 *SpeciesForZoneUpdateInput) (*ZoneHolder, error) {
zone, err := stores.NewZoneStore(relationalDB).AddSpeciesToZone(input.credential.UserID, input.Body.ZoneID, input.Body.SpeciesID)
if err != nil {
return nil, handleError(err)
}
return &ZoneHolder{Body: zone}, nil
})
}
func (o *zoneOperations) RegisterRemoveSpeciesFromZone(api huma.API) {
name := "Remove a Species from a Zone"
description := "Remove a species for which this zone should create alarms."
path := "/" + o.Endpoint + "/species/"
scopes := []string{"land-user", "wildlife-manager"}
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 *SpeciesForZoneUpdateInput) (*ZoneHolder, error) {
zone, err := stores.NewZoneStore(relationalDB).RemoveSpeciesFromZone(input.credential.UserID, input.Body.ZoneID, input.Body.SpeciesID)
if err != nil {
return nil, handleError(err)
}
return &ZoneHolder{Body: zone}, nil
})
}
func (o *zoneOperations) RegisterDeactivate(api huma.API) {
name := "Deactivate Zone"
description := "Deactivate a zone."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{"land-user", "wildlife-manager"}
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 *ZoneDeactivateInput) (*ZoneHolder, error) {
zoneStore := stores.NewZoneStore(relationalDB)
zone, err := zoneStore.Get(input.ID)
if zone.User.ID != input.credential.UserID {
return nil, generateNotFoundForThisUserError(o.Endpoint, input.ID)
}
result, err := zoneStore.Deactivate(zone.ID)
if err != nil {
return nil, handleError(err)
}
return &ZoneHolder{Body: result}, nil
})
}