Spaces:
Running
Running
File size: 4,579 Bytes
83d6851 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | package community_handler
import (
"net/http"
community_service "agentdeck-whatsapp-service/pkg/community/service"
instance_model "agentdeck-whatsapp-service/pkg/instance/model"
"github.com/gin-gonic/gin"
)
type CommunityHandler interface {
CreateCommunity(ctx *gin.Context)
CommunityAdd(ctx *gin.Context)
CommunityRemove(ctx *gin.Context)
}
type communityHandler struct {
communityService community_service.CommunityService
}
// Create community
// @Summary Create community
// @Description Create community
// @Tags Community
// @Accept json
// @Produce json
// @Param message body community_service.CreateCommunityStruct true "Community data"
// @Success 200 {object} gin.H "success"
// @Failure 400 {object} gin.H "Error on validation"
// @Failure 500 {object} gin.H "Internal server error"
// @Router /community/create [post]
func (c *communityHandler) CreateCommunity(ctx *gin.Context) {
getInstance := ctx.MustGet("instance")
instance, ok := getInstance.(*instance_model.Instance)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
return
}
var data *community_service.CreateCommunityStruct
err := ctx.ShouldBindBodyWithJSON(&data)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if data.CommunityName == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community name is required"})
return
}
community, err := c.communityService.CreateCommunity(data, instance)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": community})
}
// Add participant to community
// @Summary Add participant to community
// @Description Add participant to community
// @Tags Community
// @Accept json
// @Produce json
// @Param message body community_service.AddParticipantStruct true "Participant data"
// @Success 200 {object} gin.H "success"
// @Failure 400 {object} gin.H "Error on validation"
// @Failure 500 {object} gin.H "Internal server error"
// @Router /community/add [post]
func (c *communityHandler) CommunityAdd(ctx *gin.Context) {
getInstance := ctx.MustGet("instance")
instance, ok := getInstance.(*instance_model.Instance)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
return
}
var data *community_service.AddParticipantStruct
err := ctx.ShouldBindBodyWithJSON(&data)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if data.CommunityJID == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community jid is required"})
return
}
if len(data.GroupJID) == 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "group jid is required"})
return
}
resp, err := c.communityService.CommunityAdd(data, instance)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": resp})
}
// Remove participant from community
// @Summary Remove participant from community
// @Description Remove participant from community
// @Tags Community
// @Accept json
// @Produce json
// @Param message body community_service.AddParticipantStruct true "Participant data"
// @Success 200 {object} gin.H "success"
// @Failure 400 {object} gin.H "Error on validation"
// @Failure 500 {object} gin.H "Internal server error"
// @Router /community/remove [post]
func (c *communityHandler) CommunityRemove(ctx *gin.Context) {
getInstance := ctx.MustGet("instance")
instance, ok := getInstance.(*instance_model.Instance)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
return
}
var data *community_service.AddParticipantStruct
err := ctx.ShouldBindBodyWithJSON(&data)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if data.CommunityJID == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community jid is required"})
return
}
if len(data.GroupJID) == 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "group jid is required"})
return
}
resp, err := c.communityService.CommunityRemove(data, instance)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": resp})
}
func NewCommunityHandler(
communityService community_service.CommunityService,
) CommunityHandler {
return &communityHandler{
communityService: communityService,
}
}
|