2023-04-25 12:14:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-04-25 14:23:19 +00:00
|
|
|
"encoding/json"
|
2023-04-25 12:14:42 +00:00
|
|
|
"fmt"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"github.com/spf13/viper"
|
2023-04-25 14:23:19 +00:00
|
|
|
"regexp"
|
2023-04-25 12:14:42 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PostgresqlStorage struct {
|
|
|
|
DbConnection *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
|
|
|
|
var (
|
|
|
|
host = cfg.GetString("storage.db.psql.host")
|
|
|
|
port = cfg.GetString("storage.db.psql.port")
|
|
|
|
user = cfg.GetString("storage.db.psql.user")
|
|
|
|
password = cfg.GetString("storage.db.psql.password")
|
|
|
|
dbname = cfg.GetString("storage.db.psql.dbname")
|
|
|
|
)
|
|
|
|
portInt, _ := strconv.Atoi(port)
|
|
|
|
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, portInt,
|
|
|
|
user, password, dbname)
|
|
|
|
db, err := sql.Open("postgres", psqlconn)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error", err)
|
|
|
|
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed")
|
|
|
|
}
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed")
|
|
|
|
}
|
|
|
|
return PostgresqlStorage{
|
|
|
|
DbConnection: db,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:23:19 +00:00
|
|
|
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
|
2023-04-25 12:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (PostgresqlStorage) LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error) {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error) {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error) {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (PostgresqlStorage) CreateAccount(account Account) error {
|
|
|
|
return fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (PostgresqlStorage) UpdateAccount(account Account) error {
|
|
|
|
return fmt.Errorf("")
|
|
|
|
}
|
2023-04-25 14:23:19 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|