Skip to content

Commit

Permalink
feat: add user list interface (dragonflyoss#771)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Oct 29, 2021
1 parent 0d2ae1f commit 4aa395a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
36 changes: 36 additions & 0 deletions manager/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions manager/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions manager/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions manager/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions manager/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 4aa395a

Please sign in to comment.