this repo has no description
1package db 2 3import "time" 4 5type Email struct { 6 ID int64 7 Did string 8 Address string 9 Verified bool 10 Primary bool 11 VerificationCode string 12 CreatedAt time.Time 13} 14 15func GetPrimaryEmail(e Execer, did string) (Email, error) { 16 query := ` 17 select id, did, email, verified, is_primary, verification_code, created 18 from emails 19 where did = ? and is_primary = true 20 ` 21 var email Email 22 var createdStr string 23 err := e.QueryRow(query, did).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr) 24 if err != nil { 25 return Email{}, err 26 } 27 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr) 28 if err != nil { 29 return Email{}, err 30 } 31 return email, nil 32} 33 34func GetEmail(e Execer, did string, em string) (Email, error) { 35 query := ` 36 select id, did, email, verified, is_primary, verification_code, created 37 from emails 38 where did = ? and email = ? 39 ` 40 var email Email 41 var createdStr string 42 err := e.QueryRow(query, did, em).Scan(&email.ID, &email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr) 43 if err != nil { 44 return Email{}, err 45 } 46 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr) 47 if err != nil { 48 return Email{}, err 49 } 50 return email, nil 51} 52 53func GetDidForEmail(e Execer, em string) (string, error) { 54 query := ` 55 select did 56 from emails 57 where email = ? 58 ` 59 var did string 60 err := e.QueryRow(query, em).Scan(&did) 61 if err != nil { 62 return "", err 63 } 64 return did, nil 65} 66 67func GetVerificationCodeForEmail(e Execer, did string, email string) (string, error) { 68 query := ` 69 select verification_code 70 from emails 71 where did = ? and email = ? 72 ` 73 var code string 74 err := e.QueryRow(query, did, email).Scan(&code) 75 if err != nil { 76 return "", err 77 } 78 return code, nil 79} 80 81func CheckEmailExists(e Execer, did string, email string) (bool, error) { 82 query := ` 83 select count(*) 84 from emails 85 where did = ? and email = ? 86 ` 87 var count int 88 err := e.QueryRow(query, did, email).Scan(&count) 89 if err != nil { 90 return false, err 91 } 92 return count > 0, nil 93} 94 95func CheckValidVerificationCode(e Execer, did string, email string, code string) (bool, error) { 96 query := ` 97 select count(*) 98 from emails 99 where did = ? and email = ? and verification_code = ? 100 ` 101 var count int 102 err := e.QueryRow(query, did, email, code).Scan(&count) 103 if err != nil { 104 return false, err 105 } 106 return count > 0, nil 107} 108 109func AddEmail(e Execer, email Email) error { 110 // Check if this is the first email for this DID 111 countQuery := ` 112 select count(*) 113 from emails 114 where did = ? 115 ` 116 var count int 117 err := e.QueryRow(countQuery, email.Did).Scan(&count) 118 if err != nil { 119 return err 120 } 121 122 // If this is the first email, mark it as primary 123 if count == 0 { 124 email.Primary = true 125 } 126 127 query := ` 128 insert into emails (did, email, verified, is_primary, verification_code) 129 values (?, ?, ?, ?, ?) 130 ` 131 _, err = e.Exec(query, email.Did, email.Address, email.Verified, email.Primary, email.VerificationCode) 132 return err 133} 134 135func DeleteEmail(e Execer, did string, email string) error { 136 query := ` 137 delete from emails 138 where did = ? and email = ? 139 ` 140 _, err := e.Exec(query, did, email) 141 return err 142} 143 144func MarkEmailVerified(e Execer, did string, email string) error { 145 query := ` 146 update emails 147 set verified = true 148 where did = ? and email = ? 149 ` 150 _, err := e.Exec(query, did, email) 151 return err 152} 153 154func MakeEmailPrimary(e Execer, did string, email string) error { 155 // First, unset all primary emails for this DID 156 query1 := ` 157 update emails 158 set is_primary = false 159 where did = ? 160 ` 161 _, err := e.Exec(query1, did) 162 if err != nil { 163 return err 164 } 165 166 // Then, set the specified email as primary 167 query2 := ` 168 update emails 169 set is_primary = true 170 where did = ? and email = ? 171 ` 172 _, err = e.Exec(query2, did, email) 173 return err 174} 175 176func GetAllEmails(e Execer, did string) ([]Email, error) { 177 query := ` 178 select did, email, verified, is_primary, verification_code, created 179 from emails 180 where did = ? 181 ` 182 rows, err := e.Query(query, did) 183 if err != nil { 184 return nil, err 185 } 186 defer rows.Close() 187 188 var emails []Email 189 for rows.Next() { 190 var email Email 191 var createdStr string 192 err := rows.Scan(&email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr) 193 if err != nil { 194 return nil, err 195 } 196 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr) 197 if err != nil { 198 return nil, err 199 } 200 emails = append(emails, email) 201 } 202 return emails, nil 203}