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

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

Generated by: LCOV version 2.0-1