-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsLivingLab.go
88 lines (78 loc) · 2.96 KB
/
opsLivingLab.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type LivingLabHolder struct {
Body *models.LivingLab `json:"livinglab"`
}
type LivingLabsHolder struct {
Body []models.LivingLab `json:"livinglabs"`
}
type LivingLabAddInput struct {
Input
Body *models.LivingLab `json:"livinglab"`
}
type livinglabOperations Operations
func newLivingLabOperations() *livinglabOperations {
return &livinglabOperations{Endpoint: "livinglab"}
}
func (o *livinglabOperations) RegisterGet(api huma.API) {
name := "Get LivingLab By ID"
description := "Retrieve a specific living lab 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" format:"uuid" doc:"The ID of the livinglab."`
}) (*LivingLabHolder, error) {
livinglab, err := stores.NewLivingLabStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if livinglab == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &LivingLabHolder{Body: livinglab}, nil
})
}
func (o *livinglabOperations) RegisterGetAll(api huma.API) {
name := "Get all LivingLabs"
description := "Retrieve all living labs."
path := "/" + o.Endpoint + "s/"
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{}) (output *LivingLabsHolder, err error) {
livinglabs, err := stores.NewLivingLabStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &LivingLabsHolder{Body: livinglabs}, nil
})
}
func (o *livinglabOperations) RegisterAdd(api huma.API) {
name := "Add New LivingLab"
description := "Submit a new living lab."
path := "/" + o.Endpoint + "/"
scopes := []string{"administrator"}
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 *LivingLabAddInput) (*LivingLabHolder, error) {
if len(input.Body.Definition) < 3 {
return nil, huma.Error400BadRequest("definition should contain 3 or more points")
}
livinglab, err := stores.NewLivingLabStore(relationalDB).Add(input.Body)
if err != nil {
return nil, handleError(err)
}
return &LivingLabHolder{Body: livinglab}, nil
})
}