🐛 Fix pagination fetch channel members does not include total count

This commit is contained in:
LittleSheep 2024-12-01 00:17:43 +08:00
parent 40ef26f75d
commit e82f100b67
2 changed files with 20 additions and 1 deletions

View File

@ -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,
})
}
}

View File

@ -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