···202 email text not null,
203 verified integer not null default 0,
204 verification_code text not null,
0205 is_primary integer not null default 0,
206 created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
207 unique(did, email)
···202 email text not null,
203 verified integer not null default 0,
204 verification_code text not null,
205+ last_sent text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
206 is_primary integer not null default 0,
207 created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
208 unique(did, email)
+51-6
appview/db/email.go
···12 Verified bool
13 Primary bool
14 VerificationCode string
015 CreatedAt time.Time
16}
1718func GetPrimaryEmail(e Execer, did string) (Email, error) {
19 query := `
20- select id, did, email, verified, is_primary, verification_code, created
21 from emails
22 where did = ? and is_primary = true
23 `
24 var email Email
25 var createdStr string
26- err := e.QueryRow(query, did).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr)
027 if err != nil {
28 return Email{}, err
29 }
30 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr)
31 if err != nil {
32 return Email{}, err
000000033 }
34 return email, nil
35}
3637func GetEmail(e Execer, did string, em string) (Email, error) {
38 query := `
39- select id, did, email, verified, is_primary, verification_code, created
40 from emails
41 where did = ? and email = ?
42 `
43 var email Email
44 var createdStr string
45- err := e.QueryRow(query, did, em).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr)
046 if err != nil {
47 return Email{}, err
48 }
49 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr)
50 if err != nil {
51 return Email{}, err
000000052 }
53 return email, nil
54}
···220221func GetAllEmails(e Execer, did string) ([]Email, error) {
222 query := `
223- select did, email, verified, is_primary, verification_code, created
224 from emails
225 where did = ?
226 `
···234 for rows.Next() {
235 var email Email
236 var createdStr string
237- err := rows.Scan(&email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr)
0238 if err != nil {
239 return nil, err
240 }
···242 if err != nil {
243 return nil, err
244 }
0000000245 emails = append(emails, email)
246 }
247 return emails, nil
248}
00000000000000000000
···12 Verified bool
13 Primary bool
14 VerificationCode string
15+ LastSent *time.Time
16 CreatedAt time.Time
17}
1819func GetPrimaryEmail(e Execer, did string) (Email, error) {
20 query := `
21+ select id, did, email, verified, is_primary, verification_code, last_sent, created
22 from emails
23 where did = ? and is_primary = true
24 `
25 var email Email
26 var createdStr string
27+ var lastSent *string
28+ err := e.QueryRow(query, did).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &lastSent, &createdStr)
29 if err != nil {
30 return Email{}, err
31 }
32 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr)
33 if err != nil {
34 return Email{}, err
35+ }
36+ if lastSent != nil {
37+ parsedTime, err := time.Parse(time.RFC3339, *lastSent)
38+ if err != nil {
39+ return Email{}, err
40+ }
41+ email.LastSent = &parsedTime
42 }
43 return email, nil
44}
4546func GetEmail(e Execer, did string, em string) (Email, error) {
47 query := `
48+ select id, did, email, verified, is_primary, verification_code, last_sent, created
49 from emails
50 where did = ? and email = ?
51 `
52 var email Email
53 var createdStr string
54+ var lastSent *string
55+ err := e.QueryRow(query, did, em).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &lastSent, &createdStr)
56 if err != nil {
57 return Email{}, err
58 }
59 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr)
60 if err != nil {
61 return Email{}, err
62+ }
63+ if lastSent != nil {
64+ parsedTime, err := time.Parse(time.RFC3339, *lastSent)
65+ if err != nil {
66+ return Email{}, err
67+ }
68+ email.LastSent = &parsedTime
69 }
70 return email, nil
71}
···237238func GetAllEmails(e Execer, did string) ([]Email, error) {
239 query := `
240+ select did, email, verified, is_primary, verification_code, last_sent, created
241 from emails
242 where did = ?
243 `
···251 for rows.Next() {
252 var email Email
253 var createdStr string
254+ var lastSent *string
255+ err := rows.Scan(&email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &lastSent, &createdStr)
256 if err != nil {
257 return nil, err
258 }
···260 if err != nil {
261 return nil, err
262 }
263+ if lastSent != nil {
264+ parsedTime, err := time.Parse(time.RFC3339, *lastSent)
265+ if err != nil {
266+ return nil, err
267+ }
268+ email.LastSent = &parsedTime
269+ }
270 emails = append(emails, email)
271 }
272 return emails, nil
273}
274+275+func UpdateVerificationCode(e Execer, did string, email string, code string) error {
276+ query := `
277+ update emails
278+ set verification_code = ?
279+ where did = ? and email = ?
280+ `
281+ _, err := e.Exec(query, code, did, email)
282+ return err
283+}
284+285+func UpdateLastSent(e Execer, did string, email string, lastSent time.Time) error {
286+ query := `
287+ update emails
288+ set last_sent = ?
289+ where did = ? and email = ?
290+ `
291+ _, err := e.Exec(query, lastSent.Format(time.RFC3339), did, email)
292+ return err
293+}