Repository: CSCfi/rems
Pull request #1490 (2019-08-14 11:54:58)

Add rems.api.services.resources, providing API with resources


Patch:
diff --git a/src/clj/rems/api/resources.clj b/src/clj/rems/api/resources.clj
index 7ac2f9660..ac4cb1ede 100644
--- a/src/clj/rems/api/resources.clj
+++ b/src/clj/rems/api/resources.clj
@@ -2,8 +2,8 @@
   (:require [compojure.api.sweet :refer :all]
             [rems.api.schema :refer :all]
             [rems.api.util] ; required for route :roles
+            [rems.api.services.resource :as resource]
             [rems.db.licenses :as licenses]
-            [rems.db.resource :as resource]
             [rems.util :refer [getx-user-id]]
             [ring.util.http-response :refer :all]
             [schema.core :as s])
@@ -36,31 +36,6 @@
    (s/optional-key :id) s/Int
    (s/optional-key :errors) [s/Any]})
 
-(defn- format-resource
-  [{:keys [id owneruserid modifieruserid organization resid start end expired enabled archived]}]
-  {:id id
-   :owneruserid owneruserid
-   :modifieruserid modifieruserid
-   :organization organization
-   :resid resid
-   :start start
-   :end end
-   :expired expired
-   :enabled enabled
-   :archived archived})
-
-(defn- get-resources [filters]
-  (doall
-   (for [res (resource/get-resources filters)]
-     (assoc (format-resource res)
-            :licenses (licenses/get-resource-licenses (:id res))))))
-
-(defn- get-resource [id]
-  (-> id
-      resource/get-resource
-      format-resource
-      (assoc :licenses (licenses/get-resource-licenses id))))
-
 (def resources-api
   (context "/resources" []
     :tags ["resources"]
@@ -72,16 +47,16 @@
                      {expired :- (describe s/Bool "whether to include expired resources") false}
                      {archived :- (describe s/Bool "whether to include archived resources") false}]
       :return Resources
-      (ok (get-resources (merge (when-not expired {:expired false})
-                                (when-not disabled {:enabled true})
-                                (when-not archived {:archived false})))))
+      (ok (resource/get-resources (merge (when-not expired {:expired false})
+                                         (when-not disabled {:enabled true})
+                                         (when-not archived {:archived false})))))
 
     (GET "/:resource-id" []
       :summary "Get resource by id"
       :roles #{:owner}
       :path-params [resource-id :- (describe s/Int "resource id")]
       :return Resource
-      (ok (get-resource resource-id)))
+      (ok (resource/get-resource resource-id)))
 
     (POST "/create" []
       :summary "Create resource"
diff --git a/src/clj/rems/api/services/resource.clj b/src/clj/rems/api/services/resource.clj
new file mode 100644
index 000000000..32554db38
--- /dev/null
+++ b/src/clj/rems/api/services/resource.clj
@@ -0,0 +1,42 @@
+(ns rems.api.services.resource
+  (:require [rems.db.catalogue :as catalogue]
+            [rems.db.core :as db]
+            [rems.db.resource :as resource])
+  (:import (org.postgresql.util PSQLException)))
+
+(defn- duplicate-resid? [^Exception e]
+  (let [cause (.getCause e)]
+    (and (instance? PSQLException cause)
+         (.getServerErrorMessage ^PSQLException cause)
+         (= "duplicate key value violates unique constraint \"resource_resid_u\""
+            (.getMessage (.getServerErrorMessage ^PSQLException cause))))))
+
+(defn create-resource! [{:keys [resid organization licenses]} user-id]
+  (try
+    (let [id (:id (db/create-resource! {:resid resid
+                                        :organization organization
+                                        :owneruserid user-id
+                                        :modifieruserid user-id}))]
+      (doseq [licid licenses]
+        (db/create-resource-license! {:resid id
+                                      :licid licid}))
+      {:success true
+       :id id})
+    (catch Exception e
+      (if (duplicate-resid? e)
+        {:success false
+         :errors [{:type :t.administration.errors/duplicate-resid :resid resid}]}
+        (throw e)))))
+
+(defn update-resource! [command]
+  (let [catalogue-items (->> (catalogue/get-localized-catalogue-items {:resource-id (:id command) :archived false})
+                             (map #(select-keys % [:id :title :localizations])))]
+    (if (and (:archived command) (seq catalogue-items))
+      {:success false
+       :errors [{:type :t.administration.errors/resource-in-use :catalogue-items catalogue-items}]}
+      (do
+        (db/set-resource-state! (select-keys command [:id :enabled :archived]))
+        {:success true}))))
+
+(defn get-resource [id] (resource/get-resource id))
+(defn get-resources [filters] (resource/get-resources filters))
diff --git a/src/clj/rems/db/resource.clj b/src/clj/rems/db/resource.clj
index a5ba77c13..7abaf4d38 100644
--- a/src/clj/rems/db/resource.clj
+++ b/src/clj/rems/db/resource.clj
@@ -1,48 +1,32 @@
 (ns rems.db.resource
   (:require [rems.db.catalogue :as catalogue]
-            [rems.db.core :as db])
+            [rems.db.core :as db]
+            [rems.db.licenses :as licenses])
   (:import (org.postgresql.util PSQLException)))
 
+(defn- format-resource
+  [{:keys [id owneruserid modifieruserid organization resid start end expired enabled archived]}]
+  {:id id
+   :owneruserid owneruserid
+   :modifieruserid modifieruserid
+   :organization organization
+   :resid resid
+   :start start
+   :end end
+   :expired expired
+   :enabled enabled
+   :archived archived})
+
 (defn get-resource [id]
   (-> {:id id}
       db/get-resource
-      db/assoc-expired))
+      db/assoc-expired
+      format-resource
+      (assoc :licenses (licenses/get-resource-licenses id))))
 
 (defn get-resources [filters]
   (->> (db/get-resources)
        (map db/assoc-expired)
-       (db/apply-filters filters)))
-
-(defn duplicate-resid? [^Exception e]
-  (let [cause (.getCause e)]
-    (and (instance? PSQLException cause)
-         (.getServerErrorMessage ^PSQLException cause)
-         (= "duplicate key value violates unique constraint \"resource_resid_u\""
-            (.getMessage (.getServerErrorMessage ^PSQLException cause))))))
-
-(defn create-resource! [{:keys [resid organization licenses]} user-id]
-  (try
-    (let [id (:id (db/create-resource! {:resid resid
-                                        :organization organization
-                                        :owneruserid user-id
-                                        :modifieruserid user-id}))]
-      (doseq [licid licenses]
-        (db/create-resource-license! {:resid id
-                                      :licid licid}))
-      {:success true
-       :id id})
-    (catch Exception e
-      (if (duplicate-resid? e)
-        {:success false
-         :errors [{:type :t.administration.errors/duplicate-resid :resid resid}]}
-        (throw e)))))
-
-(defn update-resource! [command]
-  (let [catalogue-items (->> (catalogue/get-localized-catalogue-items {:resource-id (:id command) :archived false})
-                             (map #(select-keys % [:id :title :localizations])))]
-    (if (and (:archived command) (seq catalogue-items))
-      {:success false
-       :errors [{:type :t.administration.errors/resource-in-use :catalogue-items catalogue-items}]}
-      (do
-        (db/set-resource-state! (select-keys command [:id :enabled :archived]))
-        {:success true}))))
+       (db/apply-filters filters)
+       (map format-resource)
+       (map #(assoc % :licenses (licenses/get-resource-licenses (:id %))))))


Test patch:
diff --git a/src/clj/rems/db/test_data.clj b/src/clj/rems/db/test_data.clj
index 60d40847a..8976e511e 100644
--- a/src/clj/rems/db/test_data.clj
+++ b/src/clj/rems/db/test_data.clj
@@ -2,11 +2,11 @@
   "Populating the database with nice test data."
   (:require [clj-time.core :as time]
             [rems.api.services.licenses :as licenses]
+            [rems.api.services.resource :as resource]
             [rems.db.applications :as applications]
             [rems.db.catalogue :as catalogue]
             [rems.db.core :as db]
             [rems.db.form :as form]
-            [rems.db.resource :as resource]
             [rems.db.roles :as roles]
             [rems.db.users :as users]
             [rems.db.workflow :as workflow]
diff --git a/test/clj/rems/db/test_resources.clj b/test/clj/rems/api/services/test_resources.clj
similarity index 95%
rename from test/clj/rems/db/test_resources.clj
rename to test/clj/rems/api/services/test_resources.clj
index b346df198..96094c6d0 100644
--- a/test/clj/rems/db/test_resources.clj
+++ b/test/clj/rems/api/services/test_resources.clj
@@ -1,6 +1,6 @@
-(ns ^:integration rems.db.test-resources
+(ns ^:integration rems.api.services.test-resources
   (:require [clojure.test :refer :all]
-            [rems.db.resource :as resources]
+            [rems.api.services.resource :as resources]
             [rems.db.test-data :as test-data]
             [rems.db.testing :refer [rollback-db-fixture test-db-fixture]]))