Skip to content

Commit

Permalink
new methods to refresh mlb games
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsw committed Aug 7, 2016
1 parent 650ecb1 commit dcb22a2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
19 changes: 19 additions & 0 deletions app/imports/api/games/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { LoggedInMixin } from 'meteor/tunifight:loggedin-mixin';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';

import mlbGameData from './server/mlb_game_data';
import SeasonFinder from '../seasons/finder';

export const ingestMlbSeasonData = new ValidatedMethod({
name: 'mlbGameData.ingestSeasonData',
mixins: [LoggedInMixin],
checkLoggedInError: {
error: 'notLoggedIn',
},
validate: new SimpleSchema({ }).validator(),
run() {
const season = SeasonFinder.getLatestByLeagueName('MLB');
mlbGameData.ingestSeasonData(season);
},
});
5 changes: 1 addition & 4 deletions app/imports/api/games/server/mlb_game_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ function parseGameDate(game) {

export default {
ingestSeasonData(season) {
const league = LeagueFinder.getByName('MLB');
if (! league) throw new Error('League is not found!');

const thisSeason = season || SeasonFinder.getLatestByLeague(league);
const thisSeason = season || SeasonFinder.getLatestByLeagueName('MLB');

const startDate = moment(thisSeason.startDate);
const endDate = moment(thisSeason.endDate);
Expand Down
4 changes: 3 additions & 1 deletion app/imports/api/leagues/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default {
},

getIdByName(name) {
return Leagues.findOne({ name })._id;
const league = Leagues.findOne({ name });
if (! league) throw new Error('League not found!', name);
return league._id;
},
};
8 changes: 7 additions & 1 deletion app/imports/api/seasons/finder.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Seasons } from './seasons';
import LeagueFinder from '../leagues/finder';

export default {
getByYear(league, year = (new Date()).getFullYear()) {
return Seasons.findOne({ leagueId: league._id, year });
},

getLatestByLeague(league) {
return Seasons.findOne({ leagueId: league._id }, { sort: { year: -1 } });
return this.getLatestByLeagueId(league._id);
},

getLatestByLeagueId(leagueId) {
return Seasons.findOne({ leagueId }, { sort: { year: -1 } });
},

getLatestByLeagueName(leagueName) {
const leagueId = LeagueFinder.getIdByName(leagueName);
return this.getLatestByLeagueId(leagueId);
},

getLatestCursorByLeagueId(leagueId) {
return Seasons.find({ leagueId }, { sort: { year: -1 }, limit: 1 });
},
Expand Down
1 change: 1 addition & 0 deletions app/imports/startup/server/register-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '../../api/games/server/hooks';
import '../../api/games/methods';

import '../../api/league_teams/finder';
import '../../api/league_teams/server/publications';
Expand Down

0 comments on commit dcb22a2

Please sign in to comment.