42 lines
660 B
Go
42 lines
660 B
Go
package repos
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type AllRepos interface {
|
|
RoomsRepo
|
|
ActionsRepo
|
|
PlayersRepo
|
|
SessionsRepo
|
|
}
|
|
|
|
type RepoProvider struct {
|
|
DB *sqlx.DB
|
|
}
|
|
|
|
func NewRepoProvider(pathToDB string) *RepoProvider {
|
|
db, err := sqlx.Connect("sqlite3", pathToDB)
|
|
if err != nil {
|
|
slog.Error("Unable to connect to database", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
slog.Info("Successfully connected to database")
|
|
return &RepoProvider{
|
|
DB: db,
|
|
}
|
|
}
|
|
|
|
func getDB(ctx context.Context, db *sqlx.DB) sqlx.ExtContext {
|
|
if tx, ok := ctx.Value("tx").(*sqlx.Tx);
|
|
ok {
|
|
return tx
|
|
}
|
|
return db
|
|
}
|