From d56343f167a0e8c32a44d0e87ad432832a55bf04 Mon Sep 17 00:00:00 2001 From: sbouaram Date: Wed, 26 Apr 2023 12:20:39 +0200 Subject: [PATCH] GetAccountsByIds --- storage/postgresql.go | 63 ++++++++++++++++++++++++++++++++++++-- storage/postgresql_test.go | 46 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/storage/postgresql.go b/storage/postgresql.go index f5e29b9..bf3989f 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -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 { diff --git a/storage/postgresql_test.go b/storage/postgresql_test.go index 751c3ac..c69676e 100644 --- a/storage/postgresql_test.go +++ b/storage/postgresql_test.go @@ -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) + } +}