make tag updates wipe non-included tags

Updated via bruno"
Updated via bruno",
This commit is contained in:
Travis Shears 2026-03-10 17:56:41 +01:00
parent 5fad04d04c
commit 5245c9a2dd
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
2 changed files with 13 additions and 11 deletions

View file

@ -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"]
}
}

View file

@ -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)]