make tag updates wipe non-included tags
Updated via bruno" Updated via bruno",
This commit is contained in:
parent
5fad04d04c
commit
5245c9a2dd
2 changed files with 13 additions and 11 deletions
|
|
@ -17,6 +17,7 @@ params:query {
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"title": "Mock snippet sent via Bruno with updated title",
|
"title": "Mock snippet sent via Bruno with updated title",
|
||||||
"markdown": "## MOCK SNIPPET\nUpdated via bruno"
|
"markdown": "## MOCK SNIPPET\nUpdated via bruno",
|
||||||
|
"tags": ["mock", "updated"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
(ns snippets.infra.db2
|
(ns snippets.infra.db2
|
||||||
(:require
|
(:require
|
||||||
|
[clojure.set :as set]
|
||||||
[datomic.client.api :as d]
|
[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
|
;; Initialize the Datomic Local client
|
||||||
;; :system "dev" groups your databases in the "dev" system
|
;; :system "dev" groups your databases in the "dev" system
|
||||||
|
|
@ -151,19 +152,19 @@
|
||||||
"Update specific fields of a snippet."
|
"Update specific fields of a snippet."
|
||||||
[slug raw-patch]
|
[slug raw-patch]
|
||||||
(let [conn (get-conn)
|
(let [conn (get-conn)
|
||||||
db (d/db conn)
|
snippet (get-snippet-by-slug-from-db slug)
|
||||||
;; query '[:find ?e
|
eid (:db/id snippet)
|
||||||
;; :in $ ?slug
|
new-tags (get raw-patch :tags '[])
|
||||||
;; :where [?e :snippet/slug ?slug]]
|
existing-tags (get snippet :snippet/tags '[])
|
||||||
;; eid (ffirst (d/q query db slug))
|
tags-to-remove (vec (set/difference (set existing-tags) (set new-tags)))
|
||||||
eid (:db/id (get-snippet-by-slug-from-db slug))
|
retracts (map #(vector :db/retract eid :snippet/tags %) tags-to-remove)
|
||||||
patch (merge (to-update raw-patch) {:db/id eid})]
|
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)
|
(when (nil? eid)
|
||||||
(throw (ex-info "Snippet not found" {:slug slug})))
|
(throw (ex-info "Snippet not found" {:slug slug})))
|
||||||
(when-not (m/validate update-schema patch)
|
(when-not (m/validate update-schema patch)
|
||||||
(throw (ex-info "Invalid patch" {:errors (m/explain update-schema patch) :patch 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]
|
(defn update-snippet [& args]
|
||||||
(let [res (apply patch-snippet-in-db args)]
|
(let [res (apply patch-snippet-in-db args)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue