diff --git a/bruno/CodeSnippets/edit_snippet.bru b/bruno/CodeSnippets/edit_snippet.bru index 69fc83c..f42fb44 100644 --- a/bruno/CodeSnippets/edit_snippet.bru +++ b/bruno/CodeSnippets/edit_snippet.bru @@ -17,6 +17,7 @@ params:query { body:json { { "title": "Mock snippet sent via Bruno with updated title", - "markdown": "## MOCK SNIPPET\nUpdated via bruno" + "markdown": "## MOCK SNIPPET\nUpdated via bruno", + "tags": ["mock", "updated"] } } diff --git a/src/snippets/infra/db2.clj b/src/snippets/infra/db2.clj index 9cbf100..134f93a 100644 --- a/src/snippets/infra/db2.clj +++ b/src/snippets/infra/db2.clj @@ -1,8 +1,9 @@ (ns snippets.infra.db2 (:require + [clojure.set :as set] [datomic.client.api :as d] - [taoensso.telemere :as t] - [malli.core :as m])) + [malli.core :as m] + [taoensso.telemere :as t])) ;; Initialize the Datomic Local client ;; :system "dev" groups your databases in the "dev" system @@ -151,19 +152,19 @@ "Update specific fields of a snippet." [slug raw-patch] (let [conn (get-conn) - db (d/db conn) - ;; query '[:find ?e - ;; :in $ ?slug - ;; :where [?e :snippet/slug ?slug]] - ;; eid (ffirst (d/q query db slug)) - eid (:db/id (get-snippet-by-slug-from-db slug)) + snippet (get-snippet-by-slug-from-db slug) + eid (:db/id snippet) + new-tags (get raw-patch :tags '[]) + existing-tags (get snippet :snippet/tags '[]) + tags-to-remove (vec (set/difference (set existing-tags) (set new-tags))) + retracts (map #(vector :db/retract eid :snippet/tags %) tags-to-remove) patch (merge (to-update raw-patch) {:db/id eid})] - (t/log! {:level :info, :data {:patch patch :slug slug :eid eid}} "Patching snippet") + (t/log! {:level :info, :data {:patch patch :retracts retracts :slug slug :eid eid}} "Patching snippet") (when (nil? eid) (throw (ex-info "Snippet not found" {:slug slug}))) (when-not (m/validate update-schema patch) (throw (ex-info "Invalid patch" {:errors (m/explain update-schema patch) :patch patch}))) - (d/transact conn {:tx-data [patch]}))) + (d/transact conn {:tx-data (into [patch] retracts)}))) (defn update-snippet [& args] (let [res (apply patch-snippet-in-db args)]