                          (let [body-map (reduce conj {} decoded-body)]
                            (parse-by-schema body-map profile-schema))
                          decoded-body)
-              entity_id  (-> item :entity_id)
+              {:keys [entity_id profile_id]} item
               username   (sec/session-username (-> ctx :request :session))
               user_id    (-> (db/execute session (hayt/select :users (hayt/where [[= :username username]]))) first :id)
-              profile_id (-> item :profile_id)]
+              ]
           (db/execute session (hayt/insert :profiles (hayt/values (-> body
                                                                       (assoc :id profile_id)
                                                                       (assoc :user_id user_id)


(defn resource-exists? [store ctx]
  (db/with-session [session (:hecuba-session store)]
    (let [request    (-> ctx :request)
          _          (log/infof "resource-exists? request: %s" request)
          entity_id  (-> request :params :entity_id)
          profile_id (-> request :params :profile_id)

  while input is expected to be a hash-map representing the profile, with strings as keys"
  (let [association-name   (:name   attr)
        association-schema (:schema attr)
        nested-item (reduce
                      (fn [nested-item nested-attr]
                        (let [nested-attr-name (name nested-attr)
                              exploded-attr-name (str (name association-name) "_" nested-attr-name)]
                          (conj nested-item { nested-attr (input exploded-attr-name)})))
                      {}
                      association-schema)]
    {association-name nested-item}))

(defn extract-associated-item [association-name association-schema input index]
  "Extracts the item belonging to a 'has many' association from input, at position index."
  (reduce
    (fn [associated-item associated-item-attr]
      (let [associated-item-attr-name (name associated-item-attr)
            exploded-attr-name (str (name association-name) "_" index "_" associated-item-attr-name)]
        (conj associated-item { associated-item-attr (input exploded-attr-name)})))
    {}
    association-schema))

(defn extract-associated-items [attr input]
  "Extracts a collection representing a 'has many' association from input.
