-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsProfile.go
102 lines (91 loc) · 3.51 KB
/
opsProfile.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type ProfileHolder struct {
Body *models.Profile `json:"profile"`
}
type UpdateProfileHolder struct {
Input
Body *models.ProfileRecord `json:"profile"`
}
type ProfilesHolder struct {
Body []models.Profile `json:"profiles"`
}
type profileOperations Operations
func newProfileOperations() *profileOperations {
return &profileOperations{Endpoint: "profile"}
}
func (o *profileOperations) RegisterGet(api huma.API) {
name := "Get Profile By ID"
description := "Retrieve a specific profile by ID."
path := "/" + o.Endpoint + "/{id}"
scopes := []string{"administrator"}
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 profile."`
}) (*ProfileHolder, error) {
profile, err := stores.NewProfileStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if profile == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &ProfileHolder{Body: profile}, nil
})
}
func (o *profileOperations) RegisterGetAll(api huma.API) {
name := "Get all Profiles"
description := "Retrieve all profiles."
path := "/" + o.Endpoint + "s/"
scopes := []string{"administrator"}
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{}) (*ProfilesHolder, error) {
profiles, err := stores.NewProfileStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &ProfilesHolder{Body: profiles}, nil
})
}
func (o *profileOperations) RegisterGetMine(api huma.API) {
name := "Get My Profile"
description := "Retrieve the profile for the current user."
path := "/" + o.Endpoint + "/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) (*ProfileHolder, error) {
me, err := stores.NewProfileStore(relationalDB).Get(input.credential.UserID)
if err != nil {
return nil, handleError(err)
}
return &ProfileHolder{Body: me}, nil
})
}
func (o *profileOperations) RegisterUpdateMine(api huma.API) {
name := "Update My Profile"
description := "Update the profile for the current user."
path := "/" + o.Endpoint + "/me/"
scopes := []string{}
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 *UpdateProfileHolder) (*ProfileHolder, error) {
me, err := stores.NewProfileStore(relationalDB).Update(input.credential.UserID, input.Body)
if err != nil {
return nil, handleError(err)
}
return &ProfileHolder{Body: me}, nil
})
}