-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathget_user.go
More file actions
62 lines (49 loc) · 1.28 KB
/
get_user.go
File metadata and controls
62 lines (49 loc) · 1.28 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
package user
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/get-eventually/go-eventually/core/aggregate"
"github.com/get-eventually/go-eventually/core/query"
"github.com/get-eventually/go-eventually/core/version"
)
var ErrEmptyID = errors.New("user: empty id provided")
type ViewModel struct {
Version version.Version
ID uuid.UUID
FirstName, LastName string
BirthDate time.Time
Email string
}
func buildViewModel(u *User) ViewModel {
return ViewModel{
Version: u.Version(),
ID: u.id,
FirstName: u.firstName,
LastName: u.lastName,
BirthDate: u.birthDate,
Email: u.email,
}
}
type GetQuery struct {
ID uuid.UUID
}
func (GetQuery) Name() string { return "GetUser" }
type GetQueryHandler struct {
Repository aggregate.Getter[uuid.UUID, *User]
}
func (h GetQueryHandler) Handle(ctx context.Context, q query.Envelope[GetQuery]) (ViewModel, error) {
makeError := func(err error) error {
return fmt.Errorf("user.GetQuery: failed to handle query, %w", err)
}
if q.Message.ID == uuid.Nil {
return ViewModel{}, makeError(ErrEmptyID)
}
user, err := h.Repository.Get(ctx, q.Message.ID)
if err != nil {
return ViewModel{}, makeError(err)
}
return buildViewModel(user), nil
}