public async Task <IActionResult> CreateInitiative(HealthInitiativeDto model) { var dbEntity = new HealthInitiative { Id = model.Id, Name = model.Name, TotalWeightLossGoal = model.TotalWeightLossGoal, HealthParticipants = model.Participants?.Select(x => new HealthParticipant { IndividualWeightLossGoal = x.IndividualWeightLossGoal, Name = x.Name, StartingWeight = x.StartingWeight }).ToList(), StartDateTime = model.StartDateTime, EndDateTime = model.EndDateTime }; await _repo.InsertHealthInitiativeAsync(dbEntity); return(Ok(dbEntity.Id)); }
private List <LeaderboardParticipantDto> TallyPointsAndMapParticipantsToLeaderboard(HealthInitiative initiative) { if (initiative.HealthParticipants != null) { var participantLogs = initiative.HealthParticipants .SelectMany(x => x.ParticipantLogs) .GroupBy(x => x.ParticipantId) .ToDictionary(x => x.Key, x => x.ToList()); var leaderboardParticipants = new List <LeaderboardParticipantDto>(); foreach (var participant in initiative.HealthParticipants) { if (participant.ParticipantLogs.Any()) { var lostWeight = 0.0m; var overallWeightLoss = participantLogs[participant.Id] .OrderByDescending(x => x.CurrentWeight) .Select(x => x.CurrentWeight) .Aggregate(participant.StartingWeight, (total, next) => { lostWeight += total - next; return(next); }); var postPoints = participantLogs[participant.Id].Sum(x => x.Points); var leaderboardParticipantDto = new LeaderboardParticipantDto { Name = participant.Name, Points = postPoints, PoundsLost = lostWeight, PercentTowardsGoal = Math.Round((lostWeight / (participant.StartingWeight - participant.IndividualWeightLossGoal)) * 100) }; leaderboardParticipants.Add(leaderboardParticipantDto); } else { var leaderboardParticipantDto = new LeaderboardParticipantDto { Name = participant.Name, Points = 0, PoundsLost = 0, PercentTowardsGoal = 0 }; leaderboardParticipants.Add(leaderboardParticipantDto); } } return(leaderboardParticipants.OrderByDescending(x => x.Points).Select((item, index) => { item.Place = index + 1; return item; }).ToList()); } return(new List <LeaderboardParticipantDto>()); }
public async Task InsertHealthInitiativeAsync(HealthInitiative initiative) { await _db.HealthInitiatives.AddAsync(initiative); await _db.SaveChangesAsync(); }