chore: get users prim mail
This commit is contained in:
parent
47affd6fe5
commit
12d8c145af
2 changed files with 65 additions and 0 deletions
pkg/userstore
|
@ -1,6 +1,10 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.keks.cloud/kekskurse/miniauth/pkg/utils"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
@ -46,3 +50,41 @@ func (s Store) MailRemove(email string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Mail struct {
|
||||
ID int
|
||||
Mail string
|
||||
UserID int
|
||||
ValidationCode string
|
||||
IsPrimary bool
|
||||
IsValidated bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (s Store) MailGetPrimaryForUsername(username string) (Mail, error) {
|
||||
query := fmt.Sprintf("SELECT %v FROM mail WHERE user_id = (SELECT id FROM users WHERE username = ? LIMIT 1) AND is_primary = ?", s.mailGetFields())
|
||||
log := s.log.With().Str("func", "MailGetPrimaryForUsername").Str("username", username).Str("query", query).Logger()
|
||||
|
||||
row := s.db.QueryRow(query, username, 1)
|
||||
mail, err := s.mailScan(row)
|
||||
if err != nil {
|
||||
return Mail{}, utils.WrapError(ErrCantExecuteQuery, err, log)
|
||||
}
|
||||
|
||||
return mail, nil
|
||||
}
|
||||
|
||||
func (s Store) mailGetFields() string {
|
||||
return "id, created_at, updated_at, mail, user_id, validation_code, is_validated, is_primary"
|
||||
}
|
||||
|
||||
func (s Store) mailScan(row *sql.Row) (Mail, error) {
|
||||
mail := Mail{}
|
||||
err := row.Scan(&mail.ID, &mail.CreatedAt, &mail.UpdatedAt, &mail.Mail, &mail.UserID, &mail.ValidationCode, &mail.IsValidated, &mail.IsPrimary)
|
||||
if err != nil {
|
||||
return Mail{}, err
|
||||
}
|
||||
|
||||
return mail, nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package userstore
|
|||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -122,3 +123,25 @@ func TestDeleteMail(t *testing.T) {
|
|||
assert.Nil(t, err, "should execute count query for mail")
|
||||
assert.Equal(t, 1, count, "should found 1 mails in the database")
|
||||
}
|
||||
|
||||
func TestGetPrimaryMailForUser(t *testing.T) {
|
||||
store, err := NewDummyStore()
|
||||
assert.Nil(t, err, "[setup] should be abel to create dummystore")
|
||||
|
||||
_, err = store.UserWrite("kekskurse", "kekskurse")
|
||||
assert.Nil(t, err, "[setup] should be abel to store user")
|
||||
|
||||
err = store.MailAdd("mail1@example.com", 1, true)
|
||||
assert.Nil(t, err, "[setup] should be abel to store mail")
|
||||
err = store.MailAdd("mail2@example.com", 1, false)
|
||||
assert.Nil(t, err, "[setup] should be abel to store mail2")
|
||||
|
||||
mail, err := store.MailGetPrimaryForUsername("kekskurse")
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, "mail1@example.com", mail.Mail)
|
||||
assert.Equal(t, true, mail.IsPrimary)
|
||||
assert.Equal(t, false, mail.IsValidated)
|
||||
assert.Equal(t, time.Now().Year(), mail.CreatedAt.Year())
|
||||
assert.Equal(t, time.Now().Year(), mail.UpdatedAt.Year())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue