this repo has no description
1package db 2 3import ( 4 "strings" 5 "time" 6) 7 8type Email struct { 9 ID int64 10 Did string 11 Address string 12 Verified bool 13 Primary bool 14 VerificationCode string 15 CreatedAt time.Time 16} 17 18func 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) 27 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 33 } 34 return email, nil 35} 36 37func 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) 46 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 52 } 53 return email, nil 54} 55 56func GetDidForEmail(e Execer, em string) (string, error) { 57 query := ` 58 select did 59 from emails 60 where email = ? 61 ` 62 var did string 63 err := e.QueryRow(query, em).Scan(&did) 64 if err != nil { 65 return "", err 66 } 67 return did, nil 68} 69 70func GetDidsForEmails(e Execer, ems []string) ([]string, error) { 71 if len(ems) == 0 { 72 return []string{}, nil 73 } 74 75 // Create placeholders for the IN clause 76 placeholders := make([]string, len(ems)) 77 args := make([]interface{}, len(ems)) 78 for i, em := range ems { 79 placeholders[i] = "?" 80 args[i] = em 81 } 82 83 query := ` 84 select did 85 from emails 86 where email in (` + strings.Join(placeholders, ",") + `) 87 ` 88 89 rows, err := e.Query(query, args...) 90 if err != nil { 91 return nil, err 92 } 93 defer rows.Close() 94 95 var dids []string 96 for rows.Next() { 97 var did string 98 if err := rows.Scan(&did); err != nil { 99 return nil, err 100 } 101 dids = append(dids, did) 102 } 103 104 if err := rows.Err(); err != nil { 105 return nil, err 106 } 107 108 return dids, nil 109} 110 111func GetVerificationCodeForEmail(e Execer, did string, email string) (string, error) { 112 query := ` 113 select verification_code 114 from emails 115 where did = ? and email = ? 116 ` 117 var code string 118 err := e.QueryRow(query, did, email).Scan(&code) 119 if err != nil { 120 return "", err 121 } 122 return code, nil 123} 124 125func CheckEmailExists(e Execer, did string, email string) (bool, error) { 126 query := ` 127 select count(*) 128 from emails 129 where did = ? and email = ? 130 ` 131 var count int 132 err := e.QueryRow(query, did, email).Scan(&count) 133 if err != nil { 134 return false, err 135 } 136 return count > 0, nil 137} 138 139func CheckValidVerificationCode(e Execer, did string, email string, code string) (bool, error) { 140 query := ` 141 select count(*) 142 from emails 143 where did = ? and email = ? and verification_code = ? 144 ` 145 var count int 146 err := e.QueryRow(query, did, email, code).Scan(&count) 147 if err != nil { 148 return false, err 149 } 150 return count > 0, nil 151} 152 153func AddEmail(e Execer, email Email) error { 154 // Check if this is the first email for this DID 155 countQuery := ` 156 select count(*) 157 from emails 158 where did = ? 159 ` 160 var count int 161 err := e.QueryRow(countQuery, email.Did).Scan(&count) 162 if err != nil { 163 return err 164 } 165 166 // If this is the first email, mark it as primary 167 if count == 0 { 168 email.Primary = true 169 } 170 171 query := ` 172 insert into emails (did, email, verified, is_primary, verification_code) 173 values (?, ?, ?, ?, ?) 174 ` 175 _, err = e.Exec(query, email.Did, email.Address, email.Verified, email.Primary, email.VerificationCode) 176 return err 177} 178 179func DeleteEmail(e Execer, did string, email string) error { 180 query := ` 181 delete from emails 182 where did = ? and email = ? 183 ` 184 _, err := e.Exec(query, did, email) 185 return err 186} 187 188func MarkEmailVerified(e Execer, did string, email string) error { 189 query := ` 190 update emails 191 set verified = true 192 where did = ? and email = ? 193 ` 194 _, err := e.Exec(query, did, email) 195 return err 196} 197 198func MakeEmailPrimary(e Execer, did string, email string) error { 199 // First, unset all primary emails for this DID 200 query1 := ` 201 update emails 202 set is_primary = false 203 where did = ? 204 ` 205 _, err := e.Exec(query1, did) 206 if err != nil { 207 return err 208 } 209 210 // Then, set the specified email as primary 211 query2 := ` 212 update emails 213 set is_primary = true 214 where did = ? and email = ? 215 ` 216 _, err = e.Exec(query2, did, email) 217 return err 218} 219 220func GetAllEmails(e Execer, did string) ([]Email, error) { 221 query := ` 222 select did, email, verified, is_primary, verification_code, created 223 from emails 224 where did = ? 225 ` 226 rows, err := e.Query(query, did) 227 if err != nil { 228 return nil, err 229 } 230 defer rows.Close() 231 232 var emails []Email 233 for rows.Next() { 234 var email Email 235 var createdStr string 236 err := rows.Scan(&email.Did, &email.Address, &email.Verified, &email.Primary, &email.VerificationCode, &createdStr) 237 if err != nil { 238 return nil, err 239 } 240 email.CreatedAt, err = time.Parse(time.RFC3339, createdStr) 241 if err != nil { 242 return nil, err 243 } 244 emails = append(emails, email) 245 } 246 return emails, nil 247}