init server

This commit is contained in:
2021-11-03 14:10:58 +01:00
commit 4f9782785d
1603 changed files with 519678 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package postgresql
import (
"database/sql"
"fmt"
"nos-comptes/internal/storage/dao"
"nos-comptes/internal/utils"
"github.com/lib/pq"
)
const (
pgCodeUniqueViolation = "23505"
pgCodeForeingKeyViolation = "23503"
)
func HandlePgError(e *pq.Error) error {
if e.Code == pgCodeUniqueViolation {
return dao.NewDAOError(dao.ErrTypeDuplicate, e)
}
if e.Code == pgCodeForeingKeyViolation {
return dao.NewDAOError(dao.ErrTypeForeignKeyViolation, e)
}
return e
}
type DatabasePostgreSQL struct {
Session *sql.DB
}
func NewDatabasePostgreSQL(connectionURI string) *DatabasePostgreSQL {
db, err := sql.Open("postgres", connectionURI)
if err != nil {
utils.GetLogger().WithError(err).Fatal("Unable to get a connection to the postgres db")
}
err = db.Ping()
if err != nil {
fmt.Println(err)
utils.GetLogger().WithError(err).Fatal("Unable to ping the postgres db")
}
return &DatabasePostgreSQL{Session: db}
}