From c4f1ff1898cf07298d19aefcdb2f3118eba16771 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Mon, 23 Oct 2023 13:29:41 +0800 Subject: [PATCH] Feat: user room list --- server/handlers/init.go | 2 + server/handlers/user.go | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/server/handlers/init.go b/server/handlers/init.go index 02152bd..551f4e8 100644 --- a/server/handlers/init.go +++ b/server/handlers/init.go @@ -108,6 +108,8 @@ func Init(e *gin.Engine) { needAuthUser.POST("/logout", LogoutUser) needAuthUser.GET("/me", Me) + + needAuthUser.GET("/rooms", UserRooms) } } } diff --git a/server/handlers/user.go b/server/handlers/user.go index c929719..4b2c01f 100644 --- a/server/handlers/user.go +++ b/server/handlers/user.go @@ -4,8 +4,10 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/synctv-org/synctv/internal/db" "github.com/synctv-org/synctv/internal/op" "github.com/synctv-org/synctv/server/model" + "gorm.io/gorm" ) func Me(ctx *gin.Context) { @@ -27,3 +29,88 @@ func LogoutUser(ctx *gin.Context) { ctx.Status(http.StatusNoContent) } + +func UserRooms(ctx *gin.Context) { + user := ctx.MustGet("user").(*op.User) + order := ctx.Query("order") + if order == "" { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("order is required")) + return + } + page, pageSize, err := GetPageAndPageSize(ctx) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) + return + } + resp := make([]*model.RoomListResp, 0, pageSize) + + var desc = ctx.DefaultQuery("sort", "desc") == "desc" + + // search mode, all, name, creator + var search = ctx.DefaultQuery("search", "all") + + scopes := []func(db *gorm.DB) *gorm.DB{ + db.WhereCreatorID(user.ID), + } + + switch order { + case "createdAt": + if desc { + scopes = append(scopes, db.OrderByCreatedAtDesc) + } else { + scopes = append(scopes, db.OrderByCreatedAtAsc) + } + if keyword := ctx.Query("keyword"); keyword != "" { + switch search { + case "all": + scopes = append(scopes, db.WhereRoomNameLikeOrCreatorIn(keyword, db.GerUsersIDByUsernameLike(keyword))) + case "name": + scopes = append(scopes, db.WhereRoomNameLike(keyword)) + case "creator": + scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) + } + } + case "roomName": + if desc { + scopes = append(scopes, db.OrderByDesc("name")) + } else { + scopes = append(scopes, db.OrderByAsc("name")) + } + if keyword := ctx.Query("keyword"); keyword != "" { + switch search { + case "all": + scopes = append(scopes, db.WhereRoomNameLikeOrCreatorIn(keyword, db.GerUsersIDByUsernameLike(keyword))) + case "name": + scopes = append(scopes, db.WhereRoomNameLike(keyword)) + case "creator": + scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) + } + } + case "roomId": + if desc { + scopes = append(scopes, db.OrderByIDDesc) + } else { + scopes = append(scopes, db.OrderByIDAsc) + } + if keyword := ctx.Query("keyword"); keyword != "" { + switch search { + case "all": + scopes = append(scopes, db.WhereRoomNameLikeOrCreatorIn(keyword, db.GerUsersIDByUsernameLike(keyword))) + case "name": + scopes = append(scopes, db.WhereRoomNameLike(keyword)) + case "creator": + scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword))) + } + } + default: + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order")) + return + } + + resp = genRoomListResp(resp, append(scopes, db.Paginate(page, pageSize))...) + + ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ + "total": db.GetAllRoomsWithoutHiddenCount(scopes...), + "list": resp, + })) +}