From 4aa3a14ba26d75d7c4ab654b479465a71d86ac76 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 20 Aug 2019 15:52:44 +0200 Subject: [PATCH] move logic from instance.db to instance.domain --- src/wanijo/instance/db.clj | 71 ++++------------------------- src/wanijo/instance/domain.clj | 65 ++++++++++++++++++++++++++ src/wanijo/instance/forms.clj | 3 +- src/wanijo/instance/view.clj | 1 - src/wanijo/tag/routes.clj | 4 +- src/wanijo/visualisation/db.clj | 9 ++-- src/wanijo/visualisation/routes.clj | 10 ++-- 7 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 src/wanijo/instance/domain.clj diff --git a/src/wanijo/instance/db.clj b/src/wanijo/instance/db.clj index 2f73edc..327ad45 100644 --- a/src/wanijo/instance/db.clj +++ b/src/wanijo/instance/db.clj @@ -1,33 +1,9 @@ (ns wanijo.instance.db (:require [clojure.spec.alpha :as spec] - [wanijo.specs :as specs] [wanijo.infrastructure.neo4j :as neo4j] - [wanijo.schema.db :as domain-schema] - [wanijo.attribute.domain :as domain-attr] - [wanijo.tag.db :as db-tag] - [wanijo.link.db :as db-link])) + [wanijo.instance.domain :as domain-instance] + [wanijo.tag.db :as db-tag])) -(spec/def ::instance - (spec/keys :req-un [::neo4j/uuid - ::specs/created_at - ::specs/updated_at - ::specs/name])) -(spec/def ::properties - (spec/coll-of - (spec/keys :req-un [::neo4j/uuid - ::specs/created_at - ::specs/updated_at - ::domain-attr/attribute]))) -(spec/def ::target ::instance) -(spec/def ::source ::instance) -(spec/def ::tags - (spec/coll-of ::db-tag/tag)) -(spec/def ::contains-schema - (spec/keys :req-un [::domain-schema/schema])) - -(spec/def ::instances-with-tags - (spec/coll-of (spec/merge ::instance - (spec/keys :req-un [::tags])))) (neo4j/defquery findy-by-schema "MATCH (i:instance)-[:of]->(s:schema) WHERE s.uuid = {uuid} @@ -35,7 +11,7 @@ (i)-[:tagged_with]->(t:tag) RETURN i, t") (defn find-by-schema! [schema-uuid] - {:post [(spec/assert ::instances-with-tags %)]} + {:post [(spec/assert ::domain-instance/instances-with-tags %)]} (->> (neo4j/exec-query! findy-by-schema {:uuid schema-uuid}) (group-by :i) @@ -84,13 +60,11 @@ (concat [instance-tuple] prop-tuples)))) -(spec/def ::instance-with-schema - (spec/merge ::instance ::contains-schema)) (neo4j/defquery find-by-uuid "MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema) RETURN i, s") (defn find-by-uuid! [uuid] - {:post [(spec/assert ::instance-with-schema %)]} + {:post [(spec/assert ::domain-instance/instance-with-schema %)]} (->> (neo4j/exec-query! find-by-uuid {:uuid uuid}) (map #(assoc (:i %) @@ -111,15 +85,6 @@ (neo4j/exec-query! find-properties {:uuid uuid}))) -(spec/def ::prop-tuple-values - (spec/keys :req-un [::neo4j/uuid - ::specs/now - ::value - ::specs/instance_uuid - ::specs/attribute_uuid])) -(spec/def ::prop-tuple - (spec/coll-of - (spec/tuple fn? ::prop-tuple-values))) (neo4j/defquery edit-instance "MATCH (i:instance {uuid:{uuid}}) SET i.name = {name}, @@ -135,7 +100,7 @@ ON MATCH SET p.updated_at = {now}, p.value = {value}") (defn instance->prop-tuples [instance] - {:post [(spec/assert ::prop-tuple %)]} + {:post [(spec/assert ::domain-instance/prop-tuple %)]} (map (fn [prop] (let [prop-uuid (:uuid prop) uuid (if (empty? prop-uuid) @@ -192,12 +157,6 @@ #(-> % direction :name) #(-> % direction :created_at))))) -(spec/def ::links-out - (spec/coll-of - (spec/keys :req-un [::db-link/link - ::target - ::domain-schema/schema - ::tags]))) (neo4j/defquery outgoing-links "MATCH (inst:instance {uuid:{uuid}}), (inst)<-[:link_from]-(link:link), @@ -207,15 +166,9 @@ (target)-[:tagged_with]->(tag:tag) RETURN link, target, schema, tag") (defn outgoing-links! [uuid] - {:post [(spec/assert ::links-out %)]} + {:post [(spec/assert ::domain-instance/links-out %)]} (group-by-link-and-assoc-tags uuid outgoing-links :target)) -(spec/def ::links-in - (spec/coll-of - (spec/keys :req-un [::db-link/link - ::source - ::domain-schema/schema - ::tags]))) (neo4j/defquery incoming-links "MATCH (inst:instance {uuid:{uuid}}), (inst)<-[:link_to]-(link:link), @@ -226,19 +179,11 @@ RETURN link, source, schema, tag ORDER BY schema.name, source.name, source.created_at") (defn incoming-links! [uuid] - {:post [(spec/assert ::links-in %)]} + {:post [(spec/assert ::domain-instance/links-in %)]} (group-by-link-and-assoc-tags uuid incoming-links :source)) -(spec/def ::contains-full-information - (spec/keys :req-un [::properties - ::links-out - ::links-in - ::tags])) -(spec/def ::full-instance - (spec/merge ::instance-with-schema - ::contains-full-information)) (defn full-instance-by-uuid! [uuid] - {:post [(spec/assert ::full-instance %)]} + {:post [(spec/assert ::domain-instance/full-instance %)]} (assoc (find-by-uuid! uuid) :properties (find-properties! uuid) diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj new file mode 100644 index 0000000..d0a0fbe --- /dev/null +++ b/src/wanijo/instance/domain.clj @@ -0,0 +1,65 @@ +(ns wanijo.instance.domain + (:require [wanijo.specs :as specs] + [wanijo.attribute.domain :as domain-attr] + [wanijo.schema.db :as db-schema] + [wanijo.tag.db :as db-tag] + [wanijo.link.db :as db-link] + [clojure.spec.alpha :as spec])) + +(spec/def ::instance + (spec/keys :req-un [::specs/uuid + ::specs/created_at + ::specs/updated_at + ::specs/name])) +(spec/def ::properties + (spec/coll-of + (spec/keys :req-un [::specs/uuid + ::specs/created_at + ::specs/updated_at + ::domain-attr/attribute]))) +(spec/def ::target ::instance) +(spec/def ::source ::instance) +(spec/def ::tags + (spec/coll-of ::db-tag/tag)) +(spec/def ::contains-schema + (spec/keys :req-un [::db-schema/schema])) + +(spec/def ::instances-with-tags + (spec/coll-of (spec/merge ::instance + (spec/keys :req-un [::tags])))) + +(spec/def ::instance-with-schema + (spec/merge ::instance ::contains-schema)) + +(spec/def ::prop-tuple-values + (spec/keys :req-un [::specs/uuid + ::specs/now + ::value + ::specs/instance_uuid + ::specs/attribute_uuid])) +(spec/def ::prop-tuple + (spec/coll-of + (spec/tuple fn? ::prop-tuple-values))) + +(spec/def ::links-out + (spec/coll-of + (spec/keys :req-un [::db-link/link + ::target + ::db-schema/schema + ::tags]))) + +(spec/def ::links-in + (spec/coll-of + (spec/keys :req-un [::db-link/link + ::source + ::db-schema/schema + ::tags]))) + +(spec/def ::contains-full-information + (spec/keys :req-un [::properties + ::links-out + ::links-in + ::tags])) +(spec/def ::full-instance + (spec/merge ::instance-with-schema + ::contains-full-information)) diff --git a/src/wanijo/instance/forms.clj b/src/wanijo/instance/forms.clj index 2c27a99..d98fc47 100644 --- a/src/wanijo/instance/forms.clj +++ b/src/wanijo/instance/forms.clj @@ -1,6 +1,5 @@ (ns wanijo.instance.forms - (:require [wanijo.specs :as specs] - [wanijo.instance.db :as domain])) + (:require [wanijo.specs :as specs])) (def form {:fields {:name {:label "Name" diff --git a/src/wanijo/instance/view.clj b/src/wanijo/instance/view.clj index b88a1dc..7a59d8d 100644 --- a/src/wanijo/instance/view.clj +++ b/src/wanijo/instance/view.clj @@ -6,7 +6,6 @@ [markdown.core :as md] [formulare.core :as form] [wanijo.tag.view :as view-tag] - [wanijo.instance.db :as domain] [wanijo.visualisation.viz :as viz] [wanijo.infrastructure [view :as view] diff --git a/src/wanijo/tag/routes.clj b/src/wanijo/tag/routes.clj index 8b83127..64e2733 100644 --- a/src/wanijo/tag/routes.clj +++ b/src/wanijo/tag/routes.clj @@ -6,7 +6,7 @@ [wanijo.schema.db :as domain-schema] [wanijo.instance [view :as view-instance] - [db :as domain-instance]] + [db :as db-instance]] [wanijo.tag [db :as domain] [forms :as forms]])) @@ -22,7 +22,7 @@ (resp/redirect (path :instance-show {:uuid instance-uuid}))) (view-instance/show! - (domain-instance/full-instance-by-uuid! instance-uuid) + (db-instance/full-instance-by-uuid! instance-uuid) (domain-schema/accessible-schemas! user-uuid) req)))) diff --git a/src/wanijo/visualisation/db.clj b/src/wanijo/visualisation/db.clj index c9848f8..514812c 100644 --- a/src/wanijo/visualisation/db.clj +++ b/src/wanijo/visualisation/db.clj @@ -1,7 +1,8 @@ (ns wanijo.visualisation.db (:require [clojure.spec.alpha :as spec] [wanijo.infrastructure.neo4j :as neo4j] - [wanijo.instance.db :as domain-instance])) + [wanijo.instance.db :as db-instance] + [wanijo.instance.domain :as domain-instance])) (spec/def ::link-name (spec/or :not-give nil? @@ -34,7 +35,7 @@ :links-out (fn [links-in] (map #(assoc - (domain-instance/full-instance-by-uuid! + (db-instance/full-instance-by-uuid! (-> % :target :uuid)) :link-name (-> % :label :name)) @@ -42,9 +43,9 @@ (defn search! [term] {:post [(spec/assert (spec/coll-of ::with-enriched-out-links) %)]} (->> (neo4j/exec-query! search {:term (str ".*" term ".*")}) - (map #(domain-instance/full-instance-by-uuid! (-> % :i :uuid))) + (map #(db-instance/full-instance-by-uuid! (-> % :i :uuid))) (map enrich-links))) (defn instance! [uuid] {:post [(spec/assert ::with-enriched-out-links %)]} - (enrich-links (domain-instance/full-instance-by-uuid! uuid))) + (enrich-links (db-instance/full-instance-by-uuid! uuid))) diff --git a/src/wanijo/visualisation/routes.clj b/src/wanijo/visualisation/routes.clj index 053fff7..122e72b 100644 --- a/src/wanijo/visualisation/routes.clj +++ b/src/wanijo/visualisation/routes.clj @@ -1,10 +1,10 @@ (ns wanijo.visualisation.routes (:require [compojure.core :refer [defroutes wrap-routes GET POST DELETE]] - [ring.util.response :as resp] - [wanijo.infrastructure.routing :refer [register! path]] - [wanijo.infrastructure.view :as view] - [wanijo.instance.db :as domain-instance] + [wanijo.infrastructure + [routing :refer [register! path]] + [view :as view]] + [wanijo.instance.db :as db-instance] [wanijo.schema.db :as domain-schema] [wanijo.visualisation [db :as vis-db] @@ -35,7 +35,7 @@ (defroutes routes (GET (register! :vis-explore "/vis/explore/:instance-uuid") [instance-uuid :as req] - (vis-view/index (domain-instance/find-by-uuid! instance-uuid) + (vis-view/index (db-instance/find-by-uuid! instance-uuid) req)) (GET (register! :vis-all-instances "/vis/all-instances") [:as req]