MCPcopy Create free account
hub / github.com/CFBD/cfb-api / playerSearch

Function playerSearch

app/player/player.service.js:3–60  ·  view source on GitHub ↗
(year, school, position, searchTerm)

Source from the content-addressed store, hash-verified

1module.exports = (db) => {
2
3 const playerSearch = async (year, school, position, searchTerm) => {
4 let filter;
5 let params;
6 let index;
7
8 if (year) {
9 filter = 'WHERE att.start_year <= $1 AND att.end_year >= $1';
10 params = [year];
11 index = 2;
12 } else {
13 filter = '';
14 params = [];
15 index = 1;
16 }
17
18 if (school) {
19 filter += ` AND LOWER(t.school) = LOWER($${index})`;
20 params.push(school);
21 index++;
22 }
23
24 if (position) {
25 filter += ` AND LOWER(p.abbreviation) = LOWER($${index})`;
26 params.push(position);
27 index++;
28 }
29
30 filter += ` AND LOWER(a.name) LIKE LOWER('%$${index}:value%')`;
31 params.push(searchTerm);
32 index++;
33
34 const results = await db.any(`
35 SELECT DISTINCT a.id, t.school, a.name, a.first_name, a.last_name, a.weight, a.height, a.jersey, p.abbreviation AS "position", h.city || ', ' || h.state AS hometown, '#' || t.color AS color, '#' || t.alt_color AS alt_color
36 FROM athlete AS a
37 INNER JOIN athlete_team AS att ON a.id = att.athlete_id
38 INNER JOIN team AS t ON att.team_id = t.id
39 INNER JOIN "position" AS p ON a.position_id = p.id
40 INNER JOIN hometown AS h ON a.hometown_id = h.id
41 ${filter}
42 ORDER BY a.name
43 LIMIT 100
44 `, params);
45
46 return results.map(r => ({
47 id: r.id,
48 team: r.school,
49 name: r.name,
50 firstName: r.first_name,
51 lastName: r.last_name,
52 weight: r.weight,
53 height: r.height,
54 jersey: r.jersey,
55 position: r.position,
56 hometown: r.hometown,
57 teamColor: r.color,
58 teamColorSecondary: r.alt_color
59 }));
60 };

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected