-
Notifications
You must be signed in to change notification settings - Fork 9
/
user.go
183 lines (158 loc) · 4.59 KB
/
user.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bytes"
"log"
"strconv"
)
type (
User struct {
Id int32
Email []byte
FirstName []byte
LastName []byte
Gender byte
BirthDate int64
birthdateSetted bool
cache UserVisits
}
)
func (u *User) Parse(buf []byte) bool {
changed := false
u.birthdateSetted = false
err := ParseItem(buf, func(key, value []byte, valueType JSValueType) bool {
changed = true // проверка, что не совсем пустой buf пришел
if bytes.Equal(key, strId) {
if valueType != jsValueTypeNumeric {
return false
} else if i64, ok := byteSliceToInt64(value); !ok {
return false
} else {
u.Id = int32(i64)
}
} else if bytes.Equal(key, strEmail) {
if valueType != jsValueTypeString {
return false
}
u.Email = append(u.Email[0:0], value...)
} else if bytes.Equal(key, strFirstName) {
if valueType != jsValueTypeString {
return false
}
u.FirstName = append(u.FirstName[0:0], value...)
} else if bytes.Equal(key, strLastName) {
if valueType != jsValueTypeString {
return false
}
u.LastName = append(u.LastName[0:0], value...)
} else if bytes.Equal(key, strGender) {
if (valueType != jsValueTypeString) || (len(value) != 1) {
return false
}
u.Gender = value[0]
} else if bytes.Equal(key, strBirthdate) {
if valueType != jsValueTypeNumeric {
return false
} else if i64, ok := byteSliceToInt64(value); !ok {
return false
} else {
u.BirthDate = i64
u.birthdateSetted = true
}
} // else { // неизвестный ключ
return true
})
return (err == nil) && changed
}
func (u *User) Serialize(buf []byte) []byte {
buf = append(buf, `{"id":`...)
buf = strconv.AppendInt(buf, int64(u.Id), 10)
buf = append(buf, `,"email":"`...)
buf = append(buf, u.Email...)
buf = append(buf, `","first_name":"`...)
buf = append(buf, u.FirstName...)
buf = append(buf, `","last_name":"`...)
buf = append(buf, u.LastName...)
buf = append(buf, `","gender":"`...)
buf = append(buf, u.Gender)
buf = append(buf, `","birth_date":`...)
buf = strconv.AppendInt(buf, int64(u.BirthDate), 10)
buf = append(buf, '}')
return buf
}
func (u *User) CheckFields(update bool) bool {
/*if utf8EscapedStringLen(u.Email) > 100 {
return false
} else if utf8EscapedStringLen(u.FirstName) > 50 {
return false
} else if utf8EscapedStringLen(u.LastName) > 50 {
return false
} else*/
if u.Gender != 0 && u.Gender != 'm' && u.Gender != 'f' {
return false
}
if !update && (len(u.Email) == 0 || len(u.FirstName) == 0 || len(u.LastName) == 0 || u.Gender == 0 || !u.birthdateSetted) {
// при создании должны передаваться все поля
return false
}
if update && (u.Id != 0) {
// id не может обновляться
return false
}
return true
}
func (u *User) Update(update *User) bool {
/*
Если меняется Gender:
- в LocationsAvg обновить поле gender для: Visit(User.cache.visitId) => Location(Visit.Location).cache.gender
Если меняется birthdate:
- в LocationsAvg обновить поле birthdate для: Visit(User.cache.visitId) => Location(Visit.Location).cache.birthdate
*/
if len(update.Email) != 0 {
u.Email = update.Email
}
if len(update.FirstName) != 0 {
u.FirstName = update.FirstName
}
if len(update.LastName) != 0 {
u.LastName = update.LastName
}
if update.Gender != 0 && (u.Gender != update.Gender) {
u.Gender = update.Gender
u.cacheUpdateGender()
}
if update.birthdateSetted && (u.BirthDate != update.BirthDate) {
u.BirthDate = update.BirthDate
u.cacheUpdateBirthdate()
}
return true
}
func (u *User) cacheUpdateBirthdate() {
for _, uv := range u.cache.visits {
if visit := indexVisit.Get(uv.visitId); visit == nil {
log.Println(`WTF visit nil in user cache`, uv.visitId)
} else if location := indexLocation.Get(visit.Location); location == nil {
log.Println(`WTF location nil in user cache`, visit.Location)
} else {
for i, la := range location.cache.locations {
if la.visitId == visit.Id {
location.cache.locations[i].birthdate = u.BirthDate
}
}
}
}
}
func (u *User) cacheUpdateGender() {
for _, uv := range u.cache.visits {
if visit := indexVisit.Get(uv.visitId); visit == nil {
log.Println(`WTF visit nil in user cache`, uv.visitId)
} else if location := indexLocation.Get(visit.Location); location == nil {
log.Println(`WTF location nil in user cache`, visit.Location)
} else {
for i, la := range location.cache.locations {
if la.visitId == visit.Id {
location.cache.locations[i].gender = u.Gender
}
}
}
}
}