package handler import ( "errors" "net/http" "strconv" "github.com/gin-gonic/gin" "dating-app/internal/dto" "dating-app/internal/service" "dating-app/internal/utils" ) type ChatHandler struct { chatService *service.ChatService } func NewChatHandler(chatService *service.ChatService) *ChatHandler { return &ChatHandler{chatService: chatService} } // ListMatches GET /matches func (h *ChatHandler) ListMatches(c *gin.Context) { user, ok := currentUser(c) if !ok { return } matches, err := h.chatService.ListMatches(user.ID) if err != nil { utils.Fail(c, http.StatusInternalServerError, "خطا در دریافت مچ‌ها", nil) return } utils.Success(c, http.StatusOK, "", matches) } // ListMessages GET /matches/:id/messages func (h *ChatHandler) ListMessages(c *gin.Context) { user, ok := currentUser(c) if !ok { return } matchID, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { utils.Fail(c, http.StatusBadRequest, "شناسه نامعتبر است", nil) return } messages, err := h.chatService.ListMessages(user.ID, uint(matchID)) if err != nil { h.handleError(c, err) return } utils.Success(c, http.StatusOK, "", messages) } // SendMessage POST /matches/:id/messages body: {text} func (h *ChatHandler) SendMessage(c *gin.Context) { user, ok := currentUser(c) if !ok { return } matchID, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil { utils.Fail(c, http.StatusBadRequest, "شناسه نامعتبر است", nil) return } var req dto.SendMessageRequest if err := c.ShouldBindJSON(&req); err != nil { utils.Fail(c, http.StatusBadRequest, "متن پیام نمی‌تونه خالی باشه", err.Error()) return } msg, err := h.chatService.SendMessage(user.ID, uint(matchID), req.Text) if err != nil { h.handleError(c, err) return } utils.Success(c, http.StatusCreated, "", msg) } func (h *ChatHandler) handleError(c *gin.Context, err error) { switch { case errors.Is(err, service.ErrNotMatchMember): utils.Fail(c, http.StatusForbidden, err.Error(), nil) default: utils.Fail(c, http.StatusNotFound, "گفتگو یافت نشد", nil) } }