GetAccountsByIds
This commit is contained in:
parent
cdda4b1c9c
commit
d56343f167
|
@ -244,8 +244,67 @@ func (psql PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error
|
|||
return accounts, nil
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
func (psql PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error) {
|
||||
var accounts []Account
|
||||
accountIdsStr := "'" + strings.Join(accountids, "', '") + "'"
|
||||
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.id IN (%s)
|
||||
`
|
||||
query = fmt.Sprintf(query, accountIdsStr)
|
||||
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 (psql PostgresqlStorage) CreateAccount(account Account) error {
|
||||
|
|
|
@ -277,3 +277,49 @@ func TestPostgresqlStorage_GetAccounts(t *testing.T) {
|
|||
fmt.Println(account)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresqlStorage_GetAccountsByIds(t *testing.T) {
|
||||
db, err := NewPostgresqlStorage(cfg)
|
||||
if err != nil {
|
||||
t.Errorf("failed to create new psql connection")
|
||||
}
|
||||
account := Account{
|
||||
ID: "772315f1-8113-486a-90c7-9073410065bd",
|
||||
Namespace: "oo",
|
||||
Authentication: AccountAuth{
|
||||
Local: LocalAuth{
|
||||
Username: "username",
|
||||
Password: "password",
|
||||
Email: "salim@test.com",
|
||||
EmailValidation: Validation{
|
||||
Validated: true,
|
||||
ValidationCode: "123",
|
||||
},
|
||||
PhoneNumber: "12345",
|
||||
PhoneNumberValidation: Validation{
|
||||
Validated: true,
|
||||
ValidationCode: "1233",
|
||||
},
|
||||
},
|
||||
},
|
||||
Data: map[string]any{
|
||||
"key1": "salim1",
|
||||
"key2": "salim2",
|
||||
},
|
||||
Metadata: map[string]any{
|
||||
"key1": "salim1",
|
||||
"key2": "salim2",
|
||||
},
|
||||
}
|
||||
err = db.CreateAccount(account)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create account")
|
||||
}
|
||||
accounts, err := db.GetAccountsByIds([]string{"772315f1-8113-486a-90c7-9073410065bd"})
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get account by ID")
|
||||
}
|
||||
for _, acc := range accounts {
|
||||
fmt.Println(acc)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue