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) {
|
2018-05-27 09:36:35 +00:00
|
|
|
email := 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() {
|
|
|
|
err := rows.Scan(&email.Email, &email.Validated, &email.Token)
|
|
|
|
if err != nil {
|
|
|
|
return emails, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if email.Email == user.Email {
|
|
|
|
email.Primary = true
|
|
|
|
}
|
|
|
|
emails = append(emails, email)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|