add: getAllUsers
This commit is contained in:
@@ -31,6 +31,58 @@ type RegistrationData struct {
|
||||
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) {
|
||||
var user models.User
|
||||
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})
|
||||
}
|
||||
|
||||
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) {
|
||||
tokenString, err := c.Cookie("jwt")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user