add: getAllUsers
This commit is contained in:
@@ -31,6 +31,58 @@ type RegistrationData struct {
|
|||||||
User models.User `json:"user"`
|
User models.User `json:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) CurrentUserHandler(c *gin.Context) {
|
||||||
|
userIDInterface, ok := c.Get("user_id")
|
||||||
|
if !ok || userIDInterface == nil {
|
||||||
|
logger.Error.Printf("Error getting user_id from header")
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
||||||
|
"field": "general",
|
||||||
|
"key": "server.validation.no_user_id_provided",
|
||||||
|
}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, ok := userIDInterface.(uint)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
logger.Error.Printf("Error: user_id is not of type uint")
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
||||||
|
"field": "user",
|
||||||
|
"key": "server.error.internal_server_error",
|
||||||
|
}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := uc.Service.GetUserByID(uint(userID))
|
||||||
|
if err != nil {
|
||||||
|
logger.Error.Printf("Error retrieving valid user: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
||||||
|
"field": "general",
|
||||||
|
"key": "server.error.internal_server_error",
|
||||||
|
}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"user": user.Safe(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) GetAllUsers(c *gin.Context) {
|
||||||
|
users, err := uc.Service.GetUsers(nil)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error.Printf("Error retrieving users: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
||||||
|
"field": "general",
|
||||||
|
"key": "server.error.internal_server_error",
|
||||||
|
}}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"users": users,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (uc *UserController) UpdateHandler(c *gin.Context) {
|
func (uc *UserController) UpdateHandler(c *gin.Context) {
|
||||||
var user models.User
|
var user models.User
|
||||||
if err := c.ShouldBindJSON(&user); err != nil {
|
if err := c.ShouldBindJSON(&user); err != nil {
|
||||||
@@ -141,42 +193,6 @@ func (uc *UserController) UpdateHandler(c *gin.Context) {
|
|||||||
c.JSON(http.StatusAccepted, gin.H{"message": "User updated successfully", "user": updatedUser})
|
c.JSON(http.StatusAccepted, gin.H{"message": "User updated successfully", "user": updatedUser})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *UserController) CurrentUserHandler(c *gin.Context) {
|
|
||||||
userIDInterface, ok := c.Get("user_id")
|
|
||||||
if !ok || userIDInterface == nil {
|
|
||||||
logger.Error.Printf("Error getting user_id from header")
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
|
||||||
"field": "general",
|
|
||||||
"key": "server.validation.no_user_id_provided",
|
|
||||||
}}})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userID, ok := userIDInterface.(uint)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
logger.Error.Printf("Error: user_id is not of type uint")
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
|
||||||
"field": "user",
|
|
||||||
"key": "server.error.internal_server_error",
|
|
||||||
}}})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := uc.Service.GetUserByID(uint(userID))
|
|
||||||
if err != nil {
|
|
||||||
logger.Error.Printf("Error retrieving valid user: %v", err)
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errors": []gin.H{{
|
|
||||||
"field": "general",
|
|
||||||
"key": "server.error.internal_server_error",
|
|
||||||
}}})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"user": user.Safe(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (uc *UserController) LogoutHandler(c *gin.Context) {
|
func (uc *UserController) LogoutHandler(c *gin.Context) {
|
||||||
tokenString, err := c.Cookie("jwt")
|
tokenString, err := c.Cookie("jwt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ func RegisterRoutes(router *gin.Engine, userController *controllers.UserControll
|
|||||||
userRouter.GET("/current", userController.CurrentUserHandler)
|
userRouter.GET("/current", userController.CurrentUserHandler)
|
||||||
userRouter.POST("/logout", userController.LogoutHandler)
|
userRouter.POST("/logout", userController.LogoutHandler)
|
||||||
userRouter.PATCH("/update", userController.UpdateHandler)
|
userRouter.PATCH("/update", userController.UpdateHandler)
|
||||||
|
userRouter.GET("/all", userController.GetAllUsers)
|
||||||
}
|
}
|
||||||
|
|
||||||
membershipRouter := router.Group("/backend/membership")
|
membershipRouter := router.Group("/backend/membership")
|
||||||
|
|||||||
@@ -115,6 +115,9 @@ func (service *UserService) GetUserByEmail(email string) (*models.User, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *UserService) GetUsers(where map[string]interface{}) (*[]models.User, error) {
|
func (service *UserService) GetUsers(where map[string]interface{}) (*[]models.User, error) {
|
||||||
|
if where == nil {
|
||||||
|
where = map[string]interface{}{}
|
||||||
|
}
|
||||||
return service.Repo.GetUsers(where)
|
return service.Repo.GetUsers(where)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user