diff --git a/storage/postgresql.go b/storage/postgresql.go index d926830..72e5b46 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -2,9 +2,11 @@ package storage import ( "database/sql" + "encoding/json" "fmt" _ "github.com/lib/pq" "github.com/spf13/viper" + "regexp" "strconv" ) @@ -38,8 +40,49 @@ func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) { }, nil } -func (PostgresqlStorage) GetAccount(id string) (*Account, error) { - return nil, fmt.Errorf("") +func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) { + var ( + data, metadata, emailValidation []byte + ) + account := &Account{} + if isUUIDv4(id) { + stmtAccounts, err := psql.DbConnection.Prepare("SELECT id, namespace, data, metadata FROM accounts WHERE id = $1") + if err != nil { + return account, fmt.Errorf("psql connection failed") + } + defer stmtAccounts.Close() + err = stmtAccounts.QueryRow(id).Scan(&account.ID, &account.Namespace, &data, &metadata) + if err != nil { + return account, fmt.Errorf("psql select account query failed") + } + err = json.Unmarshal(data, &account.Data) + if err != nil { + return account, err + } + err = json.Unmarshal(metadata, &account.Metadata) + if err != nil { + return account, err + } + stmtAccount_auth, err := psql.DbConnection.Prepare("SELECT local_username,local_password, local_email, local_email_validation, local_phone_number, local_phone_number_validation FROM account_auth WHERE account_id= $1") + if err != nil { + return account, fmt.Errorf("psql connection failed") + } + defer stmtAccount_auth.Close() + err = stmtAccount_auth.QueryRow(id).Scan(&account.Authentication.Local.Username, + &account.Authentication.Local.Password, + &account.Authentication.Local.Email, + &emailValidation, + &account.Authentication.Local.PhoneNumber, + &account.Authentication.Local.PhoneNumberValidation) + if err != nil { + return account, fmt.Errorf("psql select account auth query failed") + } + err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation) + if err != nil { + return account, err + } + } + return account, nil } func (PostgresqlStorage) LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error) { @@ -61,3 +104,8 @@ func (PostgresqlStorage) CreateAccount(account Account) error { func (PostgresqlStorage) UpdateAccount(account Account) error { return fmt.Errorf("") } + +func isUUIDv4(str string) bool { + pattern := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89aAbB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`) + return pattern.MatchString(str) +}