diff --git a/storage/postgresql.go b/storage/postgresql.go index 98a3ad9..d31b628 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -100,8 +100,41 @@ func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error return nil, fmt.Errorf("") } -func (PostgresqlStorage) CreateAccount(account Account) error { - return fmt.Errorf("") +func (psql PostgresqlStorage) CreateAccount(account Account) error { + insertAccountStmt, err := psql.DbConnection.Prepare("INSERT INTO accounts (id, namespace, data, metadata) VALUES ($1, $2, $3, $4)") + if err != nil { + return err + } + dataAccountJson, err := json.Marshal(account.Data) + if err != nil { + return err + } + metadataAccountJson, err := json.Marshal(account.Metadata) + if err != nil { + return err + } + _, err = insertAccountStmt.Exec(account.ID, account.Namespace, dataAccountJson, metadataAccountJson) + if err != nil { + return err + } + insertAccountAuthStmt, err := psql.DbConnection.Prepare("INSERT INTO account_auth (account_id, username, password, email, email_validation,phone_number,phone_number_validation) values($1, $2, $3, $4, $5, $6, $7)") + if err != nil { + return err + } + 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 = insertAccountAuthStmt.Exec(account.ID, account.Authentication.Local.Username, account.Authentication.Local.Password, + account.Authentication.Local.Email, emailValidationJson, account.Authentication.Local.PhoneNumber, phoneValidationJson) + if err != nil { + return err + } + return nil } func (PostgresqlStorage) UpdateAccount(account Account) error { diff --git a/storage/postgresql_test.go b/storage/postgresql_test.go index a98ab0a..585710f 100644 --- a/storage/postgresql_test.go +++ b/storage/postgresql_test.go @@ -10,14 +10,18 @@ import ( "testing" ) -func TestNewPostgresqlStorage(t *testing.T) { - cfg := viper.New() +var cfg *viper.Viper + +func init() { + cfg = viper.New() cfg.Set("storage.db.psql.host", "localhost") cfg.Set("storage.db.psql.port", "5432") cfg.Set("storage.db.psql.user", "postgres") cfg.Set("storage.db.psql.password", "postgres") cfg.Set("storage.db.psql.dbname", "mobilityaccounts") +} +func TestNewPostgresqlStorage(t *testing.T) { storage, err := NewPostgresqlStorage(cfg) if err != nil { t.Errorf("error creating new PostgreSQL storage: %v", err) @@ -35,14 +39,9 @@ func generateUUIDv4() string { uuid[8] = (uuid[8] & 0xbf) | 0x80 return hex.EncodeToString(uuid[:4]) + "-" + hex.EncodeToString(uuid[4:6]) + "-" + hex.EncodeToString(uuid[6:8]) + "-" + hex.EncodeToString(uuid[8:10]) + "-" + hex.EncodeToString(uuid[10:]) } + func TestGetAccount(t *testing.T) { // Open a database connection - cfg := viper.New() - cfg.Set("storage.db.psql.host", "localhost") - cfg.Set("storage.db.psql.port", "5432") - cfg.Set("storage.db.psql.user", "postgres") - cfg.Set("storage.db.psql.password", "postgres") - cfg.Set("storage.db.psql.dbname", "mobilityaccounts") db, err := NewPostgresqlStorage(cfg) Id := generateUUIDv4() if err != nil { @@ -116,3 +115,45 @@ func TestGetAccount(t *testing.T) { t.Errorf("The received account is not the same as expected") } } + +func TestPostgresqlStorage_CreateAccount(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") + } +}