93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package tournament
|
|
|
|
import (
|
|
"net/http"
|
|
"volleyball/internal/common"
|
|
|
|
"slices"
|
|
|
|
"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: []string{"example-user"},
|
|
},
|
|
{
|
|
ID: "2",
|
|
Name: "City Open",
|
|
Location: "Hamburg",
|
|
MaxParticipants: 10,
|
|
Teams: []Team{},
|
|
OrganizerId: []string{"example-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 {
|
|
isOrganizer := slices.Contains(t.OrganizerId, userId)
|
|
if !isOrganizer {
|
|
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"})
|
|
}
|