From 7c5a7e88aa8dd82e616addf245ffe1503f749967 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Sun, 12 Nov 2023 13:52:13 +0800 Subject: [PATCH] Opt: search user and room default status query --- internal/model/room.go | 2 +- internal/op/rooms.go | 13 +++++++------ server/handlers/admin.go | 12 ++++-------- server/handlers/room.go | 1 + 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/internal/model/room.go b/internal/model/room.go index e259672..c403d56 100644 --- a/internal/model/room.go +++ b/internal/model/room.go @@ -9,7 +9,7 @@ import ( "gorm.io/gorm" ) -type RoomStatus uint +type RoomStatus uint64 const ( RoomStatusBanned RoomStatus = 1 diff --git a/internal/op/rooms.go b/internal/op/rooms.go index f5729dc..7e4da90 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -189,12 +189,13 @@ func GetAllRoomsInCacheWithoutHidden() []*Room { } type RoomInfo struct { - RoomId string `json:"roomId"` - RoomName string `json:"roomName"` - PeopleNum int64 `json:"peopleNum"` - NeedPassword bool `json:"needPassword"` - Creator string `json:"creator"` - CreatedAt int64 `json:"createdAt"` + RoomId string `json:"roomId"` + RoomName string `json:"roomName"` + PeopleNum int64 `json:"peopleNum"` + NeedPassword bool `json:"needPassword"` + Creator string `json:"creator"` + CreatedAt int64 `json:"createdAt"` + Status model.RoomStatus `json:"status"` } func GetRoomHeapInCacheWithoutHidden() []*RoomInfo { diff --git a/server/handlers/admin.go b/server/handlers/admin.go index e2657cf..93e11c1 100644 --- a/server/handlers/admin.go +++ b/server/handlers/admin.go @@ -65,7 +65,7 @@ func Users(ctx *gin.Context) { scopes := []func(db *gorm.DB) *gorm.DB{} - switch ctx.DefaultQuery("role", "user") { + switch ctx.Query("role") { case "admin": scopes = append(scopes, db.WhereRole(dbModel.RoleAdmin)) case "user": @@ -74,9 +74,8 @@ func Users(ctx *gin.Context) { scopes = append(scopes, db.WhereRole(dbModel.RolePending)) case "banned": scopes = append(scopes, db.WhereRole(dbModel.RoleBanned)) - default: - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support role")) - return + case "root": + scopes = append(scopes, db.WhereRole(dbModel.RoleRoot)) } switch ctx.DefaultQuery("order", "name") { @@ -201,16 +200,13 @@ func Rooms(ctx *gin.Context) { scopes := []func(db *gorm.DB) *gorm.DB{} - switch ctx.DefaultQuery("status", "active") { + switch ctx.Query("status") { case "active": scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusActive)) case "pending": scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusPending)) case "banned": scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusBanned)) - default: - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support status")) - return } switch ctx.DefaultQuery("order", "name") { diff --git a/server/handlers/room.go b/server/handlers/room.go index 2cbe829..8f88828 100644 --- a/server/handlers/room.go +++ b/server/handlers/room.go @@ -147,6 +147,7 @@ func genRoomListResp(scopes ...func(db *gorm.DB) *gorm.DB) []*model.RoomListResp NeedPassword: len(r.HashedPassword) != 0, Creator: op.GetUserName(r.CreatorID), CreatedAt: r.CreatedAt.UnixMilli(), + Status: r.Status, } } return resp