LCOV - code coverage report
Current view: top level - matchscore/domain/class_template/repository - postgres.clj Coverage Total Hit
Test: lcov.info Lines: 87.8 % 90 79
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.class-template.repository.postgres
       2              :   (:require
       3              :    [cats.monad.either :as either]
       4              :    [honey.sql :as sql]
       5              :    [matchscore.domain.class-template.entity :as entity]
       6              :    [matchscore.utils.audit :as audit]
       7              :    [matchscore.utils.db :as utils.db]
       8              :    [matchscore.utils.errors :as utils.errors]
       9              :    [matchscore.utils.pagination :as qh]
      10              :    [next.jdbc :as jdbc]))
      11              : 
      12              : 
      13            4 : (defprotocol
      14              :   ClassTemplatePostgresRepository
      15              :   (find-by-id
      16              :    [this id]
      17              :    "Find class template by UUID string. Returns ClassTemplate map or nil.")
      18              :   (find-by-template-id
      19              :    [this template-id]
      20              :    "Find class templates by event template UUID. Returns a vector of ClassTemplate maps.")
      21              :   (find-by-template-ids
      22              :    [this template-ids]
      23              :    "Find class templates for multiple event template UUIDs in one query. Returns a vector of ClassTemplate maps.")
      24              :   (search
      25              :    [this filters pagination]
      26              :    "Search class templates by filters and pagination.
      27              :       filters: {:name {:eq \"...\"} :eventTemplateId {:eq \"uuid\"}}
      28              :       pagination: {:limit 20 :cursor \"base64...\" :sort-by :name :sort-dir :asc}
      29              :       Returns {:items [...] :has-next-page bool :end-cursor \"base64-or-nil\"}")
      30              :   (save!
      31              :    [this class-template]
      32              :    "Persists a class template (insert or update). Returns Either<Error, ClassTemplate>.")
      33              :   (delete!
      34              :    [this id]
      35              :    "Deletes a class template by UUID. Returns Either<Error, nil>."))
      36              : 
      37              : 
      38            4 : (defn- row->class-template
      39              :   "Converts a database row to a domain ClassTemplate map."
      40              :   [row]
      41           32 :   (when row (entity/make-class-template (utils.db/unqualify-keys row))))
      42              : 
      43              : 
      44            4 : (defn- filters->specs
      45              :   [{:keys [name eventTemplateId templateId]}]
      46            4 :   (qh/typed-filters->specs
      47            8 :    {:name        name
      48           16 :     :template-id (utils.db/cast-uuid-filter (or eventTemplateId templateId))}))
      49              : 
      50              : 
      51           12 : (def ^:private valid-sort-fields #{:name})
      52              : 
      53              : 
      54            8 : (def ^:private default-sort :name)
      55              : 
      56              : 
      57            4 : (def ^:private base-query
      58           20 :   {:select [:*]
      59            8 :    :from   [:template_classes]})
      60              : 
      61              : 
      62            4 : (defn- class-template->row
      63              :   "Converts a domain ClassTemplate map to a database row for upsert."
      64              :   [class-template]
      65            8 :   (let [{:keys [uuid public-id template-id name max-players created-by]} class-template]
      66           44 :     {:uuid        [:cast uuid :uuid]
      67            4 :      :public_id   public-id
      68           16 :      :template_id [:cast template-id :uuid]
      69            4 :      :name        name
      70            4 :      :max_players max-players
      71           16 :      :created_by  [:cast created-by :uuid]}))
      72              : 
      73              : 
      74            4 : (defrecord
      75              :   PostgresClassTemplateRepository
      76              :   [ds]
      77              :   ClassTemplatePostgresRepository
      78              : 
      79              :   (find-by-id
      80              :    [_ id]
      81           28 :    (let [query  {:select [:*]
      82            8 :                  :from   [:template_classes]
      83           28 :                  :where  [:= :uuid [:cast id :uuid]]}
      84            4 :          result (->>
      85           12 :                   (sql/format query)
      86           12 :                   (jdbc/execute! ds)
      87            4 :                   first)]
      88           12 :      (row->class-template result)))
      89              : 
      90              :   (find-by-template-id
      91              :    [_ template-id]
      92           21 :    (let [query {:select [:*]
      93            6 :                 :from   [:template_classes]
      94           21 :                 :where  [:= :template_id [:cast template-id :uuid]]}]
      95            3 :      (->>
      96            9 :        (sql/format query)
      97            9 :        (jdbc/execute! ds)
      98            6 :        (mapv row->class-template))))
      99              : 
     100              :   (find-by-template-ids
     101              :    [_ template-ids]
     102            4 :    (if (empty? template-ids)
     103            0 :      []
     104            7 :      (let [query {:select [:*]
     105            2 :                   :from   [:template_classes]
     106           12 :                   :where  [:in :template_id (mapv #(vector :cast % :uuid) template-ids)]}]
     107            1 :        (->>
     108            3 :          (sql/format query)
     109            3 :          (jdbc/execute! ds)
     110            2 :          (mapv row->class-template)))))
     111              : 
     112              :   (search
     113              :    [_ filters pagination]
     114            8 :    (let [specs (filters->specs filters)
     115           14 :          query (qh/build-search-query base-query specs valid-sort-fields default-sort pagination)
     116            2 :          rows  (->>
     117            6 :                  (sql/format query)
     118            6 :                  (jdbc/execute! ds)
     119            4 :                  (mapv row->class-template))
     120           12 :          opts  (when-not (:cursor pagination)
     121           10 :                  (let [cnt-query (qh/build-count-query base-query specs)
     122            6 :                        total     (->>
     123            6 :                                    (sql/format cnt-query)
     124            6 :                                    (jdbc/execute-one! ds)
     125            2 :                                    utils.db/unqualify-keys
     126              :                                    :total)]
     127            6 :                    {:total-count total}))]
     128           14 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     129              : 
     130              :   (save!
     131              :    [_ class-template]
     132            4 :    (try
     133           32 :      (let [query  {:insert-into   :template_classes
     134           16 :                    :values        [(class-template->row class-template)]
     135            8 :                    :on-conflict   [:uuid]
     136           24 :                    :do-update-set {:fields [:name :max_players :updated_datetime]}
     137            8 :                    :returning     [:*]}
     138            4 :            result (->>
     139           20 :                     (sql/format (audit/stamp query))
     140           12 :                     (jdbc/execute! ds)
     141            4 :                     first)]
     142           20 :        (either/right (row->class-template result)))
     143              :      (catch Exception e
     144            0 :        (either/left
     145            0 :         (utils.errors/build-error
     146              :          :postgres
     147            0 :          {:message (.getMessage e)
     148            0 :           :context {:table :template_classes
     149              :                     :op    :upsert
     150            0 :                     :uuid  (:uuid class-template)}})))))
     151              : 
     152              :   (delete!
     153              :    [_ id]
     154            1 :    (try
     155            2 :      (jdbc/execute!
     156            1 :       ds
     157            2 :       (sql/format
     158            4 :        {:delete-from :template_classes
     159            7 :         :where       [:= :uuid [:cast id :uuid]]}))
     160            3 :      (either/right nil)
     161              :      (catch Exception e
     162            0 :        (either/left
     163            0 :         (utils.errors/build-error
     164              :          :postgres
     165            0 :          {:message (.getMessage e)
     166            0 :           :context {:table :template_classes
     167              :                     :op    :delete
     168            0 :                     :uuid  id}}))))))
     169              : 
     170              : 
     171            4 : (defn make-repository
     172              :  "Creates a PostgresClassTemplateRepository from a ds."
     173              :  [ds]
     174           12 :  (->PostgresClassTemplateRepository ds))
        

Generated by: LCOV version 2.0-1