<commit_before>(ns clj-money.api.entities
  (:refer-clojure :exclude [update])
  (:require [clojure.pprint :refer [pprint]]
            [ring.util.response :refer [status response]]
            [cemerick.friend :refer [current-authentication]]
            [environ.core :refer [env]]
            [clj-money.validation :as validation]
            [clj-money.authorization :refer [authorize]]
            [clj-money.models.entities :as entities]))

(defn index
  [req]
  (response (entities/select (env :db) (:id (current-authentication)))))

(defn update
  [{:keys [params]}]
  (let [entity (authorize :edit (:entity params))]
    (try
      (entities/update (env :db) entity)
      (-> []
          response
          (status 204))

      (catch Exception e
        (->  (if (env :show-error-messages?)
               (.getMessage e)
               "Unable to save the entity.")
            response
            (status 500))))))

(defn delete
  [{{id :id} :params}]
  (entities/delete (env :db) id)
  {:status 204
   :body []})
<commit_msg>refine entities api logic<commit_after>(ns clj-money.api.entities
  (:refer-clojure :exclude [update])
  (:require [clojure.pprint :refer [pprint]]
            [ring.util.response :refer [status response header]]
            [cemerick.friend :refer [current-authentication]]
            [environ.core :refer [env]]
            [clj-money.validation :as validation]
            [clj-money.authorization :refer [authorize]]
            [clj-money.models.entities :as entities]
            [clj-money.permissions.entities]))

(defn index
  [req]
  (response (entities/select (env :db) (:id (current-authentication)))))

(defn update
  [{{id :id} :params}]
  (let [entity (authorize :update (entities/find-by-id (env :db) id))]
    (try
      (entities/update (env :db) entity)
      (-> []
          response
          (status 204))
      (catch Exception e
        (->  (if (env :show-error-messages?)
               (.getMessage e)
               "Unable to save the entity.")
            response
            (header "Content-Type" "application/json")
            (status 500))))))

(defn delete
  [{{id :id} :params}]
  (let [entity (authorize :delete (entities/find-by-id (env :db) id))]
    (try
    (entities/delete (env :db) (:id entity))
    (catch Exception e
      (-> (if (env :show-error-messages?)
            (.getMessage e)
            "Unable to delete the entity.")
          response
          (header "Content-Type" "application/json")
          (status 500))))
    {:status 204
     :body []}))

{'fraction_non_alphanumeric': 0.1515270164447925, 'fraction_numerical': 0.00822239624119029, 'mean_word_length': 2.230088495575221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804484', 'n_tokens_mistral': 820, 'n_tokens_neox': 805, 'n_words': 206}