ADD: added initial page with login
This commit is contained in:
89
backend/internal/tournament/handler.go
Normal file
89
backend/internal/tournament/handler.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package tournament
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"volleyball/internal/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var tournaments = []Tournament{
|
||||
{
|
||||
ID: "1",
|
||||
Name: "Beach Cup",
|
||||
Location: "Berlin",
|
||||
MaxParticipants: 8,
|
||||
Teams: []Team{
|
||||
{ID: "t1", Name: "Smasher"},
|
||||
{ID: "t2", Name: "Blockbuster"},
|
||||
},
|
||||
OrganizerId: "system-user-id",
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Name: "City Open",
|
||||
Location: "Hamburg",
|
||||
MaxParticipants: 10,
|
||||
Teams: []Team{},
|
||||
OrganizerId: "other-user",
|
||||
},
|
||||
}
|
||||
|
||||
func ListTournaments(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, tournaments)
|
||||
}
|
||||
|
||||
func GetTournament(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
for _, t := range tournaments {
|
||||
if t.ID == id {
|
||||
c.JSON(http.StatusOK, t)
|
||||
return
|
||||
}
|
||||
}
|
||||
common.RespondError(c, http.StatusNotFound, "Tournament not found")
|
||||
}
|
||||
|
||||
func JoinTournament(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var team Team
|
||||
if err := c.ShouldBindJSON(&team); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid team data"})
|
||||
return
|
||||
}
|
||||
|
||||
for i, t := range tournaments {
|
||||
if t.ID == id {
|
||||
tournaments[i].Teams = append(tournaments[i].Teams, team)
|
||||
c.JSON(http.StatusOK, tournaments[i])
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Tournament not found"})
|
||||
}
|
||||
|
||||
func UpdateTournament(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
userId := c.GetString("userId")
|
||||
|
||||
var updated Tournament
|
||||
if err := c.ShouldBindJSON(&updated); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid data"})
|
||||
return
|
||||
}
|
||||
|
||||
for i, t := range tournaments {
|
||||
if t.ID == id {
|
||||
if t.OrganizerId != userId {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "No permission"})
|
||||
return
|
||||
}
|
||||
tournaments[i].Name = updated.Name
|
||||
tournaments[i].Location = updated.Location
|
||||
tournaments[i].MaxParticipants = updated.MaxParticipants
|
||||
c.JSON(http.StatusOK, tournaments[i])
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Tournament not found"})
|
||||
}
|
||||
Reference in New Issue
Block a user