public async Task <IActionResult> AddMatch(AddMatchModel addMatchModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var winnerCompetitor = await _db.Competitors.FindAsync(addMatchModel.WinnerCompetitor.CompetitorId); if (winnerCompetitor == null) { return(BadRequest("Winner competitor does not exist")); } var losingCompetitor = await _db.Competitors.FindAsync(addMatchModel.LosingCompetitor.CompetitorId); if (losingCompetitor == null) { return(BadRequest("Losing competitor does not exist")); } winnerCompetitor.Wins += 1; winnerCompetitor.Wallet += addMatchModel.WinnerCompetitor.TrickShots + (addMatchModel.WinnerCompetitor.CalledTrickShots * 5) - addMatchModel.WinnerCompetitor.Fouls; losingCompetitor.Losses += 1; losingCompetitor.Wallet += addMatchModel.LosingCompetitor.TrickShots + (addMatchModel.LosingCompetitor.CalledTrickShots * 5) - addMatchModel.LosingCompetitor.Fouls; var currentFoulPot = (await _db.Matches.OrderByDescending(x => x.Timestamp).FirstOrDefaultAsync())?.Stake; var newMatch = new Match() { Stake = addMatchModel.Stake, CurrentFoulPot = addMatchModel.LosingCompetitor.Fouls + addMatchModel.WinnerCompetitor.Fouls }; _db.Competitors.Update(winnerCompetitor); _db.Competitors.Update(losingCompetitor); await _db.AddAsync(newMatch); await _db.SaveChangesAsync(); var winnerMatchCompetitor = new MatchCompetitor() { CompetitorId = addMatchModel.WinnerCompetitor.CompetitorId, MatchId = newMatch.Id }; var loserMatchCompetitor = new MatchCompetitor() { CompetitorId = addMatchModel.LosingCompetitor.CompetitorId, MatchId = newMatch.Id }; await _db.AddRangeAsync(new { winnerMatchCompetitor, loserMatchCompetitor }); await _db.SaveChangesAsync(); return(Ok(newMatch.Id)); }
public static List <MatchModel> GetMatchModel(List <MatchCompetitor> mcs) { List <Models.MatchModel> model = new List <Models.MatchModel>(); List <Models.MatchModel> competitors = new List <MatchModel>(); List <Models.MatchModel> divisions = new List <MatchModel>(); //Build models //Grouping foreach (MatchCompetitor obj in mcs) { MatchModel mm = new MatchModel(); if (divisions.Any(m => m.MatchTypeId == obj.Match.MatchType.MatchTypeId && m.MatchId == obj.Match.MatchId)) { continue; } mm.EventId = obj.Match.Event.EventId; mm.DivisionId = obj.Match.Division.DivisionId; mm.MatchTypeId = obj.Match.MatchType.MatchTypeId; mm.MatchId = obj.Match.MatchId; mm.SubDivisionId = obj.Match.SubDivisionId; mm.MatchTypeName = GetMatchTypeDisplayName(obj.Match.MatchType, LengthType.Long); mm.MatchTypeDisplayName = obj.Match.MatchType.MatchTypeDisplayName; mm.Children = new List <MatchModel>(); divisions.Add(mm); } //Details foreach (MatchCompetitor obj in mcs) { MatchModel mm = new MatchModel(); if (competitors.Any(m => m.CompetitorId == obj.Competitor.CompetitorId && m.MatchId == obj.Match.MatchId)) { continue; } mm.CompetitorId = obj.Competitor.CompetitorId; mm.MatchId = obj.Match.MatchId; mm.Age = obj.Competitor.Age; mm.DisplayName = (obj.Competitor.Person != null) ? obj.Competitor.Person.DisplayName : null; mm.DojoName = (obj.Competitor.Dojo != null) ? obj.Competitor.Dojo.Facility.FacilityName : null; mm.Gender = (obj.Competitor.Person != null) ? obj.Competitor.Person.Gender : null; mm.RankName = (obj.Competitor.Rank != null) ? obj.Competitor.Rank.RankName : null; mm.Weight = obj.Competitor.Weight; mm.Height = obj.Competitor.Height; mm.Children = new List <MatchModel>(); competitors.Add(mm); } //Nest the models foreach (MatchModel comp in competitors) { MatchCompetitor mc = mcs.First(m => m.Competitor.CompetitorId == (int)comp.CompetitorId && m.Match.MatchId == comp.MatchId); MatchModel div = divisions.First(m => m.MatchId == mc.Match.MatchId); MatchModel mm = new MatchModel { Age = comp.Age, CompetitorId = comp.CompetitorId, DisplayName = comp.DisplayName, DojoName = comp.DojoName, Gender = comp.Gender, RankName = comp.RankName, Weight = comp.Weight, Height = comp.Height, Children = comp.Children }; div.Children.Add(mm); } // Divs > model foreach (MatchModel div in divisions) { model.Add(div); } return(model); }