public RepresentativeListModel SearchForRepresentativesForParliament(Models.Representative.RepresentativeSearchModel searchModel) { using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create()) { InitializerepresentativeSearchModelInternal(context, searchModel); var results = SearchRepresentativesInternal(searchModel, context); results.Title = context.Parliaments.SingleOrDefault(x => x.ParliamentID == searchModel.ParliamentId)?.RepresentativesScreenTitle; return(results); } }
private static RepresentativeListModel SearchRepresentativesInternal(Models.Representative.RepresentativeSearchModel searchModel, ApplicationDbContext context) { var parliament = context .Parliaments .Where(x => x.ParliamentID == searchModel.ParliamentId) .Include(x => x.ParliamentHouses.Select(ph => ph.Representatives.Select(y => y.Party))) .FirstOrDefault(); if (parliament == null) { return(null); } var allRepresentativeIds = parliament.ParliamentHouses .SelectMany(x => x.Representatives) .ToList() .Where(x => searchModel.SearchName == null || (x.FirstName + " " + x.LastName).ToLowerInvariant().Contains(searchModel.SearchName.ToLowerInvariant()) || (x.LastName + " " + x.FirstName).ToLowerInvariant().Contains(searchModel.SearchName.ToLowerInvariant())) .Where(x => searchModel.SelectedParty == null || x.PartyID == searchModel.SelectedParty) .Select(x => x.RepresentativeID).ToList(); var questions = GetRepresentativeQuestionCount(context, allRepresentativeIds); var answers = GetRepresentativeAnswerCount(context, allRepresentativeIds); var result = new RepresentativeListModel { SearchModel = searchModel, ParliamentHouses = new List <ParliamentHouseModel>(), ParliamentName = parliament.Name }; foreach (var house in parliament.ParliamentHouses) { var houseModel = new ParliamentHouseModel { Name = house.Name, ParliamentHouseID = house.ParliamentHouseID, }; var representatives = new List <RepresentativeModel>(); foreach (var rep in house.Representatives.Where(x => allRepresentativeIds.Contains(x.RepresentativeID))) { var repModel = new RepresentativeModel { Representative = rep, TotalAnswers = answers.ContainsKey(rep.RepresentativeID) ? answers[rep.RepresentativeID] : 0, TotalQuestions = questions.ContainsKey(rep.RepresentativeID) ? questions[rep.RepresentativeID] : 0 }; CalculateFlagsForRepresentative(repModel); representatives.Add(repModel); } switch (searchModel.SortOrder) { case Models.Representative.SortOrder.MostQuestions: houseModel.Representatives = representatives.OrderByDescending(x => x.TotalQuestions).ToList(); break; case Models.Representative.SortOrder.MostAnswers: case Models.Representative.SortOrder.None: default: houseModel.Representatives = representatives .OrderByDescending(x => x.PercentageAnswered) .ThenByDescending(x => x.TotalQuestions) .ToList(); break; } result.ParliamentHouses.Add(houseModel); } return(result); }
private static void InitializerepresentativeSearchModelInternal(ApplicationDbContext context, Models.Representative.RepresentativeSearchModel searchModel) { searchModel.Parties = context.ParliamentHouses .Where(x => x.ParliamentID == searchModel.ParliamentId) .SelectMany(x => x.Representatives) .Select(x => x.Party) .Distinct() .Select(x => new PartyModel { FullName = x.FullName, Name = x.Name, PartyID = x.PartyID }) .ToList(); }