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

            Line data    Source code
       1            4 : (ns matchscore.domain.series.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.series.entity :as entity]
       5              :    [matchscore.utils.db :as utils.db]
       6              :    [matchscore.utils.pagination :as qh]
       7              :    [next.jdbc :as jdbc]))
       8              : 
       9              : 
      10            4 : (defprotocol
      11              :   SeriesPostgresRepository
      12              :   (find-by-id
      13              :    [this id]
      14              :    "Find series by UUID.")
      15              :   (find-by-group-id
      16              :    [this group-id]
      17              :    "Find all series for a group. Returns seq.")
      18              :   (find-by-group-ids
      19              :    [this group-ids]
      20              :    "Find all series for multiple groups in one batch query. Returns seq.")
      21              :   (find-by-bracket-match-id
      22              :    [this bracket-match-id]
      23              :    "Find all series for a bracket match. Returns seq.")
      24              :   (find-by-bracket-match-ids
      25              :    [this bracket-match-ids]
      26              :    "Find all series for multiple bracket matches in one batch query. Returns seq.")
      27              :   (search
      28              :    [this filters pagination]
      29              :    "Search. filters: {:groupId {...} :bracketMatchId {...} :state {...} :contextType {...}}. sort: #{:state}, default: :state"))
      30              : 
      31              : 
      32            4 : (defn- filters->specs
      33              :   "Converts a GraphQL filters map to a seq of [column op value] specs."
      34              :   [{:keys [groupId bracketMatchId state contextType]}]
      35            2 :   (qh/typed-filters->specs
      36            8 :    {:group-id         (utils.db/cast-uuid-filter groupId)
      37            3 :     :bracket-match-id (utils.db/cast-uuid-filter bracketMatchId)
      38            1 :     :state            state
      39            1 :     :context-type     contextType}))
      40              : 
      41              : 
      42            4 : (defn- row->series
      43              :   "Converts a database row to a domain Series map."
      44              :   [row]
      45           16 :   (when row (entity/make-series (utils.db/unqualify-keys row))))
      46              : 
      47              : 
      48           12 : (def ^:private valid-sort-fields #{:state})
      49              : 
      50              : 
      51            8 : (def ^:private default-sort :state)
      52              : 
      53              : 
      54            4 : (def ^:private base-query
      55           28 :   {:select [[:s.uuid :uuid]
      56           12 :             [:s.context-type :context-type]
      57           12 :             [:s.group-id :group-id]
      58           12 :             [:s.bracket-match-id :bracket-match-id]
      59           12 :             [:s.ruleset-id :ruleset-id]
      60           12 :             [:s.match-format :match-format]
      61           12 :             [:s.best-of :best-of]
      62           12 :             [:s.wins-required :wins-required]
      63           12 :             [:s.match-count :match-count]
      64           12 :             [:s.state :state]
      65           12 :             [:s.winner-participant-id :winner-participant-id]
      66           12 :             [:s.extra :extra]
      67           12 :             [:s.source :source]]
      68           16 :    :from   [[:series :s]]})
      69              : 
      70              : 
      71            4 : (defrecord
      72              :   PostgresSeriesRepository
      73              :   [ds]
      74              :   SeriesPostgresRepository
      75              : 
      76              :   (find-by-id
      77              :    [_ id]
      78           12 :    (let [query  (assoc base-query :where [:= :s.uuid [:cast id :uuid]])
      79            1 :          result (->>
      80            3 :                   (sql/format query)
      81            3 :                   (jdbc/execute! ds)
      82            1 :                   first)]
      83            3 :      (row->series result)))
      84              : 
      85              :   (find-by-group-id
      86              :    [_ group-id]
      87           13 :    (let [query (merge base-query {:where [:= :s.group-id [:cast group-id :uuid]]})]
      88            1 :      (->>
      89            3 :        (sql/format query)
      90            3 :        (jdbc/execute! ds)
      91            2 :        (mapv row->series))))
      92              : 
      93              :   (find-by-group-ids
      94              :    [_ group-ids]
      95            8 :    (if (empty? group-ids)
      96            1 :      []
      97            6 :      (let [query (->
      98            2 :                    base-query
      99           26 :                    (assoc :where [:in :s.group-id (mapv #(vector :cast % :uuid) group-ids)]))]
     100            2 :        (->>
     101            6 :          (sql/format query)
     102            6 :          (jdbc/execute! ds)
     103            4 :          (mapv row->series)))))
     104              : 
     105              :   (find-by-bracket-match-id
     106              :    [_ bracket-match-id]
     107            0 :    (let [query (merge base-query {:where [:= :s.bracket-match-id [:cast bracket-match-id :uuid]]})]
     108            0 :      (->>
     109            0 :        (sql/format query)
     110            0 :        (jdbc/execute! ds)
     111            0 :        (mapv row->series))))
     112              : 
     113              :   (find-by-bracket-match-ids
     114              :    [_ bracket-match-ids]
     115            4 :    (if (empty? bracket-match-ids)
     116            0 :      []
     117            3 :      (let [query (->
     118            1 :                    base-query
     119            4 :                    (assoc :where [:in
     120              :                                   :s.bracket-match-id
     121            9 :                                   (mapv #(vector :cast % :uuid) bracket-match-ids)]))]
     122            1 :        (->>
     123            3 :          (sql/format query)
     124            3 :          (jdbc/execute! ds)
     125            2 :          (mapv row->series)))))
     126              : 
     127              :   (search
     128              :    [_ filters pagination]
     129            4 :    (let [specs (filters->specs filters)
     130            7 :          query (qh/build-search-query base-query specs valid-sort-fields default-sort pagination)
     131            1 :          rows  (->>
     132            3 :                  (sql/format query)
     133            3 :                  (jdbc/execute! ds)
     134            2 :                  (mapv row->series))
     135            6 :          opts  (when-not (:cursor pagination)
     136            5 :                  (let [cnt-query (qh/build-count-query base-query specs)
     137            3 :                        total     (->>
     138            3 :                                    (sql/format cnt-query)
     139            3 :                                    (jdbc/execute-one! ds)
     140            1 :                                    utils.db/unqualify-keys
     141              :                                    :total)]
     142            3 :                    {:total-count total}))]
     143            7 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts))))
     144              : 
     145              : 
     146            4 : (defn make-repository
     147              :  "Creates a PostgresSeriesRepository from a ds."
     148              :  [ds]
     149           12 :  (->PostgresSeriesRepository ds))
        

Generated by: LCOV version 2.0-1