Line data Source code
1 4 : (ns matchscore.domain.tournament-subscription.repository.postgres
2 : (:require
3 : [matchscore.domain.tournament-subscription.repository :as repository]
4 : [next.jdbc :as jdbc]
5 : [next.jdbc.result-set :as rs]))
6 :
7 :
8 16 : (def ^:private options {:builder-fn rs/as-unqualified-kebab-maps})
9 :
10 :
11 4 : (defrecord
12 : PostgresTournamentSubscriptionRepository
13 : [ds]
14 : repository/TournamentSubscriptionRepository
15 : (list-subscriptions
16 : [_ user-id]
17 6 : (jdbc/execute!
18 3 : ds
19 6 : ["SELECT s.*,
20 : CASE s.target
21 : WHEN 'source' THEN upper(s.source)
22 : WHEN 'tier' THEN COALESCE(t.label_en, s.target_key)
23 : ELSE COALESCE(i.name, s.target_key)
24 : END AS target_label
25 : FROM tournament_subscriptions s
26 : LEFT JOIN tournament_tiers t
27 : ON s.target = 'tier' AND t.code = s.target_key
28 : LEFT JOIN imported_tournaments i
29 : ON s.target = 'tournament' AND i.source = s.source
30 : AND i.tournament_key = s.target_key
31 : WHERE s.user_id = ?::uuid
32 : ORDER BY s.created_datetime DESC, s.uuid"
33 9 : (str user-id)]
34 3 : options))
35 : (upsert-subscription!
36 : [_ user-id {:keys [source target target-key match-scope]}]
37 6 : (jdbc/execute-one!
38 3 : ds
39 6 : ["INSERT INTO tournament_subscriptions
40 : (user_id, source, target, target_key, match_scope)
41 : VALUES (?::uuid, ?, ?, ?, ?)
42 : ON CONFLICT (user_id, source, target, target_key)
43 : DO UPDATE SET match_scope = EXCLUDED.match_scope
44 : RETURNING *"
45 9 : (str user-id)
46 3 : source
47 3 : target
48 3 : target-key
49 3 : match-scope]
50 3 : options))
51 : (delete-subscription!
52 : [_ user-id id]
53 2 : (pos?
54 4 : (:next.jdbc/update-count
55 4 : (jdbc/execute-one!
56 2 : ds
57 4 : ["DELETE FROM tournament_subscriptions
58 : WHERE uuid = ?::uuid AND user_id = ?::uuid"
59 6 : (str id)
60 6 : (str user-id)]))))
61 : (find-tournament-subscription
62 : [_ user-id source tournament-key]
63 0 : (jdbc/execute-one!
64 0 : ds
65 0 : ["SELECT *
66 : FROM tournament_subscriptions
67 : WHERE user_id = ?::uuid AND source = ?
68 : AND target = 'tournament' AND target_key = ?"
69 0 : (str user-id)
70 0 : source
71 0 : (str tournament-key)]
72 0 : options))
73 : (list-tiers
74 : [_ source]
75 6 : (jdbc/execute!
76 3 : ds
77 6 : ["SELECT t.*, count(i.tournament_key)::int AS tournament_count
78 : FROM tournament_tiers t
79 : LEFT JOIN imported_tournaments i
80 : ON i.source = t.source AND i.tier = t.code AND i.date_end >= current_date
81 : WHERE t.source = ?
82 : GROUP BY t.code
83 : ORDER BY t.sort_order, t.code"
84 3 : source]
85 3 : options))
86 : (tournament-tier
87 : [_ source tournament-key]
88 0 : (jdbc/execute-one!
89 0 : ds
90 0 : ["SELECT t.*
91 : FROM imported_tournaments i
92 : LEFT JOIN tournament_tiers t ON t.code = i.tier
93 : WHERE i.source = ? AND i.tournament_key = ?"
94 0 : source
95 0 : (str tournament-key)]
96 0 : options))
97 : (valid-target?
98 : [_ source target target-key]
99 3 : (boolean
100 9 : (case target
101 39 : "source" (and (contains? #{"psa" "esf"} source) (nil? target-key))
102 4 : "tier" (jdbc/execute-one!
103 2 : ds
104 8 : ["SELECT 1 FROM tournament_tiers WHERE source = ? AND code = ?" source target-key])
105 : "tournament"
106 6 : (jdbc/execute-one!
107 3 : ds
108 6 : ["SELECT 1 FROM imported_tournaments
109 : WHERE source = ? AND tournament_key = ?"
110 3 : source
111 9 : (str target-key)])
112 : false))))
113 :
114 :
115 4 : (defn make-repository
116 : [ds]
117 12 : (->PostgresTournamentSubscriptionRepository ds))
|