From 09724d56720f25b43343d43c1fab89e3a7b26d01 Mon Sep 17 00:00:00 2001 From: Nabil Ould Hamou Date: Wed, 28 Feb 2024 17:03:42 +0100 Subject: [PATCH] fix: Modification du users endpoint --- controllers/userController.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/controllers/userController.go b/controllers/userController.go index 0acab89..8483b68 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -8,20 +8,46 @@ import ( "github.com/google/uuid" "gorm.io/gorm" "net/http" + "strconv" ) // GetUsers Returns all users func GetUsers(c *gin.Context) { + page, err := strconv.Atoi(c.Query("page")) + if err != nil || page <= 0 { + page = 1 + } + + type userResponse struct { + ID uuid.UUID `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + Avatar string `json:"profile_picture"` + } + var users []models.User - result := initializers.DB.Find(&users) + offset := (page - 1) * 50 + + result := initializers.DB.Offset(offset).Limit(50).Find(&users) if result.Error != nil { c.Status(http.StatusBadRequest) return } + var response []userResponse + for _, user := range users { + respUser := userResponse{ + ID: user.ID, + Username: user.Username, + DisplayName: user.DisplayName, + Avatar: user.Avatar.FileName, + } + response = append(response, respUser) + } + c.JSON(http.StatusAccepted, gin.H{ - "users": users, + "users": response, }) }