Line data Source code
1 4 : (ns matchscore.domain.team-member.repository.postgres
2 : (:require
3 : [honey.sql :as sql]
4 : [matchscore.domain.team-member.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 : TeamMemberPostgresRepository
12 : (find-by-id
13 : [this id]
14 : "Find team member by UUID.")
15 : (find-by-team-id
16 : [this team-id]
17 : "Find all members of a team. Returns seq.")
18 : (search
19 : [this filters pagination]
20 : "Search team members. filters: {:teamId {...} :userId {...}}. sort: #{:joined-datetime}, default: :joined-datetime"))
21 :
22 :
23 4 : (defn- filters->specs
24 : "Converts a GraphQL filters map to a seq of [column op value] specs."
25 : [{:keys [teamId userId]}]
26 2 : (qh/typed-filters->specs
27 6 : {:team-id (utils.db/cast-uuid-filter teamId)
28 3 : :user-id (utils.db/cast-uuid-filter userId)}))
29 :
30 :
31 4 : (defn- row->team-member
32 : "Converts a database row to a domain TeamMember map."
33 : [row]
34 8 : (when row (entity/make-team-member (utils.db/unqualify-keys row))))
35 :
36 :
37 12 : (def ^:private valid-sort-fields #{:joined-datetime})
38 :
39 :
40 8 : (def ^:private default-sort :joined-datetime)
41 :
42 :
43 4 : (def ^:private base-query
44 28 : {:select [[:tm.uuid :uuid]
45 12 : [:tm.team-id :team-id]
46 12 : [:tm.user-id :user-id]
47 12 : [:tm.user-id-rankedin :user-id-rankedin]
48 12 : [:tm.role :role]
49 12 : [:tm.joined-datetime :joined-datetime]
50 12 : [:tm.source :source]]
51 16 : :from [[:team-members :tm]]})
52 :
53 :
54 4 : (defrecord
55 : PostgresTeamMemberRepository
56 : [ds]
57 : TeamMemberPostgresRepository
58 :
59 : (find-by-id
60 : [_ id]
61 12 : (let [query (assoc base-query :where [:= :tm.uuid [:cast id :uuid]])
62 1 : result (->>
63 3 : (sql/format query)
64 3 : (jdbc/execute! ds)
65 1 : first)]
66 3 : (row->team-member result)))
67 :
68 : (find-by-team-id
69 : [_ team-id]
70 3 : (let [query (->
71 1 : base-query
72 8 : (assoc :where [:= :tm.team-id [:cast team-id :uuid]]))
73 1 : result (->>
74 3 : (sql/format query)
75 3 : (jdbc/execute! ds)
76 2 : (mapv row->team-member))]
77 1 : result))
78 :
79 : (search
80 : [_ filters pagination]
81 4 : (let [specs (filters->specs filters)
82 2 : query (qh/build-search-query
83 1 : base-query
84 1 : specs
85 1 : valid-sort-fields
86 1 : default-sort
87 1 : pagination
88 5 : {:sort-field-casts {:joined-datetime :timestamptz}})
89 1 : rows (->>
90 3 : (sql/format query)
91 3 : (jdbc/execute! ds)
92 2 : (mapv row->team-member))
93 6 : opts (when-not (:cursor pagination)
94 5 : (let [cnt-query (qh/build-count-query base-query specs)
95 3 : total (->>
96 3 : (sql/format cnt-query)
97 3 : (jdbc/execute-one! ds)
98 1 : utils.db/unqualify-keys
99 : :total)]
100 3 : {:total-count total}))]
101 7 : (qh/paginate-results rows valid-sort-fields default-sort pagination opts))))
102 :
103 :
104 4 : (defn make-repository
105 : "Creates a PostgresTeamMemberRepository from a ds."
106 : [ds]
107 12 : (->PostgresTeamMemberRepository ds))
|