diff --git a/pkg/internal/http/api/channel_members_api.go b/pkg/internal/http/api/channel_members_api.go index 5b7766d..147b59c 100644 --- a/pkg/internal/http/api/channel_members_api.go +++ b/pkg/internal/http/api/channel_members_api.go @@ -31,10 +31,18 @@ func listChannelMembers(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } + count, err := services.CountChannelMember(channel.ID) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + if members, err := services.ListChannelMember(channel.ID, take, offset); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { - return c.JSON(members) + return c.JSON(fiber.Map{ + "count": count, + "data": members, + }) } } diff --git a/pkg/internal/services/channel_members.go b/pkg/internal/services/channel_members.go index 6b3cedb..11ebcd8 100644 --- a/pkg/internal/services/channel_members.go +++ b/pkg/internal/services/channel_members.go @@ -15,6 +15,17 @@ import ( "git.solsynth.dev/hypernet/messaging/pkg/internal/models" ) +func CountChannelMember(channelId uint) (int64, error) { + var count int64 + if err := database.C.Where(&models.ChannelMember{ + ChannelID: channelId, + }).Model(&models.ChannelMember{}).Count(&count).Error; err != nil { + return 0, err + } else { + return count, nil + } +} + func ListChannelMember(channelId uint, take int, offset int) ([]models.ChannelMember, error) { var members []models.ChannelMember