-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
124 lines (102 loc) · 2.96 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
package usergrp
import (
"github.com/nhaancs/bhms/business/core/user"
"github.com/nhaancs/bhms/foundation/validate"
"time"
)
type AppVerifyOTP struct {
UserID string `json:"user_id" validate:"required,uuid"`
OTP string `json:"otp" validate:"required,len=6"`
}
// Validate checks the data in the model is considered clean.
func (r AppVerifyOTP) Validate() error {
if err := validate.Check(r); err != nil {
return err
}
return nil
}
// ===============================================================
// AppRegister contains information needed for a new user to register.
type AppRegister struct {
FirstName string `json:"firstName" validate:"required"`
LastName string `json:"lastName"`
Phone string `json:"phone" validate:"required,number,startswith=0,len=10"`
Password string `json:"password" validate:"required,min=6"`
}
func toCoreNewUser(a AppRegister) (user.NewUser, error) {
usr := user.NewUser{
FirstName: a.FirstName,
LastName: a.LastName,
Phone: a.Phone,
Password: a.Password,
Status: user.StatusCreated,
Roles: []user.Role{user.RoleUser},
}
return usr, nil
}
// Validate checks the data in the model is considered clean.
func (r AppRegister) Validate() error {
if err := validate.Check(r); err != nil {
return err
}
return nil
}
// ===============================================================
type AppUpdateUser struct {
FirstName *string `json:"firstName"`
LastName *string `json:"lastName"`
Password *string `json:"password" validate:"omitempty,min=6"`
}
func toCoreUpdateUser(a AppUpdateUser) user.UpdateUser {
return user.UpdateUser{
FirstName: a.FirstName,
LastName: a.LastName,
Password: a.Password,
}
}
// Validate checks the data in the model is considered clean.
func (r AppUpdateUser) Validate() error {
if err := validate.Check(r); err != nil {
return err
}
return nil
}
// ===============================================================
type token struct {
Token string `json:"token"`
}
func toToken(v string) token {
return token{
Token: v,
}
}
// ===============================================================
// AppUser represents information about an individual user.
type AppUser struct {
ID string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Phone string `json:"phone"`
Roles []string `json:"roles"`
PasswordHash []byte `json:"-"`
Status string `json:"status"`
CreatedAt string `json:"CreatedAt"`
UpdatedAt string `json:"UpdatedAt"`
}
func toAppUser(e user.User) AppUser {
roles := make([]string, len(e.Roles))
for i, role := range e.Roles {
roles[i] = role.Name()
}
return AppUser{
ID: e.ID.String(),
FirstName: e.FirstName,
LastName: e.LastName,
Phone: e.Phone,
PasswordHash: e.PasswordHash,
Roles: roles,
Status: e.Status.Name(),
CreatedAt: e.CreatedAt.Format(time.RFC3339),
UpdatedAt: e.UpdatedAt.Format(time.RFC3339),
}
}