pkg/repository/postgres.go   A
last analyzed

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
cc 3
eloc 23
dl 0
loc 36
ccs 0
cts 7
cp 0
crap 12
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A repository.NewPostgresDB 0 13 3
1
package repository
2
3
import (
4
	"fmt"
5
6
	"github.com/jmoiron/sqlx"
7
	_ "github.com/lib/pq"
8
)
9
10
const (
11
	roomsTable    = "rooms"
12
	bookingsTable = "bookings"
13
)
14
15
type Config struct {
16
	Host     string
17
	Port     string
18
	Username string
19
	Password string
20
	DBName   string
21
	SSLMode  string
22
}
23
24
func NewPostgresDB(cfg Config) (*sqlx.DB, error) {
25
	db, err := sqlx.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s",
26
		cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))
27
	if err != nil {
28
		return nil, err
29
	}
30
31
	err = db.Ping()
32
	if err != nil {
33
		return nil, err
34
	}
35
36
	return db, nil
37
}
38