Line data Source code
1 4 : (ns matchscore.domain.esf.repository.postgres
2 : (:require
3 : [clojure.string :as str]
4 : [matchscore.domain.country-exclusion :as country-exclusion]
5 : [matchscore.domain.source-sync.query :as source-sync]
6 : [next.jdbc :as jdbc]
7 : [next.jdbc.result-set :as rs]))
8 :
9 :
10 4 : (defprotocol
11 : EsfPostgresRepository
12 : (find-tournaments
13 : [this year])
14 : (find-tournament
15 : [this ts-id])
16 : (find-ranking
17 : [this circuit category-id]))
18 :
19 :
20 16 : (def ^:private options {:builder-fn rs/as-unqualified-kebab-maps})
21 :
22 :
23 4 : (def ^:private tournament-select
24 16 : (str
25 : "SELECT t.ts_id::text AS ts_id, t.name, t.date_start::text AS date_start,
26 : t.date_end::text AS date_end, t.location, t.country_code, t.url,
27 : t.event_id::text AS event_id, e.public_id AS event_public_id, "
28 16 : (format source-sync/iso-utc-sql "e.source_synced_at")
29 : " AS synced_at
30 : FROM esf_tournaments t LEFT JOIN events e ON e.uuid=t.event_id
31 : WHERE "
32 12 : (country-exclusion/allowed-sql "t.country_code")))
33 :
34 :
35 8 : (def ^:private uuid-re #"(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
36 :
37 :
38 4 : (defrecord
39 : PostgresEsfRepository
40 : [ds]
41 : EsfPostgresRepository
42 : (find-tournaments
43 : [_ year]
44 6 : (jdbc/execute!
45 3 : ds
46 6 : (if year
47 12 : [(str
48 3 : tournament-select
49 : " AND t.date_start<=?::date AND t.date_end>=?::date ORDER BY t.date_start,t.name")
50 12 : (str year "-12-31")
51 12 : (str year "-01-01")]
52 0 : [(str tournament-select " ORDER BY t.date_start,t.name")])
53 3 : options))
54 : (find-tournament
55 : [_ ts-id]
56 : ;; TournamentSoftware ids are GUIDs, in any case; anything else is unknown.
57 28 : (when (and (string? ts-id) (re-matches uuid-re (str/trim ts-id)))
58 4 : (jdbc/execute-one!
59 2 : ds
60 16 : [(str tournament-select " AND t.ts_id = ?::uuid") (str/trim ts-id)]
61 2 : options)))
62 : (find-ranking
63 : [_ circuit category-id]
64 2 : (let
65 : [categories
66 4 : (jdbc/execute!
67 2 : ds
68 4 : ["SELECT publication_id, category_id, name, period, updated_at, source_url
69 : FROM esf_ranking_categories WHERE circuit=? AND publication_id=
70 : (SELECT MAX(publication_id) FROM esf_ranking_categories WHERE circuit=?)
71 : ORDER BY CASE WHEN name LIKE '%Overall' THEN 0 ELSE 1 END,category_id"
72 2 : circuit
73 2 : circuit]
74 2 : options)
75 10 : selected (or
76 22 : (first (filter #(= (:category-id %) category-id) categories))
77 14 : (when (nil? category-id) (first categories)))
78 : entries
79 4 : (if selected
80 4 : (jdbc/execute!
81 2 : ds
82 10 : [(str
83 : "SELECT re.player_id, re.esid, re.full_name, re.country_code, re.rank,
84 : re.previous_rank, re.points, re.tournaments, re.profile_url,
85 : ru.photo_url
86 : FROM esf_ranking_entries re
87 : LEFT JOIN rankedin_users ru ON ru.source='esf'
88 : AND ('ES' || ru.rankedin_id::text)=re.esid
89 : WHERE re.publication_id=? AND re.category_id=?
90 : AND "
91 6 : (country-exclusion/allowed-sql "re.country_code")
92 : "
93 : ORDER BY re.rank,re.full_name")
94 6 : (:publication-id selected)
95 6 : (:category-id selected)]
96 2 : options)
97 0 : [])]
98 18 : {:circuit circuit
99 6 : :period (:period selected)
100 6 : :updated-at (:updated-at selected)
101 6 : :source-url (:source-url selected)
102 6 : :category-id (:category-id selected)
103 20 : :categories (mapv #(select-keys % [:category-id :name]) categories)
104 18 : :entries (mapv #(update % :points str) entries)})))
105 :
106 :
107 4 : (defn make-repository
108 : [ds]
109 12 : (->PostgresEsfRepository ds))
|