-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathteams.js
More file actions
58 lines (52 loc) · 2.17 KB
/
teams.js
File metadata and controls
58 lines (52 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const axios = require('axios');
const url = 'https://megafanz.mybluemix.net/api/team/list?league=';
module.exports = {
getTeams: function (name, league) {
console.log('Requested leagues %s', league);
//if league name not provided, then search all leagues
if (!league) league = ["nhl", "nba", "mlb", "nfl"];
var teamRequests = [];
league.forEach(element => {
var func = function findTeams() { return axios.get(url + element) };
teamRequests.push(func());
})
return axios.all(teamRequests).then(axios.spread((response1, response2, response3, response4) => {
var teamsList = [];
if (response1 && response1.data) {
response1.data.teams.filter(function (team) {
if (team.name.toLowerCase().indexOf(name.toLowerCase()) >=0) {
console.log("Found team %s", team.name);
teamsList.push(team);
}
});
}
if (response2 && response2.data) {
response2.data.teams.filter(function (team) {
if (team.name.toLowerCase().indexOf(name.toLowerCase()) >=0) {
console.log("Found team %s", team.name)
teamsList.push(team);
}
});
}
if (response3 && response3.data) {
response3.data.teams.filter(function (team) {
if (team.name.toLowerCase().indexOf(name.toLowerCase()) >=0) {
console.log("Found team %s", team.name)
teamsList.push(team);
}
});
}
if (response4 && response4.data) {
response4.data.teams.filter(function (team) {
if (team.name.toLowerCase().indexOf(name.toLowerCase()) >=0) {
console.log("Found team %s", team.name)
teamsList.push(team);
}
});
}
return teamsList;
})).catch(error => {
console.log(error);
});
}
};