func organizationPut(w http.ResponseWriter, r *http.Request) {
	org_id := mux.Vars(r)["id"]
	err := r.ParseForm()
	errors.CheckHttpErr(err != nil, "Internal Error", 500)
	form := r.PostForm

	// TODO validation of form (send email for confirmation) (have whitelist of params to accept)
	// TODO confirm admin permissions

	name := form["name"][0]
	city := form["city"][0]
	latitude := form["latitude"][0]
	longitude := form["longitude"][0]

	query, args, err := sq.Update("organization").
		Set("name", name).
		Set("city", city).
		Set("latitude", latitude).
		Set("longitude", longitude).
		Where(sq.Eq{"org_id": org_id}).
		ToSql()
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	_, err = db.DB.Exec(query, args...)
	if mysqlError, ok := err.(*mysql.MySQLError); ok {
		errors.CheckHttpErr(mysqlError.Number == 1062, "Organization Already Exists", 409)
	}
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	w.Write([]byte("Organization updated.\n"))
}
示例#2
0
func (self *Handle) UpdateX(obj Model, values map[string]interface{}, buildFn func(sq.UpdateBuilder) sq.UpdateBuilder) int {

	b := sq.Update(obj.GetTable()).SetMap(values)
	b = buildFn(b)

	s, args, err := b.ToSql()
	if err != nil {
		log.WithFields(log.Fields{
			"table":  obj.GetTable(),
			"values": values,
			"err":    err,
		}).Panic("Fail To Build Update SQL")
	}

	res, err := self.Conn.Exec(s, args...)
	if err != nil {
		log.WithFields(log.Fields{
			"table":  obj.GetTable(),
			"values": values,
			"err":    err,
		}).Panic("Fail To Execute Update SQL")
	}

	i, _ := res.RowsAffected()

	return int(i)
}
示例#3
0
func userPut(w http.ResponseWriter, r *http.Request) {
	user_id := mux.Vars(r)["id"]
	err := r.ParseForm()
	errors.CheckHttpErr(err != nil, "Internal Error", 500)
	form := r.PostForm

	errors.CheckHttpErr(len(form) == 0, "Bad Request. Body must not be empty.", 400)

	// TODO validation of form (send email for confirmation) (have whitelist of params to accept)
	// TODO confirm admin permissions
	// TODO better error handling

	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(form["password"][0]), bcrypt.DefaultCost)
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	name := form["name"][0]
	email := form["email"][0]
	password := string(hashedPassword)

	query, args, err := sq.Update("user").
		Set("name", name).
		Set("email", email).
		Set("password", password).
		Where(sq.Eq{"user_id": user_id}).
		ToSql()
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	result, err := db.DB.Exec(query, args...)
	// TODO update session ?
	if mysqlError, ok := err.(*mysql.MySQLError); ok {
		errors.CheckHttpErr(mysqlError.Number == 1062, "Email Already Exists", 409)
	}
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	affected, err := result.RowsAffected()
	errors.CheckHttpErr(affected == 0, "Request failed. User may not exist, or request had no updates.", 404)
	errors.CheckHttpErr(err != nil, "Internal Error", 500)

	w.Write([]byte("User updated.\n"))
}