109 lines
3.6 KiB
Go
109 lines
3.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/response"
|
|
)
|
|
|
|
// CaptainPreferenceHandler handles CaptainPreference REST API endpoints.
|
|
// Reference: Chatwoot enterprise/app/controllers/api/v1/captain/preferences_controller.rb
|
|
type CaptainPreferenceHandler struct {
|
|
svc *service.CaptainPreferenceService
|
|
}
|
|
|
|
func NewCaptainPreferenceHandler(svc *service.CaptainPreferenceService) *CaptainPreferenceHandler {
|
|
return &CaptainPreferenceHandler{svc: svc}
|
|
}
|
|
|
|
// Create creates a new captain preference for an account.
|
|
// POST /api/v1/accounts/:id/captain/preferences
|
|
func (h *CaptainPreferenceHandler) 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
|
|
}
|
|
|
|
var req service.CreatePreferenceRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
pref, err := h.svc.Create(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Create captain preference: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create preference")
|
|
return
|
|
}
|
|
|
|
response.OK(c, pref)
|
|
}
|
|
|
|
// Get retrieves the captain preference for an account.
|
|
// GET /api/v1/accounts/:id/captain/preferences
|
|
func (h *CaptainPreferenceHandler) Get(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
|
|
}
|
|
|
|
pref, err := h.svc.Get(c.Request.Context(), uint(accountID))
|
|
if err != nil {
|
|
applogger.L().Errorf("Get captain preference: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "preference not found")
|
|
return
|
|
}
|
|
|
|
response.OK(c, pref)
|
|
}
|
|
|
|
// Update updates the captain preference for an account.
|
|
// PUT /api/v1/accounts/:id/captain/preferences
|
|
func (h *CaptainPreferenceHandler) Update(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
|
|
}
|
|
|
|
var req service.UpdatePreferenceRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
pref, err := h.svc.Update(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("Update captain preference: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update preference")
|
|
return
|
|
}
|
|
|
|
response.OK(c, pref)
|
|
}
|
|
|
|
// Delete removes the captain preference for an account.
|
|
// DELETE /api/v1/accounts/:id/captain/preferences
|
|
func (h *CaptainPreferenceHandler) Delete(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
|
|
}
|
|
|
|
if err := h.svc.Delete(c.Request.Context(), uint(accountID)); err != nil {
|
|
applogger.L().Errorf("Delete captain preference: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete preference")
|
|
return
|
|
}
|
|
|
|
response.OK(c, gin.H{"message": "preference deleted"})
|
|
}
|