diff --git a/manager/handlers/user.go b/manager/handlers/user.go index c66086e7e..2f469a8b1 100644 --- a/manager/handlers/user.go +++ b/manager/handlers/user.go @@ -51,6 +51,42 @@ func (h *Handlers) GetUser(ctx *gin.Context) { ctx.JSON(http.StatusOK, user) } +// @Summary Get Users +// @Description Get Users +// @Tags CDN +// @Accept json +// @Produce json +// @Param page query int true "current page" default(0) +// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50) +// @Success 200 {object} []model.CDN +// @Failure 400 +// @Failure 404 +// @Failure 500 +// @Router /users [get] +func (h *Handlers) GetUsers(ctx *gin.Context) { + var query types.GetUsersQuery + if err := ctx.ShouldBindQuery(&query); err != nil { + ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()}) + return + } + + h.setPaginationDefault(&query.Page, &query.PerPage) + users, err := h.service.GetUsers(ctx.Request.Context(), query) + if err != nil { + ctx.Error(err) + return + } + + totalCount, err := h.service.UserTotalCount(ctx.Request.Context(), query) + if err != nil { + ctx.Error(err) + return + } + + h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(totalCount)) + ctx.JSON(http.StatusOK, users) +} + // @Summary SignUp user // @Description signup by json config // @Tags User diff --git a/manager/router/router.go b/manager/router/router.go index abd3358f6..0265376a1 100644 --- a/manager/router/router.go +++ b/manager/router/router.go @@ -97,6 +97,7 @@ func Init(cfg *config.Config, service service.REST, enforcer *casbin.Enforcer) ( // User u := apiv1.Group("/users") u.GET("/:id", jwt.MiddlewareFunc(), rbac, h.GetUser) + u.GET("", jwt.MiddlewareFunc(), rbac, h.GetUsers) u.POST("/signin", jwt.LoginHandler) u.POST("/signout", jwt.LogoutHandler) u.POST("/signup", h.SignUp) diff --git a/manager/service/service.go b/manager/service/service.go index b075794cf..25b88e9f0 100644 --- a/manager/service/service.go +++ b/manager/service/service.go @@ -34,6 +34,8 @@ import ( type REST interface { GetUser(context.Context, uint) (*model.User, error) + GetUsers(context.Context, types.GetUsersQuery) (*[]model.User, error) + UserTotalCount(context.Context, types.GetUsersQuery) (int64, error) SignIn(context.Context, types.SignInRequest) (*model.User, error) SignUp(context.Context, types.SignUpRequest) (*model.User, error) OauthSignin(context.Context, string) (string, error) diff --git a/manager/service/user.go b/manager/service/user.go index 6dee7d2c8..b59eb53a6 100644 --- a/manager/service/user.go +++ b/manager/service/user.go @@ -40,6 +40,34 @@ func (s *rest) GetUser(ctx context.Context, id uint) (*model.User, error) { return &user, nil } +func (s *rest) GetUsers(ctx context.Context, q types.GetUsersQuery) (*[]model.User, error) { + users := []model.User{} + if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.User{ + Name: q.Name, + Email: q.Email, + Location: q.Location, + State: q.State, + }).Find(&users).Error; err != nil { + return nil, err + } + + return &users, nil +} + +func (s *rest) UserTotalCount(ctx context.Context, q types.GetUsersQuery) (int64, error) { + var count int64 + if err := s.db.WithContext(ctx).Model(&model.User{}).Where(&model.User{ + Name: q.Name, + Email: q.Email, + Location: q.Location, + State: q.State, + }).Count(&count).Error; err != nil { + return 0, err + } + + return count, nil +} + func (s *rest) SignIn(ctx context.Context, json types.SignInRequest) (*model.User, error) { user := model.User{} if err := s.db.WithContext(ctx).First(&user, model.User{ diff --git a/manager/types/user.go b/manager/types/user.go index 8580d7bfa..b4d47c74d 100644 --- a/manager/types/user.go +++ b/manager/types/user.go @@ -20,6 +20,15 @@ type UserParams struct { ID uint `uri:"id" binding:"required"` } +type GetUsersQuery struct { + Name string `form:"name" binding:"omitempty"` + Email string `form:"email" binding:"omitempty"` + Location string `form:"location" binding:"omitempty"` + State string `form:"state" binding:"omitempty"` + Page int `form:"page" binding:"omitempty,gte=1"` + PerPage int `form:"per_page" binding:"omitempty,gte=1,lte=50"` +} + type SignInRequest struct { Name string `json:"name" binding:"required,min=3,max=10"` Password string `json:"password" binding:"required,min=8,max=20"`