Exemplo n.º 1
0
Arquivo: users.go Projeto: vmalguy/tat
// Update changes fullname and email
func (*UsersController) Update(ctx *gin.Context) {
	var updateJSON updateUserJSON
	ctx.Bind(&updateJSON)

	var userToUpdate = models.User{}
	err := userToUpdate.FindByUsername(updateJSON.Username)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("user with username %s does not exist", updateJSON.Username)})
		return
	}

	if strings.TrimSpace(updateJSON.NewFullname) == "" || strings.TrimSpace(updateJSON.NewEmail) == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("Invalid Fullname %s or Email %s", updateJSON.NewFullname, updateJSON.NewEmail)})
		return
	}

	err = userToUpdate.Update(strings.TrimSpace(updateJSON.NewFullname), strings.TrimSpace(updateJSON.NewEmail))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Update %s user to fullname %s and email %s failed : %s", updateJSON.Username, updateJSON.NewFullname, updateJSON.NewEmail, err.Error())})
		return
	}

	ctx.JSON(http.StatusCreated, "")
}