Files
gochat/internal/handler/api/v1/installation_config_handler.go
T
2026-06-04 15:44:48 +08:00

116 lines
3.4 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/pagination"
"github.com/gochat/gochat/pkg/response"
)
// InstallationConfigHandler handles InstallationConfig CRUD endpoints.
// Reference: Chatwoot app/controllers/api/v1/platform/installation_configs_controller.rb
// Note: These handlers are registered under /platform/api/v1/ namespace (super admin only)
type InstallationConfigHandler struct {
svc *service.InstallationConfigService
}
// NewInstallationConfigHandler creates a new InstallationConfig handler with service injection.
func NewInstallationConfigHandler(svc *service.InstallationConfigService) *InstallationConfigHandler {
return &InstallationConfigHandler{svc: svc}
}
// --- Platform-level InstallationConfig endpoints (super-admin only) ---
// Reference: Chatwoot routes — namespace :installation_configs under :platform_app
// List retrieves all installation configs, paginated.
// GET /platform/api/v1/installation_configs
func (h *InstallationConfigHandler) List(c *gin.Context) {
page := pagination.Parse(c)
configs, total, svcErr := h.svc.List(c.Request.Context(), page.Offset, page.PerPage)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OKWithMeta(c, configs, page.Page, page.PerPage, total)
}
// Get retrieves a single installation config by ID.
// GET /platform/api/v1/installation_configs/:id
func (h *InstallationConfigHandler) Get(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
cfg, svcErr := h.svc.Get(c.Request.Context(), id)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, cfg)
}
// Create creates a new installation config.
// POST /platform/api/v1/installation_configs
func (h *InstallationConfigHandler) Create(c *gin.Context) {
var req service.CreateInstallationConfigRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
cfg, svcErr := h.svc.Create(c.Request.Context(), &req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.Created(c, cfg)
}
// Update modifies an existing installation config.
// PUT /platform/api/v1/installation_configs/:id
func (h *InstallationConfigHandler) Update(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
var req service.UpdateInstallationConfigRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
cfg, svcErr := h.svc.Update(c.Request.Context(), id, &req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, cfg)
}
// Delete removes an installation config by ID.
// DELETE /platform/api/v1/installation_configs/:id
func (h *InstallationConfigHandler) Delete(c *gin.Context) {
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
if svcErr := h.svc.Delete(c.Request.Context(), id); svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.NoContent(c)
}