ADD: added database connection for players data handling and started login funtion with database

This commit is contained in:
hwinkel
2025-05-30 15:02:23 +02:00
parent 4158b87576
commit 1a2eec44a9
19 changed files with 924 additions and 58 deletions

View File

@@ -17,6 +17,29 @@ type database struct {
db *sql.DB // Pointer to sql.DB
}
func (d *database) SetupTables() error {
InitTables(d.db)
log.Println("Database setup completed successfully.")
return nil
}
func (d *database) GetDB() *sql.DB {
if d.db == nil {
log.Println("Database connection is not established. Call Connect() first.")
return nil
}
return d.db
}
// New creates a new database instance with the provided configuration.
// It initializes the database connection parameters but does not connect to the database.
// This function should be called before calling Connect() to establish the connection.
// It returns a pointer to the database instance.
// Example usage:
// db := database.New("localhost", 5432, "user", "password", "dbname")
// It is intended to be used in the main application or setup phase.
// This function is not intended to be called during normal application operation.
// It is not intended to be called during normal application operation.
func New(host string, port int, user, password, dbname string) *database {
return &database{
host: host,
@@ -28,7 +51,7 @@ func New(host string, port int, user, password, dbname string) *database {
}
}
func (d *database) Connect() error {
func (d *database) Connect() (db *sql.DB) {
fmt.Println("Connecting to the database...")
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
d.host, d.port, d.user, d.password, d.dbname)
@@ -49,5 +72,5 @@ func (d *database) Connect() error {
log.Println("Connected to the database successfully")
d.db = db
return nil
return db
}

View File

@@ -5,28 +5,66 @@ import (
"fmt"
"log"
"volleyball/internal/player"
_ "github.com/lib/pq" // Import the PostgreSQL driver
)
const playerTable = `
CREATE TABLE IF NOT EXISTS players (
const teamTable = `
CREATE TABLE IF NOT EXISTS teams (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT NOT NULL,
birthday DATE NOT NULL
player1_id uuid NOT NULL,
player2_id uuid NOT NULL,
formation_date DATE NOT NULL DEFAULT CURRENT_DATE,
FOREIGN KEY (player1_id) REFERENCES players(id),
FOREIGN KEY (player2_id) REFERENCES players(id)
);
`
func InitTables(db *sql.DB) {
var tableNames = []string{
"players",
"teams",
}
func CheckIfTablesExist(db *sql.DB) (bool, error) {
for _, tableName := range tableNames {
exists, err := tableExists(db, tableName)
if err != nil {
return false, fmt.Errorf("error checking if table %s exists: %w", tableName, err)
}
if !exists {
return false, nil // At least one table does not exist
}
}
return true, nil // All tables exist
}
func tableExists(db *sql.DB, tableName string) (bool, error) {
query := `
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = $1
);
`
var exists bool
err := db.QueryRow(query, tableName).Scan(&exists)
return exists, err
}
func InitTables(d *sql.DB) error {
tables := []string{
playerTable,
player.PlayerTable,
player.RoleTable,
teamTable,
}
for _, table := range tables {
if _, err := db.Exec(table); err != nil {
if _, err := d.Exec(table); err != nil {
log.Fatalf("Error creating table: %v", err)
return fmt.Errorf("error creating table: %w", err)
}
}
fmt.Println("Tables initialized successfully.")
log.Println("Tables initialized successfully.")
return nil
}