package handlers import ( "net/http" "galaxy/gamemaster/internal/domain/operation" "galaxy/gamemaster/internal/service/commandexecute" ) // newExecuteCommandsHandler returns the handler for // `POST /api/v1/internal/games/{game_id}/commands`. The request body // is engine-owned (`additionalProperties: true`) and is forwarded to // the service as a `json.RawMessage`. The response on success is the // engine's payload byte-for-byte; failure outcomes use the canonical // error envelope per the OpenAPI contract. func newExecuteCommandsHandler(deps Dependencies) http.HandlerFunc { logger := loggerFor(deps.Logger, "internal_rest.execute_commands") return func(writer http.ResponseWriter, request *http.Request) { if deps.CommandExecute == nil { writeError(writer, http.StatusInternalServerError, errorCodeInternal, "command execute service is not wired") return } gameID, ok := extractGameID(writer, request) if !ok { return } userID, ok := extractUserID(writer, request) if !ok { return } body, err := readRawJSONBody(request.Body) if err != nil { writeError(writer, http.StatusBadRequest, errorCodeInvalidRequest, err.Error()) return } result, err := deps.CommandExecute.Handle(request.Context(), commandexecute.Input{ GameID: gameID, UserID: userID, Payload: body, }) if err != nil { logger.ErrorContext(request.Context(), "command execute service errored", "game_id", gameID, "user_id", userID, "err", err.Error(), ) writeError(writer, http.StatusInternalServerError, errorCodeInternal, "command execute service failed") return } if result.Outcome == operation.OutcomeFailure { writeFailure(writer, result.ErrorCode, result.ErrorMessage) return } writeRawJSON(writer, http.StatusOK, []byte(result.RawResponse)) } }