diff --git a/src/wanijo/attribute/db.clj b/src/wanijo/attribute/db.clj index 5009ff5..d5df3e4 100644 --- a/src/wanijo/attribute/db.clj +++ b/src/wanijo/attribute/db.clj @@ -12,6 +12,16 @@ findy-by-schema {:uuid schema-uuid}))) +(neo4j/defquery with-name + "MATCH (a:attribute) + WHERE a.name = {name} + RETURN a") +(defn with-name! [name] + (-> (neo4j/exec-query! with-name + {:name name}) + first + :a)) + (neo4j/defquery create-new "MATCH (s:schema {uuid:{schema_uuid}}), (u:user {uuid:{user_uuid}}) diff --git a/src/wanijo/schema/forms.clj b/src/wanijo/schema/forms.clj index 1933c06..2a93f0c 100644 --- a/src/wanijo/schema/forms.clj +++ b/src/wanijo/schema/forms.clj @@ -5,18 +5,11 @@ [clojure.spec.alpha :as spec])) (defn unique-attr-name-in-schema! [req] - (let [schema-uuid (get-in req [:params :schema]) - attr-uuid (get-in req [:params :uuid]) + (let [attr-uuid (get-in req [:params :uuid]) name (get-in req [:params :name]) - duplicates (->> (db-attr/find-by-schema! schema-uuid) - (map #(if (or (and (empty? attr-uuid) - (= name (:name %))) - (and (seq attr-uuid) - (= name (:name %)) - (not= attr-uuid (:uuid %)))) - 1 0)) - (apply +))] - (zero? duplicates))) + matching-attr (db-attr/with-name! name)] + (or (nil? matching-attr) + (= attr-uuid (:uuid matching-attr))))) (spec/def ::unique-attr-name-per-schema unique-attr-name-in-schema!)