diff --git a/src/wanijo/instance/db.clj b/src/wanijo/instance/db.clj index 9a753e4..5b55080 100644 --- a/src/wanijo/instance/db.clj +++ b/src/wanijo/instance/db.clj @@ -2,7 +2,8 @@ (:require [clojure.spec.alpha :as spec] [wanijo.infra.neo4j :as neo4j] [wanijo.instance.domain :as domain-instance] - [wanijo.tag.db :as db-tag])) + [wanijo.tag.db :as db-tag] + [wanijo.instance.revision.db :as db-rev])) (neo4j/defquery findy-by-schema "MATCH (i:instance)-[:of]->(s:schema) @@ -125,14 +126,23 @@ :instance_uuid (:uuid instance) :attribute_uuid (-> prop :attribute :uuid)}])) (:properties instance))) -(defn edit! [instance] +(defn edit! [instance revision] (let [prop-tuples (instance->prop-tuples instance)] (apply neo4j/exec-queries! (concat [[edit-instance {:uuid (:uuid instance) :name (:name instance) :updated_at (neo4j/now-str)}]] - prop-tuples)))) + prop-tuples + (db-rev/revision-queries revision))))) + +(comment + (db-rev/revision-queries + {:instance-name "dings" + :instance-uuid "4711" + :properties [{:type "string" + :value "dings-prop"}]}) + ) (neo4j/defquery delete "MATCH (i:instance {uuid: $uuid}), @@ -242,3 +252,4 @@ :starred_at (-> % :s :created_at)) (neo4j/exec-query! starred-by-user {:user_uuid user-uuid}))) + diff --git a/src/wanijo/instance/domain.clj b/src/wanijo/instance/domain.clj index 65641c9..4facae5 100644 --- a/src/wanijo/instance/domain.clj +++ b/src/wanijo/instance/domain.clj @@ -66,3 +66,12 @@ (spec/def ::full-instance (spec/merge ::instance-with-schema ::contains-full-information)) + +(defn to-revision [instance] + {:instance-name (:name instance) + :instance-uuid (:uuid instance) + :properties + (map (fn [{:keys [attribute value]}] + {:value value + :type (:type attribute)}) + (:properties instance))}) diff --git a/src/wanijo/instance/revision/db.clj b/src/wanijo/instance/revision/db.clj new file mode 100644 index 0000000..dd4b119 --- /dev/null +++ b/src/wanijo/instance/revision/db.clj @@ -0,0 +1,33 @@ +(ns wanijo.instance.revision.db + (:require [wanijo.infra.neo4j :as neo4j])) + +(neo4j/defquery save-revision + "MATCH (i:instance) + WHERE i.uuid = $uuid + CREATE (r:revision)-[:of]->(i) + SET r.uuid = $rev_uuid, + r.instance_name = $name, + r.created_at = $now") +(neo4j/defquery save-revision-property + "MATCH (r:revision) + WHERE r.uuid = $rev_uuid + CREATE (rp:rev_property)-[:of]->(r) + SET rp.uuid = $rp_uuid, + rp.value = $value, + rp.type = $type, + rp.created_at = $now") +(defn revision-queries [revision] + (let [rev-uuid (neo4j/uuid)] + (concat [[save-revision + {:uuid (:instance-uuid revision) + :rev_uuid rev-uuid + :name (:instance-name revision) + :now (neo4j/now-str)}]] + (map (fn [{:keys [value type]}] + [save-revision-property + {:rev_uuid rev-uuid + :rp_uuid (neo4j/uuid) + :value value + :type type + :now (neo4j/now-str)}]) + (:properties revision))))) diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index 9033a89..7ff2bb1 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -11,8 +11,10 @@ [link-selection :refer [link-selection]] [bulk-link-selection :refer [bulk-link-selection]] [starred :refer [starred]]] - [wanijo.instance.db :as domain] - [wanijo.instance.forms :as forms-inst] + [wanijo.instance + [db :as db] + [forms :as forms-inst] + [domain :as domain]] [wanijo.schema.db :as domain-schema] [wanijo.schema.middleware :as middleware-schema] [wanijo.link.db :as domain-link] @@ -21,7 +23,7 @@ (defn route-list! [schema-uuid req] (instances (domain-schema/find-by-uuid! schema-uuid) - (domain/find-by-schema! schema-uuid) + (db/find-by-schema! schema-uuid) (forms-inst/with-attributes (db-attr/required! schema-uuid)) req)) @@ -35,7 +37,7 @@ req-attrs (db-attr/required! schema-uuid) instance (forms-inst/form-data->instance form-data req-attrs)] - (domain/create! user-uuid + (db/create! user-uuid schema-uuid instance) (resp/redirect (path :instance-list @@ -43,13 +45,13 @@ (route-list! schema-uuid req)))) (defn instance! [uuid] - (domain/full-instance-by-uuid! uuid)) + (db/full-instance-by-uuid! uuid)) (defn route-show! [uuid req] (let [user-uuid (-> req :session :uuid) instance (assoc (instance! uuid) :starred - (domain/is-starred! uuid + (db/is-starred! uuid user-uuid))] (show instance (domain-schema/accessible-schemas! user-uuid) @@ -75,20 +77,19 @@ (let [form-data (form/form-data form-def req) form-instance (forms-inst/form-data->instance form-data attrs) instance (assoc form-instance :uuid uuid)] - (clojure.pprint/pprint instance) - (domain/edit! instance) + (db/edit! instance (domain/to-revision instance)) (resp/redirect (path :instance-show instance))) (route-show! uuid req)))) (defn route-delete! [uuid] (let [schema (domain-schema/find-by-instance! uuid)] - (domain/delete! uuid) + (db/delete! uuid) (resp/redirect (path :instance-list {:schema-uuid (:uuid schema)})))) (defn link-form! [schema-uuid] (forms-inst/link-form - (domain/find-by-schema! schema-uuid))) + (db/find-by-schema! schema-uuid))) (defn route-link-selection! [uuid schema-uuid req] (link-selection (instance! uuid) @@ -117,28 +118,28 @@ (resp/redirect (path :instance-edit-form {:uuid uuid}))) (defn route-mark-starred! [uuid req] - (domain/mark-starred! uuid + (db/mark-starred! uuid (-> req :session :uuid)) (resp/redirect (path :instance-show {:uuid uuid}))) (defn route-remove-starred! [uuid req] - (domain/remove-starred! uuid + (db/remove-starred! uuid (-> req :session :uuid)) (resp/redirect (path :instance-show {:uuid uuid}))) (defn route-list-starred! [req] (starred - (domain/starred-by-user! (-> req :session :uuid)) + (db/starred-by-user! (-> req :session :uuid)) req)) (defn route-bulk-link-selection! [uuid req] (let [user-uuid (-> req :session :uuid)] - (bulk-link-selection (domain/full-instance-by-uuid! uuid) + (bulk-link-selection (db/full-instance-by-uuid! uuid) (->> (domain-schema/accessible-schemas! user-uuid) (map (fn [schema] {:schema schema - :instances (domain/find-by-schema! + :instances (db/find-by-schema! (:uuid schema))}))) req)))