package database

import (
	fmt "fmt"
	log "log"

	postgres "gorm.io/driver/postgres"
	gorm "gorm.io/gorm"

	models "lesson3/models"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "password"
	dbname   = "lesson3"
)

var dsn string = fmt.Sprintf("host=%s port=%d user=%s "+
	"password=%s dbname=%s sslmode=disable TimeZone=Asia/Shanghai",
	host, port, user, password, dbname)

var DB *gorm.DB

func DBconn() {
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
	}
	DB = db

	db.AutoMigrate(
		&models.User{},
	) // we are going to create a models.go file for the User Model.
}
