Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(users_pagination): Paginate /users endpoint #62

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
GMishx marked this conversation as resolved.
Show resolved Hide resolved
// @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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have something different here? (Not important BTW)

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
Loading