2018-05-27 09:36:35 +00:00
|
|
|
package common
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
|
|
|
var Emails EmailStore
|
|
|
|
|
|
|
|
type EmailStore interface {
|
|
|
|
GetEmailsByUser(user *User) (emails []Email, err error)
|
|
|
|
VerifyEmail(email string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultEmailStore struct {
|
|
|
|
getEmailsByUser *sql.Stmt
|
|
|
|
verifyEmail *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) {
|
|
|
|
return &DefaultEmailStore{
|
|
|
|
getEmailsByUser: acc.Select("emails").Columns("email, validated, token").Where("uid = ?").Prepare(),
|
|
|
|
|
|
|
|
// Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
|
|
|
|
verifyEmail: acc.Update("emails").Set("validated = 1, token = ''").Where("email = ?").Prepare(),
|
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) {
|
2019-09-29 04:56:39 +00:00
|
|
|
e := Email{UserID: user.ID}
|
2019-07-26 22:26:52 +00:00
|
|
|
rows, err := s.getEmailsByUser.Query(user.ID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return emails, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-09-29 04:56:39 +00:00
|
|
|
err := rows.Scan(&e.Email, &e.Validated, &e.Token)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return emails, err
|
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
if e.Email == user.Email {
|
|
|
|
e.Primary = true
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-09-29 04:56:39 +00:00
|
|
|
emails = append(emails, e)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return emails, rows.Err()
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
func (s *DefaultEmailStore) VerifyEmail(email string) error {
|
|
|
|
_, err := s.verifyEmail.Exec(email)
|
2018-05-27 09:36:35 +00:00
|
|
|
return err
|
|
|
|
}
|