mobility-accounts/storage/postgresql.go

148 lines
4.7 KiB
Go

package storage
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/spf13/viper"
"regexp"
"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
}
func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
var (
data, metadata, emailValidation, phoneValidation []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 username, password, email, email_validation, phone_number, 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,
&phoneValidation)
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
}
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return account, err
}
}
return account, nil
}
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 (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 {
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)
}