package handler import ( "errors" "net/http" "galaxy/model/order" "galaxy/model/rest" "galaxy/game/internal/repo" "github.com/gin-gonic/gin" ) func PutOrderHandler(c *gin.Context, executor CommandExecutor) { var cmd rest.Command if errorResponse(c, c.ShouldBindJSON(&cmd)) { return } commands := make([]order.DecodableCommand, len(cmd.Commands)) for i := range cmd.Commands { command, err := repo.ParseOrder(cmd.Commands[i], validateCommand) if errorResponse(c, err) { return } commands[i] = command } if len(commands) == 0 { errorResponse(c, errors.New("no commands given")) return } result, err := executor.ValidateOrder(cmd.Actor, commands...) if errorResponse(c, err) { return } c.JSON(http.StatusAccepted, result) } type orderParam struct { Player string `form:"player" binding:"required,notblank"` Turn int `form:"turn" binding:"gte=0"` } func GetOrderHandler(c *gin.Context, executor CommandExecutor) { p := &orderParam{} err := c.ShouldBindQuery(p) if errorResponse(c, err) { return } order, ok, err := executor.FetchOrder(p.Player, uint(p.Turn)) if errorResponse(c, err) { return } if !ok { // there was no order previously sent by player c.Status(http.StatusNoContent) } var cmd rest.Command if errorResponse(c, c.ShouldBindJSON(&cmd)) { return } c.JSON(http.StatusOK, order) }