private async Task <List <Result> > GetResults(Models.Crew crew) { List <Models.Crew> allCrews = await _context.Crews.Include(c => c.Results) .Include("Results.TimingPoint").ToListAsync(); Models.Result previousResult = null; bool isFirst = true; Models.TimingPoint startPoint = crew.Competition.TimingPoints.First(); List <Result> results = new List <Result>(); foreach (Models.Result result in crew.Results.OrderBy(x => x.TimingPoint.Order)) { results.Add(new Result() { Id = result.TimingPointId, Name = result.TimingPoint.Name, TimeOfDay = result.TimeOfDay, SectionTime = isFirst ? null : crew.RunTime(previousResult.TimingPoint, result.TimingPoint), RunTime = isFirst ? null : crew.RunTime(startPoint, result.TimingPoint), Rank = isFirst ? String.Empty : crew.Rank(allCrews.Where(x => x.RunTime(startPoint, result.TimingPoint).HasValue) .OrderBy(x => x.RunTime(startPoint, result.TimingPoint)).ToList(), startPoint, result.TimingPoint) }); previousResult = result; isFirst = false; } return(results); }
public static Api.Resources.Crew BuildCrew(IMapper mapper, Models.Competition competition, Models.Crew modelCrew) { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (competition is null) { throw new ArgumentNullException(nameof(competition)); } if (modelCrew is null) { throw new ArgumentNullException(nameof(modelCrew)); } TimingPoint startPoint = competition.TimingPoints.First(); Api.Resources.Crew apiCrew = mapper.Map <Api.Resources.Crew>(modelCrew); foreach (Models.Result modelResult in modelCrew.Results) { Api.Resources.Result apiResult = new Api.Resources.Result() { Id = modelResult.TimingPointId, TimeOfDay = modelResult.TimeOfDay, Name = modelResult.TimingPoint.Name, RunTime = modelCrew.RunTime(startPoint.TimingPointId, modelResult.TimingPointId), SectionTime = startPoint.TimingPointId != modelResult.TimingPointId ? modelCrew.RunTime(competition.TimingPoints[competition.TimingPoints.IndexOf(modelResult.TimingPoint) - 1].TimingPointId, modelResult.TimingPointId) : null }; apiCrew.Results.Add(apiResult); } return(apiCrew); }