-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
163 lines (143 loc) · 4.57 KB
/
model.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package usergrp
import (
"fmt"
"net/mail"
"time"
"github.com/aleury/service/business/core/user"
"github.com/aleury/service/business/core/usersummary"
"github.com/aleury/service/business/sys/validate"
)
// AppUser represents information about an individual user.
type AppUser struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Roles []string `json:"roles"`
PasswordHash []byte `json:"-"`
Department string `json:"department"`
Enabled bool `json:"enabled"`
DateCreated string `json:"dateCreated"`
DateUpdated string `json:"dateUpdated"`
}
func toAppUser(usr user.User) AppUser {
roles := make([]string, len(usr.Roles))
for i, role := range usr.Roles {
roles[i] = role.Name()
}
return AppUser{
ID: usr.ID.String(),
Name: usr.Name,
Email: usr.Email.Address,
Roles: roles,
PasswordHash: usr.PasswordHash,
Department: usr.Department,
Enabled: usr.Enabled,
DateCreated: usr.DateCreated.Format(time.RFC3339),
DateUpdated: usr.DateUpdated.Format(time.RFC3339),
}
}
// =============================================================================
// AppNewUser contains information needed to create a new user.
type AppNewUser struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
Roles []string `json:"roles" validate:"required"`
Department string `json:"department"`
Password string `json:"password" validate:"required"`
PasswordConfirm string `json:"passwordConfirm" validate:"eqfield=Password"`
}
func toCoreNewUser(appUsr AppNewUser) (user.NewUser, error) {
roles := make([]user.Role, len(appUsr.Roles))
for i, roleStr := range appUsr.Roles {
role, err := user.ParseRole(roleStr)
if err != nil {
return user.NewUser{}, fmt.Errorf("parsing role: %w", err)
}
roles[i] = role
}
addr, err := mail.ParseAddress(appUsr.Email)
if err != nil {
return user.NewUser{}, fmt.Errorf("parsing email: %w", err)
}
usr := user.NewUser{
Name: appUsr.Name,
Email: *addr,
Roles: roles,
Department: appUsr.Department,
Password: appUsr.Password,
PasswordConfirm: appUsr.PasswordConfirm,
}
return usr, nil
}
// Validate checks the data in the model is considered clean.
func (appUsr AppNewUser) Validate() error {
if err := validate.Check(appUsr); err != nil {
return err
}
return nil
}
// =============================================================================
// AppUpdateUser contains information needed to update a user.
type AppUpdateUser struct {
Name *string `json:"name"`
Email *string `json:"email" validate:"omitempty,email"`
Roles []string `json:"roles"`
Department *string `json:"department"`
Password *string `json:"password"`
PasswordConfirm *string `json:"passwordConfirm" validate:"omitempty,eqfield=Password"`
Enabled *bool `json:"enabled"`
}
func toCoreUpdateUser(appUsr AppUpdateUser) (user.UpdateUser, error) {
var roles []user.Role
if appUsr.Roles != nil {
roles = make([]user.Role, len(appUsr.Roles))
for i, roleStr := range appUsr.Roles {
role, err := user.ParseRole(roleStr)
if err != nil {
return user.UpdateUser{}, fmt.Errorf("parsing role: %w", err)
}
roles[i] = role
}
}
var addr *mail.Address
if appUsr.Email != nil {
var err error
addr, err = mail.ParseAddress(*appUsr.Email)
if err != nil {
return user.UpdateUser{}, fmt.Errorf("parsing email: %w", err)
}
}
usr := user.UpdateUser{
Name: appUsr.Name,
Email: addr,
Roles: roles,
Department: appUsr.Department,
Password: appUsr.Password,
PasswordConfirm: appUsr.PasswordConfirm,
Enabled: appUsr.Enabled,
}
return usr, nil
}
// Validate checks the data in the model is considered clean.
func (appUsr AppUpdateUser) Validate() error {
if err := validate.Check(appUsr); err != nil {
return err
}
return nil
}
// =============================================================================
// AppSummary repesents informatin about an individual user and their products.
type AppSummary struct {
UserID string `json:"userId"`
UserName string `json:"userName"`
TotalCount int `json:"totalCount"`
TotalCost float64 `json:"totalCost"`
}
func toAppSummary(sum usersummary.Summary) AppSummary {
return AppSummary{
UserID: sum.UserID.String(),
UserName: sum.UserName,
TotalCount: sum.TotalCount,
TotalCost: sum.TotalCost,
}
}