 
 (alias 'core 'clojure.core)
 
-(hugsql/def-db-fns "akvo/lumen/lib/collection.sql")
-
 (defn all
   ([tenant-conn]
    (all tenant-conn nil))
   ([tenant-conn ids]
    (mapv (fn [collection]
            (core/update collection :entities #(vec (.getArray %))))
-         (all-collections tenant-conn (if ids {:ids ids} {})))))
+         (db.collection/all-collections tenant-conn (if ids {:ids ids} {})))))
 
 (defn fetch [tenant-conn id]
-  (if-let [collection (fetch-collection tenant-conn {:id id})]
+  (if-let [collection (db.collection/fetch-collection tenant-conn {:id id})]
     (lib/ok (core/update collection :entities #(vec (.getArray %))))
     (lib/not-found {:id id})))
 

  (let [{:keys [entities title]} collection]
    (jdbc/with-db-transaction [tx-conn tenant-conn]
      (when title
        (update-collection-title tx-conn {:id id :title title}))

      ;; Replace entity ids
      (when entities
        (delete-collection-entities tx-conn {:id id})
        (doseq [entity (categorize-entities tx-conn entities)]
          (insert-collection-entity tx-conn (assoc entity :collection-id id))))

      (fetch tx-conn id))))

(defn delete
  "Delete a collection by id"
  [tenant-conn id]
  (delete-collection tenant-conn {:id id})
  (lib/no-content))

  [conn entities]
  (let [entity-array (text-array conn entities)]
    (->> (concat
           (db.collection/fetch-dataset-ids conn {:ids entity-array})
           (db.collection/fetch-visualisation-ids conn {:ids entity-array})
           (db.collection/fetch-dashboard-ids conn {:ids entity-array})
           (db.collection/fetch-raster-dataset-ids conn {:ids entity-array}))
      (map #(merge {:dataset-id nil :visualisation-id nil :dashboard-id nil
                    :raster-dataset-id nil} %)))))

