<commit_before>(ns akvo.lumen.endpoint.collection
  (:require [akvo.lumen.protocols :as p]
            [akvo.lumen.lib.collection :as collection]
            [clojure.spec.alpha :as s]
            [akvo.lumen.lib :as lib]
            [akvo.lumen.component.tenant-manager :as tenant-manager]
            [clojure.tools.logging :as log]
            [akvo.lumen.lib.collection :as collection]
            [clojure.walk :refer (stringify-keys)]
            [integrant.core :as ig]))

(defn all [auth-service tenant-conn]
  (let [collections (collection/all tenant-conn (:auth-collections-set auth-service))]
    (log/error :collections collections)
    (lib/ok collections)))

(defn routes [{:keys [tenant-manager] :as opts}]
  ["/collections"
   ["" {:get {:handler (fn [{tenant :tenant
                             auth-service :auth-service}]
                       (all auth-service (p/connection tenant-manager tenant)))}
        :post {:responses {200 {}}
             :parameters {:body map?}
             :handler (fn [{tenant :tenant
                            body :body}]
                        (collection/create (p/connection tenant-manager tenant) (stringify-keys body)))}}]
   ["/:id" {:get {:responses {200 {}}
                  :parameters {:path-params {:id string?}}
                  :handler (fn [{tenant :tenant
                                 {:keys [id]} :path-params}]
                             (collection/fetch (p/connection tenant-manager tenant) id))}
            :put {:responses {200 {}}
                  :parameters {:body map?
                               :path-params {:id string?}}
                  :handler (fn [{tenant :tenant
                                 body :body
                                 {:keys [id]} :path-params}]
                             (collection/update (p/connection tenant-manager tenant) id body))}
            :delete {:parameters {:path-params {:id string?}}
                         :handler (fn [{tenant :tenant
                                        {:keys [id]} :path-params}]
                                    (collection/delete (p/connection tenant-manager tenant) id))}}]])

(defmethod ig/init-key :akvo.lumen.endpoint.collection/collection  [_ opts]
  (routes opts))

(defmethod ig/pre-init-spec :akvo.lumen.endpoint.collection/collection [_]
  (s/keys :req-un [::tenant-manager/tenant-manager]))
<commit_msg>Refactor<commit_after>(ns akvo.lumen.endpoint.collection
  (:require [akvo.lumen.protocols :as p]
            [akvo.lumen.lib.collection :as collection]
            [clojure.spec.alpha :as s]
            [akvo.lumen.lib :as lib]
            [akvo.lumen.component.tenant-manager :as tenant-manager]
            [clojure.tools.logging :as log]
            [akvo.lumen.lib.collection :as collection]
            [clojure.walk :refer (stringify-keys)]
            [integrant.core :as ig]))

(defn all [auth-service tenant-conn]
  (lib/ok (collection/all tenant-conn (:auth-collections-set auth-service))))

(defn routes [{:keys [tenant-manager] :as opts}]
  ["/collections"
   ["" {:get {:handler (fn [{tenant :tenant
                             auth-service :auth-service}]
                         (all auth-service (p/connection tenant-manager tenant)))}
        :post {:responses {200 {}}
             :parameters {:body map?}
             :handler (fn [{tenant :tenant
                            body :body}]
                        (collection/create (p/connection tenant-manager tenant) (stringify-keys body)))}}]
   ["/:id" {:get {:responses {200 {}}
                  :parameters {:path-params {:id string?}}
                  :handler (fn [{tenant :tenant
                                 {:keys [id]} :path-params}]
                             (collection/fetch (p/connection tenant-manager tenant) id))}
            :put {:responses {200 {}}
                  :parameters {:body map?
                               :path-params {:id string?}}
                  :handler (fn [{tenant :tenant
                                 body :body
                                 {:keys [id]} :path-params}]
                             (collection/update (p/connection tenant-manager tenant) id body))}
            :delete {:parameters {:path-params {:id string?}}
                         :handler (fn [{tenant :tenant
                                        {:keys [id]} :path-params}]
                                    (collection/delete (p/connection tenant-manager tenant) id))}}]])

(defmethod ig/init-key :akvo.lumen.endpoint.collection/collection  [_ opts]
  (routes opts))

(defmethod ig/pre-init-spec :akvo.lumen.endpoint.collection/collection [_]
  (s/keys :req-un [::tenant-manager/tenant-manager]))

{'fraction_non_alphanumeric': 0.13593882752761258, 'fraction_numerical': 0.0038232795242141037, 'mean_word_length': 1.6695011337868482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4324109', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1272, 'n_words': 304}