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

View file

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