| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package handlers
- import (
- "net/http"
- "strconv"
- "cmr-backend/internal/apperr"
- "cmr-backend/internal/httpx"
- "cmr-backend/internal/service"
- )
- type ConfigHandler struct {
- configService *service.ConfigService
- }
- func NewConfigHandler(configService *service.ConfigService) *ConfigHandler {
- return &ConfigHandler{configService: configService}
- }
- func (h *ConfigHandler) ListLocalFiles(w http.ResponseWriter, r *http.Request) {
- result, err := h.configService.ListLocalEventFiles()
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) ImportLocal(w http.ResponseWriter, r *http.Request) {
- var req service.ImportLocalEventConfigInput
- if err := httpx.DecodeJSON(r, &req); err != nil {
- httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
- return
- }
- req.EventPublicID = r.PathValue("eventPublicID")
- result, err := h.configService.ImportLocalEventConfig(r.Context(), req)
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) BuildPreview(w http.ResponseWriter, r *http.Request) {
- var req service.BuildPreviewInput
- if err := httpx.DecodeJSON(r, &req); err != nil {
- httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
- return
- }
- result, err := h.configService.BuildPreview(r.Context(), req)
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) PublishBuild(w http.ResponseWriter, r *http.Request) {
- var req service.PublishBuildInput
- if err := httpx.DecodeJSON(r, &req); err != nil {
- httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
- return
- }
- result, err := h.configService.PublishBuild(r.Context(), req)
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) ListSources(w http.ResponseWriter, r *http.Request) {
- limit := 20
- if raw := r.URL.Query().Get("limit"); raw != "" {
- if parsed, err := strconv.Atoi(raw); err == nil {
- limit = parsed
- }
- }
- result, err := h.configService.ListEventConfigSources(r.Context(), r.PathValue("eventPublicID"), limit)
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) GetSource(w http.ResponseWriter, r *http.Request) {
- result, err := h.configService.GetEventConfigSource(r.Context(), r.PathValue("sourceID"))
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
- func (h *ConfigHandler) GetBuild(w http.ResponseWriter, r *http.Request) {
- result, err := h.configService.GetEventConfigBuild(r.Context(), r.PathValue("buildID"))
- if err != nil {
- httpx.WriteError(w, err)
- return
- }
- httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
- }
|