PSQL UpdateAccount

This commit is contained in:
sbouaram 2023-04-26 00:11:46 +02:00
parent 57e5a73c2f
commit 478d031275
2 changed files with 104 additions and 2 deletions

View File

@ -137,8 +137,35 @@ func (psql PostgresqlStorage) CreateAccount(account Account) error {
return nil return nil
} }
func (PostgresqlStorage) UpdateAccount(account Account) error { func (psql PostgresqlStorage) UpdateAccount(account Account) error {
return fmt.Errorf("") 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 { func isUUIDv4(str string) bool {

View File

@ -157,3 +157,78 @@ func TestPostgresqlStorage_CreateAccount(t *testing.T) {
t.Errorf("Failed to create account") 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")
}
}