func (self *Handle) DeleteX(obj Model, buildFn func(sq.DeleteBuilder) sq.DeleteBuilder) int { b := sq.Delete(obj.GetTable()) b = buildFn(b) s, args, err := b.ToSql() if err != nil { log.WithFields(log.Fields{ "table": obj.GetTable(), "args": args, "err": err, }).Panic("Fail To Build DELETE SQL") } res, err := self.Conn.Exec(s, args...) if err != nil { log.WithFields(log.Fields{ "table": obj.GetTable(), "args": args, "err": err, }).Panic("Fail To Execute DELTE SQL") } i, _ := res.RowsAffected() return int(i) }
func organizationDelete(w http.ResponseWriter, r *http.Request) { org_id := mux.Vars(r)["id"] query, args, err := sq.Delete("organization"). Where(sq.Eq{"org_id": org_id}). ToSql() errors.CheckHttpErr(err != nil, "Internal Error", 500) result, err := db.DB.Exec(query, args...) errors.CheckHttpErr(err != nil, "Internal Error", 500) affected, err := result.RowsAffected() errors.CheckHttpErr(affected == 0, "Organization not found.", 404) errors.CheckHttpErr(err != nil, "Internal Error", 500) w.Write([]byte("Organization deleted.\n")) }
func userDelete(w http.ResponseWriter, r *http.Request) { user_id := mux.Vars(r)["id"] query, args, err := sq.Delete("user"). Where(sq.Eq{"user_id": user_id}). ToSql() errors.CheckHttpErr(err != nil, "Internal Error", 500) result, err := db.DB.Exec(query, args...) // TODO do better error handling errors.CheckHttpErr(err != nil, "Internal Error", 500) affected, err := result.RowsAffected() errors.CheckHttpErr(affected == 0, "User not found.", 404) errors.CheckHttpErr(err != nil, "Internal Error", 500) w.Write([]byte("User deleted.\n")) }