Example #1
0
// Update merges remote object state into database database, overwritting values.
// ID must be the same as session.
func Update(c *echo.Context) (int, interface{}) {
	reqid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	attid, _ := c.Get("digitsID").(int64)

	if reqid == attid {
		var profile models.ProfileUpdate
		var err error

		// Bind body to profile data
		err = c.Bind(&profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// Valdate profile data
		err = validator.Validate(profile)
		if err != nil {
			return msg.BadRequest(err)
		}

		// If email is written, require confirmation again
		if profile.Email != "" {
			profile.RequiresConfirm = true
		}

		// Execute update
		log.Printf("Awaiting for [Profile = %d] update", reqid)
		err = mangos.Execute(collection, func(c *mgo.Collection) error {
			return c.Update(bson.M{"_id": reqid}, bson.M{"$set": profile})
		})
		if err != nil {
			log.Printf("Cannot update [Profile = %d]: %s", reqid, err)
			return msg.InternalError(err)
		}

		log.Printf("Updated Profile ->\n%s", msg.Detail(profile))
		return msg.Ok("updated")
	}

	return msg.Forbidden("the id does not belongs to user")
}