feat: gamemaster
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"galaxy/gamemaster/internal/domain/operation"
|
||||
"galaxy/gamemaster/internal/service/orderput"
|
||||
)
|
||||
|
||||
// newPutOrdersHandler returns the handler for
|
||||
// `POST /api/v1/internal/games/{game_id}/orders`. The shape and
|
||||
// semantics mirror executeCommands: engine-owned body, raw JSON
|
||||
// pass-through on success, error envelope on failure.
|
||||
func newPutOrdersHandler(deps Dependencies) http.HandlerFunc {
|
||||
logger := loggerFor(deps.Logger, "internal_rest.put_orders")
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
if deps.PutOrders == nil {
|
||||
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "put orders 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.PutOrders.Handle(request.Context(), orderput.Input{
|
||||
GameID: gameID,
|
||||
UserID: userID,
|
||||
Payload: body,
|
||||
})
|
||||
if err != nil {
|
||||
logger.ErrorContext(request.Context(), "put orders service errored",
|
||||
"game_id", gameID,
|
||||
"user_id", userID,
|
||||
"err", err.Error(),
|
||||
)
|
||||
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "put orders service failed")
|
||||
return
|
||||
}
|
||||
|
||||
if result.Outcome == operation.OutcomeFailure {
|
||||
writeFailure(writer, result.ErrorCode, result.ErrorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
writeRawJSON(writer, http.StatusOK, []byte(result.RawResponse))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user