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

            Line data    Source code
       1            4 : (ns matchscore.domain.bracket-match.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.bracket-match.entity :as entity]
       5              :    [matchscore.utils.audit :as audit]
       6              :    [matchscore.utils.db :as utils.db]
       7              :    [matchscore.utils.pagination :as qh]
       8              :    [next.jdbc :as jdbc]))
       9              : 
      10              : 
      11            4 : (defprotocol
      12              :   BracketMatchPostgresRepository
      13              :   (find-by-id
      14              :    [this id]
      15              :    "Find bracket match by UUID.")
      16              :   (find-by-ids
      17              :    [this ids]
      18              :    "Find bracket matches by multiple UUIDs. Returns seq.")
      19              :   (find-by-bracket-round-id
      20              :    [this bracket-round-id]
      21              :    "Find all matches in a round, ordered by match-number. Returns seq.")
      22              :   (find-by-bracket-round-ids
      23              :    [this bracket-round-ids]
      24              :    "Find all matches for multiple rounds in one batch query. Returns seq.")
      25              :   (search
      26              :    [this filters pagination]
      27              :    "Search. filters: {:bracketRoundId {...}}. sort: #{:match-number}, default: :match-number")
      28              :   (upsert!
      29              :    [this match-data]
      30              :    "Insert or update a bracket match. Returns [ok? result error-message]."))
      31              : 
      32              : 
      33            4 : (defn- filters->specs
      34              :   "Converts a GraphQL filters map to a seq of [column op value] specs."
      35              :   [{:keys [bracketRoundId]}]
      36            7 :   (qh/typed-filters->specs {:bracket-round-id (utils.db/cast-uuid-filter bracketRoundId)}))
      37              : 
      38              : 
      39            4 : (defn- row->bracket-match
      40              :   "Converts a database row to a domain BracketMatch map."
      41              :   [row]
      42           32 :   (when row (entity/make-bracket-match (utils.db/unqualify-keys row))))
      43              : 
      44              : 
      45           12 : (def ^:private valid-sort-fields #{:match-number})
      46              : 
      47              : 
      48            8 : (def ^:private default-sort :match-number)
      49              : 
      50              : 
      51            4 : (def ^:private base-query
      52           28 :   {:select [[:bm.uuid :uuid]
      53           12 :             [:bm.bracket-round-id :bracket-round-id]
      54           12 :             [:bm.match-number :match-number]
      55           12 :             [:bm.label :label]
      56           12 :             [:bm.type :type]
      57           12 :             [:bm.source :source]]
      58           16 :    :from   [[:bracket-matches :bm]]})
      59              : 
      60              : 
      61            4 : (defrecord
      62              :   PostgresBracketMatchRepository
      63              :   [ds]
      64              :   BracketMatchPostgresRepository
      65              : 
      66              :   (find-by-id
      67              :    [_ id]
      68           48 :    (let [query  (assoc base-query :where [:= :bm.uuid [:cast id :uuid]])
      69            4 :          result (->>
      70           12 :                   (sql/format query)
      71           12 :                   (jdbc/execute! ds)
      72            4 :                   first)]
      73           12 :      (row->bracket-match result)))
      74              : 
      75              :   (find-by-ids
      76              :    [_ ids]
      77            4 :    (if (empty? ids)
      78            0 :      []
      79           17 :      (let [query (assoc base-query :where [:in :bm.uuid (mapv #(vector :cast % :uuid) ids)])]
      80            1 :        (->>
      81            3 :          (sql/format query)
      82            3 :          (jdbc/execute! ds)
      83            2 :          (mapv row->bracket-match)))))
      84              : 
      85              :   (find-by-bracket-round-id
      86              :    [_ bracket-round-id]
      87            8 :    (let [query (->>
      88            4 :                  base-query
      89            4 :                  (merge
      90           40 :                   {:where    [:= :bm.bracket-round-id [:cast bracket-round-id :uuid]]
      91           16 :                    :order-by [[:bm.match-number :asc]]}))]
      92            4 :      (->>
      93           12 :        (sql/format query)
      94           12 :        (jdbc/execute! ds)
      95            8 :        (mapv row->bracket-match))))
      96              : 
      97              :   (find-by-bracket-round-ids
      98              :    [_ bracket-round-ids]
      99           16 :    (if (empty? bracket-round-ids)
     100            1 :      []
     101            4 :      (let [query
     102            4 :            (->>
     103            4 :              base-query
     104            4 :              (merge
     105           60 :               {:where    [:in :bm.bracket-round-id (mapv #(vector :cast % :uuid) bracket-round-ids)]
     106           16 :                :order-by [[:bm.match-number :asc]]}))]
     107            4 :        (->>
     108           12 :          (sql/format query)
     109           12 :          (jdbc/execute! ds)
     110            8 :          (mapv row->bracket-match)))))
     111              : 
     112              :   (search
     113              :    [_ filters pagination]
     114            4 :    (let [specs (filters->specs filters)
     115            2 :          query (qh/build-search-query
     116            1 :                 base-query
     117            1 :                 specs
     118            1 :                 valid-sort-fields
     119            1 :                 default-sort
     120            1 :                 pagination
     121            5 :                 {:sort-field-casts {:match-number :integer}})
     122            1 :          rows  (->>
     123            3 :                  (sql/format query)
     124            3 :                  (jdbc/execute! ds)
     125            2 :                  (mapv row->bracket-match))
     126            6 :          opts  (when-not (:cursor pagination)
     127            5 :                  (let [cnt-query (qh/build-count-query base-query specs)
     128            3 :                        total     (->>
     129            3 :                                    (sql/format cnt-query)
     130            3 :                                    (jdbc/execute-one! ds)
     131            1 :                                    utils.db/unqualify-keys
     132              :                                    :total)]
     133            3 :                    {:total-count total}))]
     134            7 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     135              : 
     136              :   (upsert!
     137              :    [_ match-data]
     138            3 :    (try
     139            6 :      (let [{:keys [uuid bracket-round-id match-number label type source]} match-data
     140           21 :            query  {:insert-into   :bracket_matches
     141           36 :                    :values        [{:uuid         [:cast uuid :uuid]
     142           12 :                                     :bracket_round_id [:cast bracket-round-id :uuid]
     143            3 :                                     :match_number match-number
     144            3 :                                     :label        label
     145            3 :                                     :type         type
     146           15 :                                     :source       (or source "matchscore")}]
     147            6 :                    :on-conflict   [:uuid]
     148           21 :                    :do-update-set {:fields [:bracket_round_id :match_number :label :type]}
     149            6 :                    :returning     [:*]}
     150            6 :            result (->>
     151            6 :                     (sql/format
     152            9 :                      (audit/stamp query)
     153           15 :                      {:quoted       true
     154              :                       :quoted-snake true})
     155            9 :                     (jdbc/execute! ds)
     156            3 :                     first
     157            3 :                     row->bracket-match)]
     158           12 :        [true result nil])
     159            0 :      (catch Exception e [false :database-error (.getMessage e)]))))
     160              : 
     161              : 
     162            4 : (defn make-repository
     163              :  "Creates a PostgresBracketMatchRepository from a ds."
     164              :  [ds]
     165           12 :  (->PostgresBracketMatchRepository ds))
        

Generated by: LCOV version 2.0-1