forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
160 lines (125 loc) · 5.23 KB
/
users.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
package models
import (
"errors"
"fmt"
"time"
"github.com/ardanlabs/gotraining/topics/http/api/app"
)
// UserAddress contains information about a user's address.
type UserAddress struct {
Type int `bson:"type" json:"type" validate:"required"`
LineOne string `bson:"line_one" json:"line_one" validate:"required"`
LineTwo string `bson:"line_two" json:"line_two,omitempty"`
City string `bson:"city" json:"city" validate:"required"`
State string `bson:"state" json:"state" validate:"required"`
Zipcode string `bson:"zipcode" json:"zipcode" validate:"required"`
Phone string `bson:"phone" json:"phone" validate:"required"`
DateModified *time.Time `bson:"date_modified" json:"date_modified"`
DateCreated *time.Time `bson:"date_created,omitempty" json:"date_created"`
}
// Validate checks the fields to verify the value is in a proper state.
func (ua *UserAddress) Validate() ([]app.Invalid, error) {
var inv []app.Invalid
errs := validate.Struct(ua)
if errs != nil {
for _, err := range errs {
inv = append(inv, app.Invalid{Fld: err.Field, Err: err.Tag})
}
return inv, errors.New("Validation failures identified")
}
return nil, nil
}
// Compare checks the fields against another UserAddress value.
func (ua *UserAddress) Compare(uat *UserAddress) ([]app.Invalid, error) {
var inv []app.Invalid
if ua.Type != uat.Type {
inv = append(inv, app.Invalid{Fld: "Type", Err: fmt.Sprintf("The value of Type is not the same. %d != %d", ua.Type, uat.Type)})
}
if ua.LineOne != uat.LineOne {
inv = append(inv, app.Invalid{Fld: "LineOne", Err: fmt.Sprintf("The value of LineOne is not the same. %s != %s", ua.LineOne, uat.LineOne)})
}
if ua.City != uat.City {
inv = append(inv, app.Invalid{Fld: "City", Err: fmt.Sprintf("The value of City is not the same. %s != %s", ua.City, uat.City)})
}
if ua.State != uat.State {
inv = append(inv, app.Invalid{Fld: "State", Err: fmt.Sprintf("The value of State is not the same. %s != %s", ua.State, uat.State)})
}
if ua.Zipcode != uat.Zipcode {
inv = append(inv, app.Invalid{Fld: "Zipcode", Err: fmt.Sprintf("The value of Zipcode is not the same. %s != %s", ua.Zipcode, uat.Zipcode)})
}
if ua.Phone != uat.Phone {
inv = append(inv, app.Invalid{Fld: "Phone", Err: fmt.Sprintf("The value of Phone is not the same. %s != %s", ua.Phone, uat.Phone)})
}
if len(inv) > 0 {
return inv, errors.New("Compare failures identified")
}
return nil, nil
}
// User contains information about a user.
type User struct {
UserID string `bson:"user_id,omitempty" json:"user_id,omitempty"`
UserType int `bson:"type" json:"type" validate:"required"`
FirstName string `bson:"first_name" json:"first_name" validate:"required"`
LastName string `bson:"last_name" json:"last_name" validate:"required"`
Email string `bson:"email" json:"email" validate:"required"`
Company string `bson:"company" json:"company" validate:"required"`
Addresses []UserAddress `bson:"addresses" json:"addresses" validate:"required"`
DateModified *time.Time `bson:"date_modified" json:"date_modified"`
DateCreated *time.Time `bson:"date_created,omitempty" json:"date_created"`
}
// Validate checks the fields to verify the value is in a proper state.
func (u *User) Validate() ([]app.Invalid, error) {
var inv []app.Invalid
errs := validate.Struct(u)
if errs != nil {
for _, err := range errs {
inv = append(inv, app.Invalid{Fld: err.Field, Err: err.Tag})
}
return inv, errors.New("Validation failures identified")
}
for _, ua := range u.Addresses {
if va, err := ua.Validate(); err != nil {
inv = append(inv, va...)
}
}
if len(inv) > 0 {
return inv, errors.New("Validation failures identified")
}
return nil, nil
}
// Compare checks the fields against another User value.
func (u *User) Compare(ut *User) ([]app.Invalid, error) {
var inv []app.Invalid
if u.UserType != ut.UserType {
inv = append(inv, app.Invalid{Fld: "UserType", Err: fmt.Sprintf("The value of UserType is not the same. %d != %d", u.UserType, ut.UserType)})
}
if u.FirstName != ut.FirstName {
inv = append(inv, app.Invalid{Fld: "FirstName", Err: fmt.Sprintf("The value of FirstName is not the same. %s != %s", u.FirstName, ut.FirstName)})
}
if u.LastName != ut.LastName {
inv = append(inv, app.Invalid{Fld: "LastName", Err: fmt.Sprintf("The value of LastName is not the same. %s != %s", u.LastName, ut.LastName)})
}
if u.Email != ut.Email {
inv = append(inv, app.Invalid{Fld: "Email", Err: fmt.Sprintf("The value of Email is not the same. %s != %s", u.Email, ut.Email)})
}
if u.Company != ut.Company {
inv = append(inv, app.Invalid{Fld: "Company", Err: fmt.Sprintf("The value of Company is not the same. %s != %s", u.Company, ut.Company)})
}
uLen := len(u.Addresses)
utLen := len(ut.Addresses)
if uLen != utLen {
inv = append(inv, app.Invalid{Fld: "Addresses", Err: fmt.Sprintf("The set of Addresses is not the same. %d != %d", uLen, utLen)})
}
for idx, ua := range u.Addresses {
if idx >= utLen {
break
}
if va, err := ua.Compare(&ut.Addresses[idx]); err != nil {
inv = append(inv, va...)
}
}
if len(inv) > 0 {
return inv, errors.New("Compare failures identified")
}
return nil, nil
}