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

135 lines
4.6 KiB
Go

package v1
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/pagination"
"github.com/gochat/gochat/pkg/response"
)
// CaptainScenarioHandler handles CaptainScenario REST API endpoints.
// Reference: Chatwoot enterprise/app/controllers/api/v1/captain/scenarios_controller.rb
type CaptainScenarioHandler struct {
svc *service.CaptainScenarioService
}
// NewCaptainScenarioHandler creates a new CaptainScenarioHandler.
func NewCaptainScenarioHandler(svc *service.CaptainScenarioService) *CaptainScenarioHandler {
return &CaptainScenarioHandler{svc: svc}
}
// Create creates a new scenario under an assistant.
// POST /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios
func (h *CaptainScenarioHandler) Create(c *gin.Context) {
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
assistantID, err := strconv.ParseUint(c.Param("assistant_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
return
}
var req service.CreateScenarioRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
scenario, err := h.svc.Create(c.Request.Context(), uint(accountID), uint(assistantID), &req)
if err != nil {
applogger.L().Errorf("Create captain scenario: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create scenario")
return
}
response.Created(c, scenario)
}
// Get retrieves a scenario by ID.
// GET /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
func (h *CaptainScenarioHandler) Get(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
scenario, err := h.svc.GetByID(c.Request.Context(), uint(id))
if err != nil {
applogger.L().Errorf("Get captain scenario: %v", err)
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "scenario not found")
return
}
response.OK(c, scenario)
}
// Update updates an existing scenario.
// PUT /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
func (h *CaptainScenarioHandler) Update(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
var req service.UpdateScenarioRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
scenario, err := h.svc.Update(c.Request.Context(), uint(id), &req)
if err != nil {
applogger.L().Errorf("Update captain scenario: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update scenario")
return
}
response.OK(c, scenario)
}
// Delete deletes a scenario.
// DELETE /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
func (h *CaptainScenarioHandler) Delete(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
if err := h.svc.Delete(c.Request.Context(), uint(id)); err != nil {
applogger.L().Errorf("Delete captain scenario: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete scenario")
return
}
response.NoContent(c)
}
// List retrieves scenarios for an assistant.
// GET /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios
func (h *CaptainScenarioHandler) List(c *gin.Context) {
assistantID, err := strconv.ParseUint(c.Param("assistant_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
return
}
p := pagination.Parse(c)
scenarios, count, err := h.svc.ListByAssistant(c.Request.Context(), uint(assistantID), p.Offset, p.PerPage)
if err != nil {
applogger.L().Errorf("List captain scenarios: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list scenarios")
return
}
response.OKWithMeta(c, scenarios, p.Page, p.PerPage, count)
}