Skip to content

Commit

Permalink
removing gameId param from some endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueSCar committed Aug 27, 2024
1 parent 8f775f8 commit 676fc44
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 84 deletions.
4 changes: 2 additions & 2 deletions app/game/game.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ module.exports = (db, Sentry) => {
},
getWeather: async (req, res) => {
try {
if (!req.query.gameId && !req.query.year) {
if (!req.query.year) {
res.status(400).send({
error: 'Year is required'
});
Expand All @@ -626,7 +626,7 @@ module.exports = (db, Sentry) => {
error: 'Year must be an integer'
});
} else {
const results = await service.getWeather(req.query.gameId, req.query.year, req.query.seasonType, req.query.week, req.query.team, req.query.conference, req.query.classification);
const results = await service.getWeather(req.query.year, req.query.seasonType, req.query.week, req.query.team, req.query.conference, req.query.classification);
res.send(results);
}
} catch (err) {
Expand Down
65 changes: 30 additions & 35 deletions app/game/game.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,50 +236,45 @@ module.exports = (db) => {
}));
};

const getWeather = async (gameId, year, seasonType, week, team, conference, classification) => {
const getWeather = async (year, seasonType, week, team, conference, classification) => {
const filters = [];
const params = [];
let index = 1;

if (gameId) {
filters.push(`g.id = $${index}`);
params.push(gameId)
} else {
if (year) {
filters.push(`g.season = $${index}`);
params.push(year);
index++;
}
if (year) {
filters.push(`g.season = $${index}`);
params.push(year);
index++;
}

if (seasonType && seasonType.toLowerCase() !== 'both') {
filters.push(`g.season_type = '${seasonType}'`);
params.push(seasonType);
index++;
}
if (seasonType && seasonType.toLowerCase() !== 'both') {
filters.push(`g.season_type = '${seasonType}'`);
params.push(seasonType);
index++;
}

if (week) {
filters.push(`g.week = $${index}`);
params.push(week);
index++
}
if (week) {
filters.push(`g.week = $${index}`);
params.push(week);
index++
}

if (team) {
filters.push(`(LOWER(home.school) = LOWER($${index}) OR LOWER(away.school) = LOWER($${index}))`);
params.push(team);
index++;
}
if (team) {
filters.push(`(LOWER(home.school) = LOWER($${index}) OR LOWER(away.school) = LOWER($${index}))`);
params.push(team);
index++;
}

if (conference) {
filters.push(`(LOWER(hc.abbreviation) = LOWER($${index}) OR LOWER(ac.abbreviation) = LOWER($${index}))`);
params.push(conference);
index++;
}
if (conference) {
filters.push(`(LOWER(hc.abbreviation) = LOWER($${index}) OR LOWER(ac.abbreviation) = LOWER($${index}))`);
params.push(conference);
index++;
}

if (classification && ['fbs', 'fcs', 'ii', 'iii'].includes(classification.toLowerCase())) {
filters.push(`(hc.division = $${index} OR ac.division = $${index})`);
params.push(classification.toLowerCase());
index++;
}
if (classification && ['fbs', 'fcs', 'ii', 'iii'].includes(classification.toLowerCase())) {
filters.push(`(hc.division = $${index} OR ac.division = $${index})`);
params.push(classification.toLowerCase());
index++;
}

const filter = 'WHERE ' + filters.join(' AND ');
Expand Down
8 changes: 4 additions & 4 deletions app/lines/lines.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ module.exports = (db, Sentry) => {

const getLines = async (req, res) => {
try {
if (req.query.gameId && isNaN(req.query.gameId)) {
if (!req.query.year) {
res.status(400).send({
error: 'gameId parameter must be numeric.'
error: 'year parameter is required.'
});

return;
} else if (!req.query.gameId && (!req.query.year || isNaN(req.query.year))) {
} else if (isNaN(req.query.year)) {
res.status(400).send({
error: 'A numeric year parameter must be specified.'
});
Expand All @@ -24,7 +24,7 @@ module.exports = (db, Sentry) => {

return;
} else {
const lines = await service.getLines(req.query.gameId, req.query.year, req.query.seasonType, req.query.week, req.query.team, req.query.home, req.query.away, req.query.conference);
const lines = await service.getLines(req.query.year, req.query.seasonType, req.query.week, req.query.team, req.query.home, req.query.away, req.query.conference);
res.send(lines);
}
} catch (err) {
Expand Down
75 changes: 34 additions & 41 deletions app/lines/lines.service.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
module.exports = (db) => {
const getLines = async (gameId, year, seasonType, week, team, home, away, conference) => {
let filter;
let params;
const getLines = async (year, seasonType, week, team, home, away, conference) => {
let filter = 'WHERE g.season = $1';
let params = [year];

if (gameId) {
filter = 'WHERE g.id = $1';
params = [gameId];
} else {
filter = 'WHERE g.season = $1';
params = [year];
let index = 2;
let index = 2;

if (seasonType != 'both') {
filter += ` AND g.season_type = $${index}`;
params.push(seasonType || 'regular');
index++;
}
if (seasonType != 'both') {
filter += ` AND g.season_type = $${index}`;
params.push(seasonType || 'regular');
index++;
}

if (week) {
filter += ` AND g.week = $${index}`;
params.push(week);
index++;
}
if (week) {
filter += ` AND g.week = $${index}`;
params.push(week);
index++;
}

if (team) {
filter += ` AND (LOWER(awt.school) = LOWER($${index}) OR LOWER(ht.school) = LOWER($${index}))`;
params.push(team);
index++;
}
if (home) {
filter += ` AND LOWER(ht.school) = LOWER($${index})`;
params.push(home);
index++;
}
if (team) {
filter += ` AND (LOWER(awt.school) = LOWER($${index}) OR LOWER(ht.school) = LOWER($${index}))`;
params.push(team);
index++;
}

if (home) {
filter += ` AND LOWER(ht.school) = LOWER($${index})`;
params.push(home);
index++;
}

if (away) {
filter += ` AND LOWER(awt.school) = LOWER($${index})`;
params.push(away);
index++;
}
if (away) {
filter += ` AND LOWER(awt.school) = LOWER($${index})`;
params.push(away);
index++;
}

if (conference) {
filter += ` AND (LOWER(hc.abbreviation) = LOWER($${index}) OR LOWER(ac.abbreviation) = LOWER($${index}))`;
params.push(conference);
index++;
}
if (conference) {
filter += ` AND (LOWER(hc.abbreviation) = LOWER($${index}) OR LOWER(ac.abbreviation) = LOWER($${index}))`;
params.push(conference);
index++;
}

let games = await db.any(`
Expand Down
2 changes: 1 addition & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"swagger": "2.0",
"info": {
"description": "This is an API for accessing all sorts of college football data. Please note that API keys should be supplied with \"Bearer \" prepended (e.g. \"Bearer your_key\"). API keys can be acquired from the CollegeFootballData.com website.",
"version": "4.5.2",
"version": "4.6.0",
"title": "College Football Data API",
"contact": {
"email": "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion swagger.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
swagger: "2.0"
info:
description: This is an API for accessing all sorts of college football data. Please note that API keys should be supplied with "Bearer " prepended (e.g. "Bearer your_key"). API keys can be acquired from the CollegeFootballData.com website.
version: 4.5.2
version: 4.6.0
title: College Football Data API
contact:
email: [email protected]
Expand Down

0 comments on commit 676fc44

Please sign in to comment.