Changes response of POST,PUT and DELETE:

For being able to easily debug and test the checkpoints which are
    adding, modifying or deleting a user the response should be the
    affected user object. Not only its ID.
This commit is contained in:
smavros 2019-08-18 15:50:23 +02:00
parent d3aa873732
commit 02f2156c1e
2 changed files with 12 additions and 13 deletions

View file

@ -56,7 +56,7 @@ func (r *updateUserRequest) updatedUser(role interface{},
// Only the Admin must be able to update user's role // Only the Admin must be able to update user's role
if role != "Admin" && r.Role != u.Role { if role != "Admin" && r.Role != u.Role {
return u, fmt.Errorf("Only Admin can update user's Role") return u, fmt.Errorf("Only Admin can update user's Role")
} else { } else if role == "Admin" && r.Role != "" {
u.Role = r.Role u.Role = r.Role
} }
@ -64,7 +64,10 @@ func (r *updateUserRequest) updatedUser(role interface{},
if err := u.ByUsername(r.Username); err == nil { if err := u.ByUsername(r.Username); err == nil {
return u, fmt.Errorf("Username is alreaday taken") return u, fmt.Errorf("Username is alreaday taken")
} }
if r.Username != "" {
u.Username = r.Username u.Username = r.Username
}
// If there is a new password then hash it and update it // If there is a new password then hash it and update it
if r.Password != "" { if r.Password != "" {
@ -74,8 +77,10 @@ func (r *updateUserRequest) updatedUser(role interface{},
} }
} }
// Update male // Update mail
if r.Mail != "" {
u.Mail = r.Mail u.Mail = r.Mail
}
return u, nil return u, nil
} }

View file

@ -225,9 +225,7 @@ func addUser(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{"user": newUser.User})
"id": newUser.ID,
})
} }
// UpdateUser godoc // UpdateUser godoc
@ -319,9 +317,7 @@ func updateUser(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{"user": updatedUser.User})
"id": updatedUser.ID,
})
} }
// GetUser godoc // GetUser godoc
@ -393,7 +389,5 @@ func deleteUser(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{"user": user})
"id": user.ID,
})
} }