From db6b3f31e4c206281f91e2e953829a965aad4a55 Mon Sep 17 00:00:00 2001 From: deo002 Date: Wed, 22 May 2024 14:53:23 +0530 Subject: [PATCH] fix(users_pagination): Paginate /users endpoint Signed-off-by: deo002 --- cmd/laas/docs/docs.go | 14 ++++++++++++++ cmd/laas/docs/swagger.json | 14 ++++++++++++++ cmd/laas/docs/swagger.yaml | 9 +++++++++ pkg/auth/auth.go | 21 +++++++++++++-------- pkg/middleware/middleware.go | 13 ++++++++++--- pkg/models/types.go | 6 +++--- 6 files changed, 63 insertions(+), 14 deletions(-) diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index 8ecb21d..e5639ad 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -1344,6 +1344,20 @@ const docTemplate = `{ ], "summary": "Get users", "operationId": "GetAllUsers", + "parameters": [ + { + "type": "integer", + "description": "Page number", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "Number of records per page", + "name": "limit", + "in": "query" + } + ], "responses": { "200": { "description": "OK", diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index 1372cd1..19354bd 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -1337,6 +1337,20 @@ ], "summary": "Get users", "operationId": "GetAllUsers", + "parameters": [ + { + "type": "integer", + "description": "Page number", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "Number of records per page", + "name": "limit", + "in": "query" + } + ], "responses": { "200": { "description": "OK", diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index f0d4d57..99ffdc8 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -1561,6 +1561,15 @@ paths: - application/json description: Get all service users operationId: GetAllUsers + parameters: + - description: Page number + in: query + name: page + type: integer + - description: Number of records per page + in: query + name: limit + type: integer produces: - application/json responses: diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 0f9c13e..cbc285c 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -96,7 +96,7 @@ func CreateUser(c *gin.Context) { res := models.UserResponse{ Data: []models.User{user}, Status: http.StatusCreated, - Meta: models.PaginationMeta{ + Meta: &models.PaginationMeta{ ResourceCount: 1, }, } @@ -112,14 +112,19 @@ func CreateUser(c *gin.Context) { // @Tags Users // @Accept json // @Produce json -// @Success 200 {object} models.UserResponse -// @Failure 404 {object} models.LicenseError "Users not found" +// @Param page query int false "Page number" +// @Param limit query int false "Number of records per page" +// @Success 200 {object} models.UserResponse +// @Failure 404 {object} models.LicenseError "Users not found" // @Security ApiKeyAuth // @Router /users [get] func GetAllUser(c *gin.Context) { var users []models.User - if err := db.DB.Find(&users).Error; err != nil { + query := db.DB.Model(&models.User{}) + _ = utils.PreparePaginateResponse(c, query, &models.UserResponse{}) + + if err := query.Find(&users).Error; err != nil { er := models.LicenseError{ Status: http.StatusNotFound, Message: "Users not found", @@ -136,7 +141,7 @@ func GetAllUser(c *gin.Context) { res := models.UserResponse{ Data: users, Status: http.StatusOK, - Meta: models.PaginationMeta{ + Meta: &models.PaginationMeta{ ResourceCount: len(users), }, } @@ -181,7 +186,7 @@ func GetUser(c *gin.Context) { res := models.UserResponse{ Data: []models.User{user}, Status: http.StatusOK, - Meta: models.PaginationMeta{ + Meta: &models.PaginationMeta{ ResourceCount: 1, }, } @@ -197,8 +202,8 @@ func GetUser(c *gin.Context) { // @Tags Users // @Accept json // @Produce json -// @Param user body models.UserLogin true "Login credentials" -// @Success 200 {object} object{token=string} "JWT token" +// @Param user body models.UserLogin true "Login credentials" +// @Success 200 {object} object{token=string} "JWT token" // @Router /login [post] func Login(c *gin.Context) { var input models.UserLogin diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 04b751c..b5d25d5 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -69,7 +69,7 @@ func AuthenticationMiddleware() gin.HandlerFunc { er := models.LicenseError{ Status: http.StatusUnauthorized, Message: "Invalid token", - Error: err.Error(), + Error: "Invalid token", Path: c.Request.URL.Path, Timestamp: time.Now().Format(time.RFC3339), } @@ -82,8 +82,7 @@ func AuthenticationMiddleware() gin.HandlerFunc { userId := int64(claims["id"].(float64)) var user models.User - result := db.DB.Where(models.User{Id: userId}).First(&user) - if result.Error != nil { + if err := db.DB.Where(models.User{Id: userId}).First(&user).Error; err != nil { er := models.LicenseError{ Status: http.StatusUnauthorized, Message: "User not found", @@ -161,9 +160,11 @@ func PaginationMiddleware() gin.HandlerFunc { var licenseRes models.LicenseResponse var obligationRes models.ObligationResponse var auditRes models.AuditResponse + var userRes models.UserResponse isLicenseRes := false isObligationRes := false isAuditRes := false + isUserRes := false responseModel, _ := c.Get("responseModel") switch responseModel.(type) { case *models.LicenseResponse: @@ -178,6 +179,10 @@ func PaginationMiddleware() gin.HandlerFunc { err = json.Unmarshal(originalBody, &auditRes) isAuditRes = true metaObject = auditRes.Meta + case *models.UserResponse: + err = json.Unmarshal(originalBody, &userRes) + isUserRes = true + metaObject = userRes.Meta default: err = fmt.Errorf("unknown response model type") } @@ -218,6 +223,8 @@ func PaginationMiddleware() gin.HandlerFunc { newBody, err = json.Marshal(obligationRes) } else if isAuditRes { newBody, err = json.Marshal(auditRes) + } else if isUserRes { + newBody, err = json.Marshal(userRes) } if err != nil { log.Fatalf("Error marshalling new body: %s", err.Error()) diff --git a/pkg/models/types.go b/pkg/models/types.go index 98f2eed..a4f1e9c 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -271,9 +271,9 @@ type UserLogin struct { // UserResponse struct is representation of design API response of user. type UserResponse struct { - Status int `json:"status" example:"200"` - Data []User `json:"data"` - Meta PaginationMeta `json:"paginationmeta"` + Status int `json:"status" example:"200"` + Data []User `json:"data"` + Meta *PaginationMeta `json:"paginationmeta"` } // SearchLicense struct represents the input needed to search in a license.