|
|
|
@ -127,6 +127,77 @@ func genUserListResp(us []*dbModel.User) []*model.UserInfoResp {
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetRoomUsers(ctx *gin.Context) {
|
|
|
|
|
id := ctx.Query("id")
|
|
|
|
|
if len(id) != 32 {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("room id error"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page, pageSize, err := GetPageAndPageSize(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var desc = ctx.DefaultQuery("sort", "desc") == "desc"
|
|
|
|
|
|
|
|
|
|
scopes := []func(db *gorm.DB) *gorm.DB{
|
|
|
|
|
db.PreloadRoomUserRelation(db.WhereRoomID(id)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ctx.DefaultQuery("order", "name") {
|
|
|
|
|
case "createdAt":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtDesc)
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtAsc)
|
|
|
|
|
}
|
|
|
|
|
case "name":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByDesc("username"))
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByAsc("username"))
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if keyword := ctx.Query("keyword"); keyword != "" {
|
|
|
|
|
// search mode, all, name, id
|
|
|
|
|
switch ctx.DefaultQuery("search", "all") {
|
|
|
|
|
case "all":
|
|
|
|
|
scopes = append(scopes, db.WhereUsernameLikeOrIDIn(keyword, db.GerUsersIDByIDLike(keyword)))
|
|
|
|
|
case "name":
|
|
|
|
|
scopes = append(scopes, db.WhereUsernameLike(keyword))
|
|
|
|
|
case "id":
|
|
|
|
|
scopes = append(scopes, db.WhereIDIn(db.GerUsersIDByIDLike(keyword)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
|
|
|
"total": db.GetAllUserCount(scopes...),
|
|
|
|
|
"list": genRoomUserListResp(db.GetAllUsers(append(scopes, db.Paginate(page, pageSize))...)),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func genRoomUserListResp(us []*dbModel.User) []*model.RoomUsersResp {
|
|
|
|
|
resp := make([]*model.RoomUsersResp, len(us))
|
|
|
|
|
for i, v := range us {
|
|
|
|
|
resp[i] = &model.RoomUsersResp{
|
|
|
|
|
UserID: v.ID,
|
|
|
|
|
Username: v.Username,
|
|
|
|
|
Role: v.Role,
|
|
|
|
|
JoinAt: v.RoomUserRelations[0].CreatedAt.UnixMilli(),
|
|
|
|
|
RoomID: v.RoomUserRelations[0].RoomID,
|
|
|
|
|
Status: v.RoomUserRelations[0].Status,
|
|
|
|
|
Permissions: v.RoomUserRelations[0].Permissions,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ApprovePendingUser(ctx *gin.Context) {
|
|
|
|
|
req := model.UserIDReq{}
|
|
|
|
|
if err := model.Decode(ctx, &req); err != nil {
|
|
|
|
@ -259,6 +330,62 @@ func Rooms(ctx *gin.Context) {
|
|
|
|
|
scopes = append(scopes, db.WhereRoomNameLike(keyword))
|
|
|
|
|
case "creator":
|
|
|
|
|
scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword)))
|
|
|
|
|
case "creatorId":
|
|
|
|
|
scopes = append(scopes, db.WhereCreatorID(keyword))
|
|
|
|
|
case "id":
|
|
|
|
|
scopes = append(scopes, db.WhereIDLike(keyword))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
|
|
|
"total": db.GetAllRoomsCount(scopes...),
|
|
|
|
|
"list": genRoomListResp(append(scopes, db.Paginate(page, pageSize))...),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetUserRooms(ctx *gin.Context) {
|
|
|
|
|
id := ctx.Query("id")
|
|
|
|
|
if len(id) != 32 {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("user id error"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
page, pageSize, err := GetPageAndPageSize(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var desc = ctx.DefaultQuery("sort", "desc") == "desc"
|
|
|
|
|
|
|
|
|
|
scopes := []func(db *gorm.DB) *gorm.DB{
|
|
|
|
|
db.WhereCreatorID(id),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ctx.DefaultQuery("order", "name") {
|
|
|
|
|
case "createdAt":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtDesc)
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByCreatedAtAsc)
|
|
|
|
|
}
|
|
|
|
|
case "name":
|
|
|
|
|
if desc {
|
|
|
|
|
scopes = append(scopes, db.OrderByDesc("name"))
|
|
|
|
|
} else {
|
|
|
|
|
scopes = append(scopes, db.OrderByAsc("name"))
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if keyword := ctx.Query("keyword"); keyword != "" {
|
|
|
|
|
// search mode, all, name, creator
|
|
|
|
|
switch ctx.DefaultQuery("search", "all") {
|
|
|
|
|
case "all":
|
|
|
|
|
scopes = append(scopes, db.WhereRoomNameLikeOrIDLike(keyword, keyword))
|
|
|
|
|
case "name":
|
|
|
|
|
scopes = append(scopes, db.WhereRoomNameLike(keyword))
|
|
|
|
|
case "id":
|
|
|
|
|
scopes = append(scopes, db.WhereIDLike(keyword))
|
|
|
|
|
}
|
|
|
|
|