<commit_before>(ns clj-money.models.attachments
  (:refer-clojure :exclude [update])
  (:require [clojure.pprint :refer [pprint]]
            [clojure.spec :as s]
            [clj-money.validation :as validation]
            [clj-money.authorization :as authorization]
            [clj-money.coercion :as coercion]
            [clj-money.models.helpers :refer [with-storage
                                              with-transacted-storage
                                              create-fn
                                              update-fn]]
            [clj-money.models.storage :refer [create-attachment
                                              select-attachments
                                              delete-attachment]]
            [clj-money.models.images :as images]
            [clj-money.models.transactions :as transactions]
            [clj-money.models.auth-helpers :refer [user-owns-entity?
                                                   user-entity-ids]]))

(s/def ::transaction-id integer?)
(s/def ::image-id integer?)
(s/def ::caption string?)
(s/def ::new-attachment (s/keys :req-un [::transaction-id
                                         ::caption
                                         ::image-id]))

(defn- after-read
  ([attachment]
   (after-read nil attachment))
  ([_ attachment]
   (when attachment
     (authorization/tag-resource attachment :attachment))))

(def create
  (create-fn {:create create-attachment
              :after-read after-read
              :spec ::new-attachment}))

(defn search
  ([storage-spec criteria]
   (search storage-spec criteria {}))
  ([storage-spec criteria options]
   (with-storage [s storage-spec]
     (->> criteria
          (select-attachments s )
          after-read))))

(defn find-by-id
  [storage-spec id]
  (->> (search storage-spec {:id id} {:limit 1})
       first
       after-read))

(defn delete
  [storage-spec id-or-attachment]
  (with-transacted-storage [s storage-spec]
    (let [attachment (if (integer? id-or-attachment)<commit_msg>correct function invokation<commit_after>(ns clj-money.models.attachments
  (:refer-clojure :exclude [update])
  (:require [clojure.pprint :refer [pprint]]
            [clojure.spec :as s]
            [clj-money.validation :as validation]
            [clj-money.authorization :as authorization]
            [clj-money.coercion :as coercion]
            [clj-money.models.helpers :refer [with-storage
                                              with-transacted-storage
                                              create-fn
                                              update-fn]]
            [clj-money.models.storage :refer [create-attachment
                                              select-attachments
                                              delete-attachment]]
            [clj-money.models.images :as images]
            [clj-money.models.transactions :as transactions]
            [clj-money.models.auth-helpers :refer [user-owns-entity?
                                                   user-entity-ids]]))

(s/def ::transaction-id integer?)
(s/def ::image-id integer?)
(s/def ::caption string?)
(s/def ::new-attachment (s/keys :req-un [::transaction-id
                                         ::caption
                                         ::image-id]))

(defn- after-read
  ([attachment]
   (after-read nil attachment))
  ([_ attachment]
   (when attachment
     (authorization/tag-resource attachment :attachment))))

(def create
  (create-fn {:create create-attachment
              :after-read after-read
              :spec ::new-attachment}))

(defn search
  ([storage-spec criteria]
   (search storage-spec criteria {}))
  ([storage-spec criteria options]
   (with-storage [s storage-spec]
     (->> criteria
          (select-attachments s )
          (map after-read)))))

(defn find-by-id
  [storage-spec id]
  (->> (search storage-spec {:id id} {:limit 1})
       first
       after-read))

(defn delete
  [storage-spec id-or-attachment]
  (with-transacted-storage [s storage-spec]
    (let [attachment (if (integer? id-or-attachment)
{'fraction_non_alphanumeric': 0.11972855065438681, 'fraction_numerical': 0.0004847309743092584, 'mean_word_length': 1.5923366834170853, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3294949', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1082, 'n_words': 242}