コード例 #1
0
        private async void Submit_add_representative_Click(object sender, RoutedEventArgs e)
        {
            RepresentativeModel tempRepresentative = new RepresentativeModel(name.Text, surname.Text, Convert.ToInt32(contactNumber.Text), email.Text, Convert.ToInt32((companyId.SelectedItem as Models.ComboBoxItem).Value));
            await RepresentativeProcessor.UploadRepresentative(tempRepresentative);

            this.NavigationService.Navigate(new CompaniesPage());
        }
コード例 #2
0
        public TopRepresentativesModel GetTopRepresentatives(int take, int parliamentId)
        {
            var result = new TopRepresentativesModel
            {
                MostActive   = new List <RepresentativeModel>(),
                MostInactive = new List <RepresentativeModel>()
            };

            using (var context = ApplicationDbContext.Create())
            {
                var representatives = context.ParliamentHouses
                                      .Where(x => x.ParliamentID == parliamentId)
                                      .SelectMany(x => x.Representatives)
                                      .Include(x => x.Party)
                                      .ToList();

                var representativeIds = representatives.Select(x => x.RepresentativeID).ToList();


                var questions = GetRepresentativeQuestionCount(context, representativeIds);

                var answers = GetRepresentativeAnswerCount(context, representativeIds);


                var representativeModels = new List <RepresentativeModel>();
                foreach (var rep in representatives)
                {
                    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);
                    representativeModels.Add(repModel);
                }

                result.MostActive = representativeModels
                                    .OrderByDescending(x => x.PercentageAnswered)
                                    .ThenByDescending(x => x.TotalQuestions)
                                    .Take(take)
                                    .ToList();

                result.MostInactive = representativeModels
                                      .OrderBy(x => x.PercentageAnswered)
                                      .ThenByDescending(x => x.TotalQuestions)
                                      .Take(take)
                                      .ToList();

                return(result);
            }
        }
コード例 #3
0
        private static void CalculateFlagsForRepresentative(RepresentativeModel repModel)
        {
            repModel.PercentageAnswered = repModel.TotalAnswers == 0 || repModel.TotalQuestions == 0 ? 0 :
                                          Infrastructure.Math.Percentage(repModel.TotalAnswers, repModel.TotalQuestions);

            if (repModel.TotalQuestions == 0)
            {
                repModel.Gray = true;
                return;
            }

            repModel.Green  = repModel.PercentageAnswered > 66;
            repModel.Red    = repModel.PercentageAnswered < 33;
            repModel.Yellow = !repModel.Green && !repModel.Red;
        }
コード例 #4
0
        private void loadRepOnCombo()
        {
            conDB       = new ConnectionDB();
            queryString = "SELECT ID, name, telephonenumber FROM dbjuiceacct.tblrepresentative " +
                          "WHERE isDeleted = 0";
            RepresentativeModel repp = new RepresentativeModel();

            cmbRep.Items.Clear();
            MySqlDataReader reader = conDB.getSelectConnection(queryString, null);

            while (reader.Read())
            {
                repp.ID              = reader["ID"].ToString();
                repp.Name            = reader["name"].ToString();
                repp.TelephoneNumber = reader["telephonenumber"].ToString();
                cmbRep.Items.Add(repp);
                repp = new RepresentativeModel();
            }
            conDB.closeConnection();
        }
コード例 #5
0
        public static async Task UploadRepresentative(RepresentativeModel content)
        {
            if (content.name != null && content.surname != null && content.email != null && content.companyId != 0 && content.phoneNumber != 0)
            {
                string url      = "api/Representative";
                var    response = await ApiHelper.ApiClient.PostAsJsonAsync(url, content);

                var responseString = response.Content.ReadAsStringAsync().Result;
                if (response.IsSuccessStatusCode)
                {
                    await LoadRepresentatives();
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
            else
            {
            }
        }
コード例 #6
0
        public ActionResult Representative(string pCode, int?repID)
        {
            if (repID == null)
            {
                return(HttpNotFound("No such Representative Found"));
            }

            RepresentativeService service = new RepresentativeService();
            RepresentativeModel   model   = service.GetRepresentative(repID.Value, User.Identity.GetUserId());

            if (model == null)
            {
                return(HttpNotFound("No such Representative Found"));
            }

            if (pCode == null || pCode != model.Representative.ParliamentHouse.Parliament.Code)
            {
                return(RedirectToRoute(Resources.Routes.Representatives_Representative,
                                       new { pCode = model.Representative.ParliamentHouse.Parliament.Code, repID }));
            }

            return(View("Representative", model));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        public RepresentativeModel GetRepresentative(int representativeID, string userId = null)
        {
            Dictionary <int, bool> userLikedQuestions = null;
            Dictionary <int, bool> userLikedAnswers   = null;

            var auService = new AnonymousUserService();

            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var result = new RepresentativeModel
                {
                    Laws = new List <RepresentativeLawModel>()
                };

                var representative = context.Representatives
                                     .Where(x => x.RepresentativeID == representativeID)
                                     .Include(x => x.Party)
                                     .Include("ParliamentHouse.Parliament.ParliamentHouses")
                                     .Include(x => x.ExternalLinks)
                                     .Include(x => x.Assignments)
                                     .FirstOrDefault();

                if (representative == null)
                {
                    return(null);
                }

                var questions = context.UserRepresentativeQuestions
                                .Where(x => x.RepresentativeID == representativeID)
                                .Where(x => x.Question.Verified)
                                .GroupBy(x => new
                {
                    RepID        = x.RepresentativeID,
                    QuestionID   = x.QuestionID,
                    QuestionText = x.Question.Text,
                    LawID        = x.Question.LawID,
                    AskedTimeUtc = x.Question.CreateTimeUtc
                })
                                .Select(g => new QuestionHelper
                {
                    QuestionID   = g.Key.QuestionID,
                    QuestionText = g.Key.QuestionText,
                    RepID        = g.Key.RepID,
                    LawID        = g.Key.LawID,
                    AskedTimeUtc = g.Key.AskedTimeUtc,
                    Count        = g.Count()
                })
                                .ToList();

                var lawIDs      = questions.Select(x => x.LawID).ToList();
                var questionIDs = questions.Select(x => x.QuestionID).ToList();

                var laws = context.Laws
                           .Where(x => lawIDs.Contains(x.LawID))
                           .ToList();

                var answers = context.Answers
                              .Where(a => a.RepresentativeID == representativeID)
                              .Where(x => questionIDs.Contains(x.QuestionID))
                              .ToList();

                var answerIDs = answers.Select(x => x.AnswerID).ToList();

                var lawVotingResults = context.LawVotes
                                       .Where(x => lawIDs.Contains(x.LawID))
                                       .GroupBy(x => x.Vote)
                                       .Select(g => new { Key = g.Key, Count = g.Count() })
                                       .ToList();

                var questionLikeCounts = context.QuestionLikes
                                         .Where(x => questionIDs.Contains(x.QuestionID))
                                         .GroupBy(x => new { x.QuestionID, x.Vote })
                                         .Select(g => new LikeHelper {
                    ObjectID = g.Key.QuestionID, Vote = g.Key.Vote, Count = g.Count()
                })
                                         .ToList();

                var answerLikesCounts = context.AnswerLikes
                                        .Where(x => answerIDs.Contains(x.AnswerID))
                                        .GroupBy(x => new { x.AnswerID, x.Vote })
                                        .Select(g => new LikeHelper {
                    ObjectID = g.Key.AnswerID, Vote = g.Key.Vote, Count = g.Count()
                })
                                        .ToList();

                if (userId != null)
                {
                    userLikedAnswers = context.AnswerLikes
                                       .Where(x => x.ApplicationUserID == userId)
                                       .Where(x => x.Answer.RepresentativeID == representativeID)
                                       .ToDictionary(k => k.AnswerID, v => v.Vote);

                    userLikedQuestions = context.QuestionLikes
                                         .Where(x => x.Question.UserRepresentativeQuestions.Select(y => y.RepresentativeID).Contains(representativeID))
                                         .Where(x => x.ApplicationUserID == userId)
                                         .ToDictionary(k => k.QuestionID, v => v.Vote);
                }
                else
                {
                    userLikedQuestions = auService.GetUserQuestionLikes();
                    userLikedAnswers   = auService.GetUserAnswerLikes();
                }

                result.TotalQuestions = questionIDs.Distinct().Count();
                result.TotalAnswers   = answerIDs.Distinct().Count();
                CalculateFlagsForRepresentative(result);

                // local vars
                List <JavnaRasprava.WEB.Models.Representative.RepresentativeQuestionModel> questionModelList = null;
                IEnumerable <QuestionHelper> questionsToIterate = null;

                // Process laws to get questions and answers on laws
                foreach (var law in laws)
                {
                    questionModelList = new List <Models.Representative.RepresentativeQuestionModel>();
                    var lawModel = new RepresentativeLawModel
                    {
                        ID        = law.LawID,
                        Title     = law.Title,
                        Questions = new List <Models.Representative.RepresentativeQuestionModel>()
                    };
                    var unsafeResult = lawVotingResults.Where(x => x.Key.HasValue && x.Key.Value).FirstOrDefault();
                    lawModel.VotesUp = unsafeResult == null ? 0 : unsafeResult.Count;

                    unsafeResult       = lawVotingResults.Where(x => x.Key.HasValue && !x.Key.Value).FirstOrDefault();
                    lawModel.VotesDown = unsafeResult == null ? 0 : unsafeResult.Count;

                    lawModel.VotesDownPercentage = Infrastructure.Math.Percentage(lawModel.VotesDown, lawModel.VotesDown + lawModel.VotesUp);
                    lawModel.VotesUpPercentage   = Infrastructure.Math.Percentage(lawModel.VotesUp, lawModel.VotesDown + lawModel.VotesUp);

                    questionsToIterate = questions.Where(x => x.LawID == law.LawID);
                    questionModelList  = PopulateQuestionsModelInternal(questionsToIterate, answers, questionLikeCounts, answerLikesCounts, userLikedQuestions, userLikedAnswers);

                    lawModel.Questions.AddRange(questionModelList);
                    lawModel.LatestAnswerTime = questionModelList.First().AnswerTime;
                    result.Laws.Add(lawModel);
                }

                // process questions asked directly to rep
                questionsToIterate = questions.Where(x => x.LawID == null);
                questionModelList  = PopulateQuestionsModelInternal(questionsToIterate, answers, questionLikeCounts, answerLikesCounts, userLikedQuestions, userLikedAnswers);
                result.Questions   = new List <Models.Representative.RepresentativeQuestionModel>(questionModelList.OrderByDescending(x => x.AnswerTime));

                result.Representative          = representative;
                result.IsSingleHouseParliament = representative.ParliamentHouse.Parliament.ParliamentHouses.Count() == 1;
                result.Laws = result.Laws.OrderByDescending(x => x.LatestAnswerTime).ToList();

                return(result);
            }
        }