Fix: room list total is zero

pull/21/head
zijiren233 2 years ago
parent 98d51c0604
commit 145cc2d101

@ -169,6 +169,12 @@ func GetAllRoomsWithoutHidden(scopes ...func(*gorm.DB) *gorm.DB) []*model.Room {
return rooms return rooms
} }
func GetAllRoomsWithoutHiddenCount(scopes ...func(*gorm.DB) *gorm.DB) int64 {
var count int64
db.Model(&model.Room{}).Preload("Setting", "hidden = ?", false).Scopes(scopes...).Count(&count)
return count
}
func GetAllRoomsAndCreator(scopes ...func(*gorm.DB) *gorm.DB) []*model.Room { func GetAllRoomsAndCreator(scopes ...func(*gorm.DB) *gorm.DB) []*model.Room {
rooms := []*model.Room{} rooms := []*model.Room{}
db.Preload("Creator").Scopes(scopes...).Find(&rooms) db.Preload("Creator").Scopes(scopes...).Find(&rooms)

@ -109,11 +109,7 @@ func RoomList(ctx *gin.Context) {
// search mode, all, name, creator // search mode, all, name, creator
var search = ctx.DefaultQuery("search", "all") var search = ctx.DefaultQuery("search", "all")
scopes := []func(db *gorm.DB) *gorm.DB{ scopes := []func(db *gorm.DB) *gorm.DB{}
db.Paginate(page, pageSize),
}
total := 0
switch order { switch order {
case "createdAt": case "createdAt":
@ -132,7 +128,6 @@ func RoomList(ctx *gin.Context) {
scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword)))
} }
} }
resp = genRoomListResp(resp, scopes...)
case "roomName": case "roomName":
if desc { if desc {
scopes = append(scopes, db.OrderByDesc("name")) scopes = append(scopes, db.OrderByDesc("name"))
@ -149,7 +144,6 @@ func RoomList(ctx *gin.Context) {
scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword)))
} }
} }
resp = genRoomListResp(resp, scopes...)
case "roomId": case "roomId":
if desc { if desc {
scopes = append(scopes, db.OrderByIDDesc) scopes = append(scopes, db.OrderByIDDesc)
@ -166,14 +160,15 @@ func RoomList(ctx *gin.Context) {
scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword)))
} }
} }
resp = genRoomListResp(resp, scopes...)
default: default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order")) ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
return return
} }
resp = genRoomListResp(resp, append(scopes, db.Paginate(page, pageSize))...)
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"total": total, "total": db.GetAllRoomsWithoutHiddenCount(scopes...),
"list": resp, "list": resp,
})) }))
} }

Loading…
Cancel
Save