Skip to content

Commit

Permalink
fix findRound bug and create roundNumber attribute for Search
Browse files Browse the repository at this point in the history
  • Loading branch information
JCGuest committed Aug 29, 2021
1 parent 40d0107 commit 7b1d83f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
42 changes: 20 additions & 22 deletions models/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Search {
this.position = this.getPosition(searchQuery);
}
search() {
// console.log([this.query, this.raceName, this.year, this.position]);
if (this.lastWeek) {
return this.searchLastWeek();
}
Expand All @@ -21,14 +20,14 @@ class Search {
else if (this.driverChamp) {
return this.searchDriverChamp();
}
else if (this.year) {
else if (this.year != null) {
return this.searchPast();
}
else {
return 'Please enter a valid search query';
}
// else {
// return 'Please enter a valid search query';
// }
}
// in here i will switch the flags for lastWeek or call a switch for teamChamp, and driverChamp
// here i will switch the flags for lastWeek or call a switch for teamChamp, and driverChamp
async champOrLastWeekSearch(query) {
const lastWeekWords = [
'last',
Expand Down Expand Up @@ -62,31 +61,30 @@ class Search {
}
}
async searchPast() {
let roundNumber = await this.findRound();
if (roundNumber) {
let result = await axios
.get(`http://ergast.com/api/f1/${this.year}/${roundNumber}/results/${this.position}.json`)
.then((json) => {
const data = json.data;
return (data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName);
})
.catch((err) => console.error(err));
return result;
}
this.roundNumber = await this.findRound();
let result = await axios
.get(`http://ergast.com/api/f1/${this.year}/${this.roundNumber}/results/${this.position}.json`)
.then((json) => {
const data = json.data;
return (data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName);
})
.catch((err) => console.error(err));
return result;
}
async findRound() {
if (!this.champOrLastWeekQuery) {
let answer = await axios
.get(`http://ergast.com/api/f1/${this.year}.json`)
.then((json) => {
let data = json.data;
return data.MRData.RaceTable.Races.find((race) => {
let round = '';
json.data.MRData.RaceTable.Races.forEach((race) => {
if (race.raceName.toString().toLowerCase() == this.raceName) {
return race.raceName.round;
round = race.round;
}
});
return round;
})
.catch((err) => console.error(err));
return answer;
Expand Down
51 changes: 25 additions & 26 deletions models/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Search {
public raceName: string;
public year: string;
public position: string;
public roundNumber: string;
public lastWeek: boolean = false;
public champOrLastWeekQuery: boolean = false;
public teamChamp: boolean = false;
Expand All @@ -17,21 +18,21 @@ class Search {
this.position = this.getPosition(searchQuery);
}
search() {
// console.log([this.query, this.raceName, this.year, this.position]);
if (this.lastWeek) {
return this.searchLastWeek();
} else if (this.teamChamp) {
return this.searchTeamChamp();
} else if (this.driverChamp) {
return this.searchDriverChamp();
} else if (this.year) {
} else if (this.year != null) {
return this.searchPast();
} else {
return 'Please enter a valid search query';
}
// else {
// return 'Please enter a valid search query';
// }
}

// in here i will switch the flags for lastWeek or call a switch for teamChamp, and driverChamp
// here i will switch the flags for lastWeek or call a switch for teamChamp, and driverChamp
async champOrLastWeekSearch(query: string) {
const lastWeekWords = [
'last',
Expand Down Expand Up @@ -69,40 +70,38 @@ class Search {
}

async searchPast() {
let roundNumber = await this.findRound();
if (roundNumber) {
let result = await axios
.get(
`http://ergast.com/api/f1/${this.year}/${roundNumber}/results/${this.position}.json`
)
.then((json) => {
const data = json.data;
return (
data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName
);
})
.catch((err) => console.error(err));
this.roundNumber = await this.findRound();
let result = await axios
.get(
`http://ergast.com/api/f1/${this.year}/${this.roundNumber}/results/${this.position}.json`
)
.then((json) => {
const data = json.data;
return (
data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName
);
})
.catch((err) => console.error(err));

return result;
}
return result;
}

async findRound() {
if (!this.champOrLastWeekQuery) {
let answer = await axios
.get(`http://ergast.com/api/f1/${this.year}.json`)
.then((json) => {
let data = json.data;
return data.MRData.RaceTable.Races.find((race) => {
let round = '';
json.data.MRData.RaceTable.Races.forEach((race) => {
if (race.raceName.toString().toLowerCase() == this.raceName) {
return race.raceName.round;
round = race.round;
}
});
return round;
})
.catch((err) => console.error(err));

return answer;
}
}
Expand Down

0 comments on commit 7b1d83f

Please sign in to comment.