Line data Source code
1 4 : (ns matchscore.domain.fsu.repository.postgres
2 : (:require
3 : [honey.sql :as sql]
4 : [matchscore.domain.source-sync.query :as source-sync]
5 : [next.jdbc :as jdbc]
6 : [next.jdbc.result-set :as rs]))
7 :
8 :
9 4 : (defprotocol
10 : FsuPostgresRepository
11 : (find-tournaments
12 : [this])
13 : (find-latest-snapshot-date
14 : [this])
15 : (find-rating-categories
16 : [this snapshot-date])
17 : (find-rating-entries
18 : [this snapshot-date category]))
19 :
20 :
21 16 : (def ^:private query-options {:builder-fn rs/as-unqualified-kebab-maps})
22 :
23 :
24 4 : (defn- sql-array->vector
25 : [value]
26 8 : (if (instance? java.sql.Array value) (vec (.getArray ^java.sql.Array value)) (vec (or value []))))
27 :
28 :
29 9 : (defn- tournament-row [row] (update row :formats sql-array->vector))
30 :
31 :
32 4 : (defrecord
33 : PostgresFsuRepository
34 : [ds]
35 : FsuPostgresRepository
36 : (find-tournaments
37 : [_]
38 10 : (let [query {:select [[:ft.uuid :uuid]
39 3 : [:ft.fsu_id :fsu-id]
40 3 : [:ft.name :name]
41 6 : [[:cast :ft.date_start :text] :date-start]
42 6 : [[:cast :ft.date_end :text] :date-end]
43 3 : [:ft.category :category]
44 3 : [:ft.formats :formats]
45 3 : [:ft.city :city]
46 3 : [:ft.venue :venue]
47 3 : [:ft.status_code :status-code]
48 3 : [:ft.is_actual :is-actual]
49 3 : [:ft.rankedin_id :rankedin-id]
50 6 : [[:cast :ft.event_id :text] :event-id]
51 3 : [:e.public_id :event-public-id]
52 5 : [(source-sync/iso-utc :e.source_synced_at) :synced-at]]
53 4 : :from [[:fsu_tournaments :ft]]
54 8 : :left-join [[:events :e] [:= :e.uuid :ft.event_id]]
55 7 : :order-by [[:ft.date_start :asc] [:ft.fsu_id :asc]]}]
56 10 : (mapv tournament-row (jdbc/execute! ds (sql/format query) query-options))))
57 : (find-latest-snapshot-date
58 : [_]
59 13 : (let [query {:select [[[:cast [:max :snapshot_date] :text] :snapshot-date]]
60 2 : :from [:fsu_rating_entries]}]
61 9 : (:snapshot-date (jdbc/execute-one! ds (sql/format query) query-options))))
62 : (find-rating-categories
63 : [_ snapshot-date]
64 7 : (let [query {:select-distinct [:category]
65 2 : :from [:fsu_rating_entries]
66 7 : :where [:= :snapshot_date [:cast snapshot-date :date]]}]
67 10 : (mapv :category (jdbc/execute! ds (sql/format query) query-options))))
68 : (find-rating-entries
69 : [_ snapshot-date category]
70 11 : (let [query {:select [[:fre.place :place]
71 3 : [:fre.previous_place :previous-place]
72 3 : [:fre.trend :trend]
73 3 : [:fre.points :points]
74 3 : [:fre.crm_profile_id :crm-profile-id]
75 3 : [:fp.fsu_id :fsu-id]
76 3 : [:fp.full_name :full-name]
77 3 : [:fp.city :city]]
78 4 : :from [[:fsu_rating_entries :fre]]
79 8 : :join [[:fsu_players :fp] [:= :fp.crm_profile_id :fre.crm_profile_id]]
80 2 : :where [:and
81 7 : [:= :fre.snapshot_date [:cast snapshot-date :date]]
82 4 : [:= :fre.category category]]
83 7 : :order-by [[:fre.place :asc] [:fp.full_name :asc]]}]
84 7 : (jdbc/execute! ds (sql/format query) query-options))))
85 :
86 :
87 4 : (defn make-repository
88 : "Creates a PostgresFsuRepository from a datasource."
89 : [ds]
90 3 : (->PostgresFsuRepository ds))
|