From fc6a6d424df71f2fe7af2e5b66c08f0fae3acaaf Mon Sep 17 00:00:00 2001 From: Manuel Luypaert Date: Thu, 9 Jul 2020 18:24:29 +0100 Subject: [PATCH] Added gene/{id}/update-other-names PUT and DELETE REST API endpoints to add/delete a set of other-names from a gene its other-names #304 --- src/wormbase/names/entity.clj | 21 +++++++++++++++++++++ src/wormbase/names/gene.clj | 26 ++++++++++++++++++++++++++ src/wormbase/specs/gene.clj | 4 +++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/wormbase/names/entity.clj b/src/wormbase/names/entity.clj index aa230afb..fa4f4e8a 100644 --- a/src/wormbase/names/entity.clj +++ b/src/wormbase/names/entity.clj @@ -195,6 +195,27 @@ (not-found (format "%s '%s' does not exist" ent-ns (last lur))))))))))) +(defn update-multi-card + [identify-fn uiident dt-transact-fn attr] + (fn handle-multi-card [request identifier] + (let [{conn :conn payload :body-params} request + ent-ns (namespace uiident) + data (some-> payload :data) + [lur entity] (identify-fn request identifier)] + (when (and entity data) + (let [build-tx (fn [id attr ^String val] (vec [dt-transact-fn id attr val])) + ;;retriev db-id to add values to + db-id (:db/id entity) + ;;build transaction statements to make + txes (map #(build-tx db-id attr %) data) + ;;transact data to datomic + tx-result @(d/transact-async conn txes)] + (when-let [db-after (:db-after tx-result)] + (if-let [updated (wdb/pull db-after [attr uiident] lur)] + (ok (or (attr updated) [])) + (not-found + (format "%s '%s' does not exist" ent-ns (last lur)))))))))) + (defn status-changer "Return a handler to change the status of an entity to `to-status`. diff --git a/src/wormbase/names/gene.clj b/src/wormbase/names/gene.clj index 50640841..c116fb07 100644 --- a/src/wormbase/names/gene.clj +++ b/src/wormbase/names/gene.clj @@ -443,6 +443,32 @@ (nil? (:cgc-name data))) (bad-request! {:message "CGC name cannot be removed from an uncloned gene."})) (update-gene request identifier)))}}) + (sweet/context "/update-other-names" [] + (sweet/resource + {:put + {:summary "Add a set of other-names to a gene." + :x-name ::add-other-names-gene + :parameters {:body-params {:data ::wsg/update-other-names + :prov ::wsp/provenance}} + :responses (-> wnu/default-responses + (dissoc conflict) + (assoc bad-request {:schema ::wsv/error-response}) + (wnu/response-map)) + :handler (fn [request] + (let [add-other-names (wne/update-multi-card identify :gene/id :db/add ':gene/other-names)] + (add-other-names request identifier)))} + :delete + {:summary "Delete a set of other-names from a gene." + :x-name ::delete-other-names-gene + :parameters {:body-params {:data ::wsg/update-other-names + :prov ::wsp/provenance}} + :responses (-> wnu/default-responses + (dissoc conflict) + (assoc bad-request {:schema ::wsv/error-response}) + (wnu/response-map)) + :handler (fn [request] + (let [delete-other-names (wne/update-multi-card identify :gene/id :db/retract ':gene/other-names)] + (delete-other-names request identifier)))}})) (sweet/context "/merge/:from-identifier" [from-identifier] (sweet/resource {:post diff --git a/src/wormbase/specs/gene.clj b/src/wormbase/specs/gene.clj index 00f82553..7776a834 100644 --- a/src/wormbase/specs/gene.clj +++ b/src/wormbase/specs/gene.clj @@ -85,7 +85,6 @@ (s/def ::update (stc/spec {:spec (s/and (s/keys :opt-un [:gene/cgc-name :gene/sequence-name - :gene/other-names :gene/biotype :gene/species]) seq) @@ -94,6 +93,9 @@ (s/def ::updated (stc/spec {:spec (s/keys :req-un [:gene/id]) :description "The response data from updating a Gene."})) +(s/def ::update-other-names (stc/spec {:spec :gene/other-names + :description "A collection of other names."})) + (s/def ::merge (stc/spec {:spec (s/keys :req-un [:gene/biotype]) :description "The data requried to update a Gene."}))