Skip to content

Commit

Permalink
Merge pull request #62 from siemens/users_paginate
Browse files Browse the repository at this point in the history
fix(users_pagination): Paginate /users endpoint

Reviewed-by: [email protected], [email protected]
Tested-by: [email protected]
  • Loading branch information
GMishx authored Jun 3, 2024
2 parents 51e67b4 + db6b3f3 commit ee3282d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
14 changes: 14 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 13 additions & 8 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand All @@ -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",
Expand All @@ -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),
},
}
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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")
}
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ee3282d

Please sign in to comment.