2023-12-08 06:35:04 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-10-31 19:32:54 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PostgresqlStorage struct {
|
|
|
|
DbConnection *sql.DB
|
|
|
|
Schema string
|
|
|
|
Tables map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
|
|
|
|
var (
|
2024-10-31 19:32:54 +00:00
|
|
|
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")
|
|
|
|
sslMode = cfg.GetString("storage.db.psql.sslMode")
|
|
|
|
pgSchema = cfg.GetString("storage.db.psql.schema")
|
|
|
|
pgtablesUsersFirebase = cfg.GetString("storage.db.psql.tables.users_firebase")
|
2023-12-08 06:35:04 +00:00
|
|
|
)
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
portInt, _ := strconv.Atoi(port)
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
psqlConn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslMode=%s",
|
|
|
|
host, portInt, user, password, dbname, sslMode)
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", psqlConn)
|
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("opening connection to postgresql failed")
|
|
|
|
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed")
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
if err = db.Ping(); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
log.Error().Err(err).Msg("ping to postgresql failed")
|
|
|
|
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed")
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
return PostgresqlStorage{
|
|
|
|
DbConnection: db,
|
2024-10-31 19:32:54 +00:00
|
|
|
Schema: pgSchema,
|
2023-12-08 06:35:04 +00:00
|
|
|
Tables: map[string]string{
|
2024-10-31 19:32:54 +00:00
|
|
|
"users_firebase": fmt.Sprintf("%s.%s", pgSchema, pgtablesUsersFirebase),
|
2023-12-08 06:35:04 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
func (s PostgresqlStorage) CreateFirebaseToken(userId string, fcmToken string, devicePlatform string) (err error) {
|
|
|
|
|
|
|
|
if _, err = uuid.Parse(userId); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
log.Error().Err(err).Msg("Postgresql Storage CreateFirebaseToken invalid User ID")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
if _, err = s.DbConnection.Exec(fmt.Sprintf("INSERT INTO %s "+
|
|
|
|
"(user_id , fcm_token , device_platform) VALUES($1,$2,$3)", s.Tables["users_firebase"]),
|
|
|
|
userId,
|
|
|
|
fcmToken,
|
|
|
|
devicePlatform); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
if strings.Contains(err.Error(), "duplicate key") {
|
2024-10-31 19:32:54 +00:00
|
|
|
_ = s.UpdateFirebaseToken(userId, devicePlatform, fcmToken)
|
2023-12-08 06:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
func (s PostgresqlStorage) GetFirebaseToken(userId string) (fcm string, devicePlatform string, err error) {
|
|
|
|
if err = s.DbConnection.QueryRow(fmt.Sprintf("SELECT fcm_token , "+
|
|
|
|
"device_platform FROM %s WHERE user_id = $1", s.Tables["users_firebase"]), userId).
|
2023-12-08 06:35:04 +00:00
|
|
|
Scan(
|
|
|
|
&fcm,
|
2024-10-31 19:32:54 +00:00
|
|
|
&devicePlatform,
|
|
|
|
); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
return fcm, devicePlatform, nil
|
2023-12-08 06:35:04 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
func (s PostgresqlStorage) UpdateFirebaseToken(userId string, fcmToken string, devicePlatform string) error {
|
|
|
|
query := fmt.Sprintf("UPDATE %s SET fcm_token ="+
|
|
|
|
" $1 device_platform = $2 WHERE user_id = $3", s.Tables["users_firebase"])
|
|
|
|
|
|
|
|
if _, err := s.DbConnection.Exec(query, fcmToken, devicePlatform, userId); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
return nil
|
|
|
|
}
|