GetAccounts
This commit is contained in:
parent
ada858a738
commit
cdda4b1c9c
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PostgresqlStorage struct {
|
||||
|
@ -180,8 +181,67 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username str
|
|||
return account, nil
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
func (psql PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error) {
|
||||
var accounts []Account
|
||||
namespacesStr := "'" + strings.Join(namespaces, "', '") + "'"
|
||||
query := `
|
||||
SELECT accounts.id, accounts.namespace, accounts.data, accounts.metadata,
|
||||
account_auth.username, account_auth.password,
|
||||
account_auth.email, account_auth.email_validation,
|
||||
account_auth.phone_number, account_auth.phone_number_validation
|
||||
FROM accounts
|
||||
INNER JOIN account_auth ON accounts.id = account_auth.account_id
|
||||
WHERE accounts.namespace IN (%s)
|
||||
`
|
||||
query = fmt.Sprintf(query, namespacesStr)
|
||||
rows, err := psql.DbConnection.Query(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var account Account
|
||||
var dataBytes []byte
|
||||
var metadataBytes []byte
|
||||
var emailValidationBytes []byte
|
||||
var phoneNumberValidationBytes []byte
|
||||
err := rows.Scan(
|
||||
&account.ID,
|
||||
&account.Namespace,
|
||||
&dataBytes,
|
||||
&metadataBytes,
|
||||
&account.Authentication.Local.Username,
|
||||
&account.Authentication.Local.Password,
|
||||
&account.Authentication.Local.Email,
|
||||
&emailValidationBytes,
|
||||
&account.Authentication.Local.PhoneNumber,
|
||||
&phoneNumberValidationBytes,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(dataBytes, &account.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(metadataBytes, &account.Metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(emailValidationBytes, &account.Authentication.Local.EmailValidation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(phoneNumberValidationBytes, &account.Authentication.Local.PhoneNumberValidation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts = append(accounts, account)
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error) {
|
||||
|
|
|
@ -10,6 +10,8 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// Tests must be run in order
|
||||
|
||||
var cfg *viper.Viper
|
||||
|
||||
func init() {
|
||||
|
@ -23,8 +25,8 @@ func init() {
|
|||
|
||||
func TestNewPostgresqlStorage(t *testing.T) {
|
||||
storage, err := NewPostgresqlStorage(cfg)
|
||||
storage.DbConnection.Exec("Delete from accounts;")
|
||||
storage.DbConnection.Exec("Delete from account_auth;")
|
||||
storage.DbConnection.Exec("Delete from account_auth ;")
|
||||
storage.DbConnection.Exec("Delete from accounts ;")
|
||||
if err != nil {
|
||||
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||||
}
|
||||
|
@ -261,3 +263,17 @@ func TestPostgresqlStorage_LocalAuthentication(t *testing.T) {
|
|||
}
|
||||
fmt.Println(accountByAll)
|
||||
}
|
||||
|
||||
func TestPostgresqlStorage_GetAccounts(t *testing.T) {
|
||||
db, err := NewPostgresqlStorage(cfg)
|
||||
if err != nil {
|
||||
t.Errorf("failed to create new psql connection")
|
||||
}
|
||||
accounts, err := db.GetAccounts([]string{"test_namespace", "salim", "namespace"})
|
||||
if err != nil {
|
||||
t.Errorf("Failed")
|
||||
}
|
||||
for _, account := range accounts {
|
||||
fmt.Println(account)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue