public async Task <bool> AddOrUpdateRosterAsync(FantasyRoster roster) { try { FantasyRoster?dbRoster = null; dbRoster = _dbContext.FantasyRosters .Where(r => r.OwnerId == roster.OwnerId) .SingleOrDefault(); if (dbRoster == null) { _dbContext.FantasyRosters.Add(roster); } else { dbRoster = roster; _dbContext.FantasyRosters.Update(dbRoster); } int changes = await _dbContext.SaveChangesAsync(); return(changes > 0); } catch { return(false); } }
public float GetRosterPointsForWeek(FantasyRoster roster, int week) { float fantasyScore = roster.AsPlayersEnumerable() // for all players in the roster // calculate how many points the individual players earned .Select(player => GetFantasyPointsForNflPlayerDuringWeek(player, week)) // and add up all the points across all the players .Sum(); return(fantasyScore); }
public async Task <IActionResult> Build(BuildFantasyRosterViewModel model) { // TODO: this method has some awful repeated code, rebuilding the // viewmodel in the error condition which hits the database a "lot". // i don't yet know if it can be improved. var currentUser = await _userManager.GetUserAsync(User); if (currentUser == null) { return(Challenge()); } if (model.RunningBack1Id == model.RunningBack2Id) { ModelState.AddModelError(String.Empty, "You can't have two of the same running backs."); } if (model.WideReceiver1Id == model.WideReceiver2Id) { ModelState.AddModelError(String.Empty, "You can't have two of the same wide receivers."); } if (ModelState.ErrorCount != 0) { model.Quarterbacks = await _nflPlayerService.GetQuarterbacks().ToListAsync(); model.RunningBacks = await _nflPlayerService.GetRunningBacks().ToListAsync(); model.TightEnds = await _nflPlayerService.GetTightEnds().ToListAsync(); model.WideReceivers = await _nflPlayerService.GetWideReceivers().ToListAsync(); return(View(model)); } var qb = await _nflPlayerService.GetQuarterbackByIdAsync(model.QuarterbackId); var rb1 = await _nflPlayerService.GetRunningBackByIdAsync(model.RunningBack1Id); var rb2 = await _nflPlayerService.GetRunningBackByIdAsync(model.RunningBack2Id); var te = await _nflPlayerService.GetTightEndByIdAsync(model.TightEndId); var wr1 = await _nflPlayerService.GetWideReceiverByIdAsync(model.WideReceiver1Id); var wr2 = await _nflPlayerService.GetWideReceiverByIdAsync(model.WideReceiver2Id); if (qb == null || rb1 == null || rb2 == null || te == null || wr1 == null || wr2 == null) { ModelState.AddModelError(String.Empty, "there was an error with one or more submitted player ids."); model.Quarterbacks = await _nflPlayerService.GetQuarterbacks().ToListAsync(); model.RunningBacks = await _nflPlayerService.GetRunningBacks().ToListAsync(); model.TightEnds = await _nflPlayerService.GetTightEnds().ToListAsync(); model.WideReceivers = await _nflPlayerService.GetWideReceivers().ToListAsync(); return(View(model)); } FantasyRoster roster = new FantasyRoster { Owner = currentUser, OwnerId = currentUser.Id, QuarterbackId = model.QuarterbackId, Quarterback = qb, RunningBack1Id = model.RunningBack1Id, RunningBack1 = rb1, RunningBack2Id = model.RunningBack2Id, RunningBack2 = rb2, TightEndId = model.TightEndId, TightEnd = te, WideReceiver1Id = model.WideReceiver1Id, WideReceiver1 = wr1, WideReceiver2Id = model.WideReceiver2Id, WideReceiver2 = wr2 }; bool success = await _rosterService.AddOrUpdateRosterAsync(roster); return(RedirectToAction("Index")); }