55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"galaxy/gamemaster/internal/domain/runtime"
|
|
)
|
|
|
|
// newListRuntimesHandler returns the handler for
|
|
// `GET /api/v1/internal/runtimes`. The optional `status` query
|
|
// parameter narrows the result; an unknown value short-circuits with
|
|
// `400 invalid_request`. Records are returned ordered by
|
|
// `created_at DESC` (the underlying store guarantees the ordering).
|
|
func newListRuntimesHandler(deps Dependencies) http.HandlerFunc {
|
|
logger := loggerFor(deps.Logger, "internal_rest.list_runtimes")
|
|
return func(writer http.ResponseWriter, request *http.Request) {
|
|
if deps.RuntimeRecords == nil {
|
|
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "runtime records store is not wired")
|
|
return
|
|
}
|
|
|
|
ctx := request.Context()
|
|
|
|
raw := strings.TrimSpace(request.URL.Query().Get("status"))
|
|
if raw == "" {
|
|
records, err := deps.RuntimeRecords.List(ctx)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "list runtime records failed", "err", err.Error())
|
|
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "failed to list runtime records")
|
|
return
|
|
}
|
|
writeJSON(writer, http.StatusOK, encodeRuntimeList(records))
|
|
return
|
|
}
|
|
|
|
status := runtime.Status(raw)
|
|
if !status.IsKnown() {
|
|
writeError(writer, http.StatusBadRequest, errorCodeInvalidRequest, "status query parameter is unsupported")
|
|
return
|
|
}
|
|
|
|
records, err := deps.RuntimeRecords.ListByStatus(ctx, status)
|
|
if err != nil {
|
|
logger.ErrorContext(ctx, "list runtime records by status failed",
|
|
"status", string(status),
|
|
"err", err.Error(),
|
|
)
|
|
writeError(writer, http.StatusInternalServerError, errorCodeInternal, "failed to list runtime records")
|
|
return
|
|
}
|
|
writeJSON(writer, http.StatusOK, encodeRuntimeList(records))
|
|
}
|
|
}
|