From ab59887933c93fd94dc542ffede6927b66b876b6 Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Thu, 19 Jun 2025 19:03:35 +0800 Subject: [PATCH] refactor: streamline JSON response structure in channel API endpoints --- controller/channel.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index acaf2977..13ed72b3 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -102,14 +102,14 @@ func GetAllChannels(c *gin.Context) { typeCounts, _ := model.CountChannelsGroupByType() c.JSON(http.StatusOK, gin.H{ - "success": true, - "message": "", - "data": gin.H{ - "items": channelData, - "total": total, - "page": p, - "page_size": pageSize, - "type_counts": typeCounts, + "success": true, + "message": "", + "data": gin.H{ + "items": channelData, + "total": total, + "page": p, + "page_size": pageSize, + "type_counts": typeCounts, }, }) return @@ -237,10 +237,20 @@ func SearchChannels(c *gin.Context) { } channelData = channels } + + // calculate type counts for search results + typeCounts := make(map[int64]int64) + for _, channel := range channelData { + typeCounts[int64(channel.Type)]++ + } + c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", - "data": channelData, + "data": gin.H{ + "items": channelData, + "type_counts": typeCounts, + }, }) return }