diff --git a/storage/postgresql.go b/storage/postgresql.go index d31b628..e59884a 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -137,8 +137,35 @@ func (psql PostgresqlStorage) CreateAccount(account Account) error { return nil } -func (PostgresqlStorage) UpdateAccount(account Account) error { - return fmt.Errorf("") +func (psql PostgresqlStorage) UpdateAccount(account Account) error { + updateAccountStmt := "update accounts set namespace=$1, data=$2, metadata=$3 where id= $4" + dataAccountJson, err := json.Marshal(account.Data) + if err != nil { + return err + } + metadataAccountJson, err := json.Marshal(account.Metadata) + if err != nil { + return err + } + _, err = psql.DbConnection.Exec(updateAccountStmt, account.Namespace, dataAccountJson, metadataAccountJson, account.ID) + if err != nil { + return err + } + updateAccountAuthStmt := "update account_auth set username = $1, password = $2, email = $3, email_validation = $4 ,phone_number = $5,phone_number_validation = $6 where account_id = $7" + emailValidationJson, err := json.Marshal(account.Authentication.Local.EmailValidation) + if err != nil { + return err + } + phoneValidationJson, err := json.Marshal(account.Authentication.Local.PhoneNumberValidation) + if err != nil { + return err + } + _, err = psql.DbConnection.Exec(updateAccountAuthStmt, account.Authentication.Local.Username, account.Authentication.Local.Password, account.Authentication.Local.Email, + emailValidationJson, account.Authentication.Local.PhoneNumber, phoneValidationJson, account.ID) + if err != nil { + return err + } + return nil } func isUUIDv4(str string) bool { diff --git a/storage/postgresql_test.go b/storage/postgresql_test.go index 585710f..cf35713 100644 --- a/storage/postgresql_test.go +++ b/storage/postgresql_test.go @@ -157,3 +157,78 @@ func TestPostgresqlStorage_CreateAccount(t *testing.T) { t.Errorf("Failed to create account") } } + +func TestPostgresqlStorage_UpdateAccount(t *testing.T) { + db, err := NewPostgresqlStorage(cfg) + Id := generateUUIDv4() + if err != nil { + t.Errorf("failed to create new psql connection") + } + emailValidation := Validation{ + Validated: true, + ValidationCode: "code", + } + localAuth := LocalAuth{ + Username: "salim", + Password: "testpassword", + Email: "test@test.com", + EmailValidation: emailValidation, + PhoneNumber: "1234567890", + PhoneNumberValidation: emailValidation, + } + accountData := map[string]any{ + "key1": "value1", + "key2": "value2", + } + accountMetadata := map[string]any{ + "key1": "value1", + "key2": "value2", + } + account := Account{ + ID: Id, + Namespace: "test_namespace", + Authentication: AccountAuth{ + Local: localAuth, + }, + Data: accountData, + Metadata: accountMetadata, + } + err = db.CreateAccount(account) + if err != nil { + fmt.Println(err) + t.Errorf("Failed to create account") + } + account2 := Account{ + ID: Id, + Namespace: "salim", + Authentication: AccountAuth{ + Local: LocalAuth{ + Username: "salim", + Password: "salim", + Email: "salim@test.com", + EmailValidation: Validation{ + Validated: false, + 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.UpdateAccount(account2) + if err != nil { + fmt.Println(err) + t.Errorf("failed") + } +}