config_handler.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package handlers
  2. import (
  3. "net/http"
  4. "strconv"
  5. "cmr-backend/internal/apperr"
  6. "cmr-backend/internal/httpx"
  7. "cmr-backend/internal/service"
  8. )
  9. type ConfigHandler struct {
  10. configService *service.ConfigService
  11. }
  12. func NewConfigHandler(configService *service.ConfigService) *ConfigHandler {
  13. return &ConfigHandler{configService: configService}
  14. }
  15. func (h *ConfigHandler) ListLocalFiles(w http.ResponseWriter, r *http.Request) {
  16. result, err := h.configService.ListLocalEventFiles()
  17. if err != nil {
  18. httpx.WriteError(w, err)
  19. return
  20. }
  21. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  22. }
  23. func (h *ConfigHandler) ImportLocal(w http.ResponseWriter, r *http.Request) {
  24. var req service.ImportLocalEventConfigInput
  25. if err := httpx.DecodeJSON(r, &req); err != nil {
  26. httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
  27. return
  28. }
  29. req.EventPublicID = r.PathValue("eventPublicID")
  30. result, err := h.configService.ImportLocalEventConfig(r.Context(), req)
  31. if err != nil {
  32. httpx.WriteError(w, err)
  33. return
  34. }
  35. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  36. }
  37. func (h *ConfigHandler) BuildPreview(w http.ResponseWriter, r *http.Request) {
  38. var req service.BuildPreviewInput
  39. if err := httpx.DecodeJSON(r, &req); err != nil {
  40. httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
  41. return
  42. }
  43. result, err := h.configService.BuildPreview(r.Context(), req)
  44. if err != nil {
  45. httpx.WriteError(w, err)
  46. return
  47. }
  48. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  49. }
  50. func (h *ConfigHandler) PublishBuild(w http.ResponseWriter, r *http.Request) {
  51. var req service.PublishBuildInput
  52. if err := httpx.DecodeJSON(r, &req); err != nil {
  53. httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
  54. return
  55. }
  56. result, err := h.configService.PublishBuild(r.Context(), req)
  57. if err != nil {
  58. httpx.WriteError(w, err)
  59. return
  60. }
  61. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  62. }
  63. func (h *ConfigHandler) ListSources(w http.ResponseWriter, r *http.Request) {
  64. limit := 20
  65. if raw := r.URL.Query().Get("limit"); raw != "" {
  66. if parsed, err := strconv.Atoi(raw); err == nil {
  67. limit = parsed
  68. }
  69. }
  70. result, err := h.configService.ListEventConfigSources(r.Context(), r.PathValue("eventPublicID"), limit)
  71. if err != nil {
  72. httpx.WriteError(w, err)
  73. return
  74. }
  75. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  76. }
  77. func (h *ConfigHandler) GetSource(w http.ResponseWriter, r *http.Request) {
  78. result, err := h.configService.GetEventConfigSource(r.Context(), r.PathValue("sourceID"))
  79. if err != nil {
  80. httpx.WriteError(w, err)
  81. return
  82. }
  83. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  84. }
  85. func (h *ConfigHandler) GetBuild(w http.ResponseWriter, r *http.Request) {
  86. result, err := h.configService.GetEventConfigBuild(r.Context(), r.PathValue("buildID"))
  87. if err != nil {
  88. httpx.WriteError(w, err)
  89. return
  90. }
  91. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  92. }