Add endpoint for fetching matchups & hook up to matchups interface
This commit is contained in:
24
app/controllers/Scores.controller.js
Normal file
24
app/controllers/Scores.controller.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const { Controller } = require('libflitter')
|
||||
|
||||
class ScoresController extends Controller {
|
||||
static get services() {
|
||||
return [...super.services, 'models', 'sports_data']
|
||||
}
|
||||
|
||||
async get_weekly_scores(req, res, next) {
|
||||
const Matchup = this.models.get('Matchup')
|
||||
|
||||
const current_week = await this.sports_data.current_play_week()
|
||||
const weekly_data = []
|
||||
|
||||
for ( let i = 1; i <= current_week; i += 1 ) {
|
||||
const matchups = await Matchup.find({ week_num: i })
|
||||
const api_data = await Promise.all(matchups.map(x => x.to_api()))
|
||||
weekly_data.push(api_data)
|
||||
}
|
||||
|
||||
return res.api(weekly_data)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = ScoresController
|
||||
@@ -25,6 +25,30 @@ class Matchup extends Model {
|
||||
const Team = this.models.get('Team')
|
||||
return Team.findById(this.visitor_team_id)
|
||||
}
|
||||
|
||||
async to_api() {
|
||||
const home_team = await this.home_team()
|
||||
const visitor_team = await this.visitor_team()
|
||||
|
||||
const data = {
|
||||
date: '2020-11-11', // TODO generate this in the matches patch
|
||||
team_1: home_team.team_name,
|
||||
team_1_projection: 0,
|
||||
|
||||
team_2: visitor_team.team_name,
|
||||
team_2_projection: 0,
|
||||
}
|
||||
|
||||
if ( this.complete ) {
|
||||
const winner = this.home_team_score > this.visitor_team_score ? home_team : visitor_team
|
||||
|
||||
data.winner = winner.team_name
|
||||
data.winner_score = Math.max(this.home_team_score, this.visitor_team_score)
|
||||
data.loser_score = Math.min(this.home_team_score, this.visitor_team_score)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Matchup
|
||||
|
||||
@@ -52,6 +52,8 @@ const index = {
|
||||
'/my-team/lineup': ['controller::Teams.get_my_team_current_lineup'],
|
||||
|
||||
'/draft-board/available': ['controller::DraftBoard.get_available_players'],
|
||||
|
||||
'/matchups': ['controller::Scores.get_weekly_scores'],
|
||||
},
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user