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

@@ -3,6 +3,8 @@ package main
import (
"os"
"volleyball/internal/auth"
"volleyball/internal/database"
"volleyball/internal/player"
"volleyball/internal/tournament"
"github.com/gin-contrib/cors"
@@ -10,6 +12,19 @@ import (
)
func main() {
var host = "localhost"
var DBport = 5432
var user = "volleyball"
db := database.New(host, DBport, user, "volleyball", "volleyball")
db.Connect()
// Setup the database and tables
if err := db.SetupTables(); err != nil {
os.Exit(1)
}
r := gin.Default()
// CORS
@@ -21,7 +36,10 @@ func main() {
}))
// Public
r.POST("/api/login", auth.LoginHandler)
r.POST("/api/login", func(c *gin.Context) {
auth.LoginHandler(c, db.GetDB())
})
r.GET("/api/tournaments", tournament.ListTournaments)
// Protected API
@@ -32,6 +50,19 @@ func main() {
api.POST("/tournaments/:id/join", tournament.JoinTournament)
api.PUT("/tournaments/:id", tournament.UpdateTournament)
api.GET("/players", func(c *gin.Context) {
player.GetPlayers(c, db.GetDB())
})
api.POST("/players", func(c *gin.Context) {
player.CreatePlayer(c, db.GetDB())
})
api.PUT("/players/:id", func(c *gin.Context) {
player.UpdatePlayer(c, db.GetDB())
})
api.DELETE("/players/:id", func(c *gin.Context) {
player.DeletePlayer(c, db.GetDB())
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"