-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsUser.go
63 lines (55 loc) · 1.89 KB
/
opsUser.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
package wildlifenl
import (
"context"
"net/http"
"github.com/UtrechtUniversity/wildlifenl/models"
"github.com/UtrechtUniversity/wildlifenl/stores"
"github.com/danielgtaylor/huma/v2"
)
type UserHolder struct {
Body *models.User `json:"user"`
}
type UsersHolder struct {
Body []models.User `json:"users"`
}
type userOperations Operations
func newUserOperations() *userOperations {
return &userOperations{Endpoint: "user"}
}
func (o *userOperations) RegisterGet(api huma.API) {
name := "Get User By ID"
description := "Retrieve a specific user 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 user."`
}) (*UserHolder, error) {
user, err := stores.NewUserStore(relationalDB).Get(input.ID)
if err != nil {
return nil, handleError(err)
}
if user == nil {
return nil, generateNotFoundByIDError(o.Endpoint, input.ID)
}
return &UserHolder{Body: user}, nil
})
}
func (o *userOperations) RegisterGetAll(api huma.API) {
name := "Get all Users"
description := "Retrieve all users."
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{}) (*UsersHolder, error) {
users, err := stores.NewUserStore(relationalDB).GetAll()
if err != nil {
return nil, handleError(err)
}
return &UsersHolder{Body: users}, nil
})
}