예제 #1
0
        public ActionResult GetLawQuestionModel(int lawId)
        {
            QuestionsService    service = new QuestionsService();
            AskLawQuestionModel model   = service.GetQuestionsModel(lawId, User.Identity.GetUserId());

            return(PartialView("Representatives/_AskRepresentatives", model));
        }
예제 #2
0
        public ActionResult AskRepresentative(int lawId, AskLawQuestionModel model)
        {
            TryUpdateModel(model);

            QuestionsService service = new QuestionsService();

            service.PostQuestion(model, User.Identity.GetUserId());
            return(PartialView("Representatives/_AskRepresentativesSuccess"));
        }
예제 #3
0
        public AskLawQuestionModel GetQuestionsModel(int lawID, string userID)
        {
            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var result = new AskLawQuestionModel
                {
                    LawID = lawID,
                    OtherRepresentatives     = new List <AskRepresentativeModel>(),
                    SuggestedRepresentatives = new List <AskRepresentativeModel>(),
                    Questions = new List <AskPredefinedQuestionsModel>(),
                };

                var user = context.Users.Where(x => x.Id == userID).FirstOrDefault();
                if (user == null)
                {
                    return(null);
                }

                var law = context.Laws
                          .Where(x => x.LawID == lawID)
                          .Include(x => x.LawRepresentativeAssociations.Select(lra => lra.Representative.Party))
                          .FirstOrDefault();
                if (law == null)
                {
                    return(null);
                }

                foreach (var ra in law.LawRepresentativeAssociations)
                {
                    result.SuggestedRepresentatives.Add(
                        new AskRepresentativeModel
                    {
                        ID                = ra.RepresentativeID,
                        FullName          = ra.Representative.DisplayName,
                        PartyName         = ra.Representative.Party.Name,
                        IsSuggested       = true,
                        Reason            = ra.Reason,
                        ImageRelativePath = ra.Representative.ImageRelativePath
                    });
                }

                var suggestedRepIDList = law.LawRepresentativeAssociations.Select(x => x.RepresentativeID).ToList();
                result.OtherRepresentatives = context.Representatives
                                              .Include(x => x.Party)
                                              .Where(x => x.ParliamentHouse.ParliamentID == law.ParliamentID && !suggestedRepIDList.Contains(x.RepresentativeID))
                                              .ToList()
                                              .Select(x => new AskRepresentativeModel
                {
                    ID                = x.RepresentativeID,
                    IsSuggested       = false,
                    FullName          = x.DisplayName,
                    PartyName         = x.Party.Name,
                    ImageRelativePath = x.ImageRelativePath
                })
                                              .ToList();

                result.Questions = context.Questions
                                   .Where(x => x.LawID == lawID && x.IsSuggested)
                                   .Select(x => new AskPredefinedQuestionsModel
                {
                    ID   = x.QuestionID,
                    Text = x.Text
                })
                                   .ToList();

                return(result);
            }
        }
예제 #4
0
        public void PostQuestion(AskLawQuestionModel model, string userID)
        {
            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var user = context.Users.Where(x => x.Id == userID).FirstOrDefault();
                if (user == null)
                {
                    return;
                }

                var law = context.Laws.Where(x => x.LawID == model.LawID).FirstOrDefault();
                if (law == null)
                {
                    return;
                }

                List <int> selectedPredefinedQuestionIDs = new List <int>();
                if (model.Questions != null)
                {
                    selectedPredefinedQuestionIDs = model.Questions.Where(x => x.IsSelected).Select(x => x.ID).ToList();
                }

                var predefinedQuestions = context.Questions.Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID));

                var selectedRepresentativeIDs = model.SuggestedRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList();
                selectedRepresentativeIDs.AddRange(model.OtherRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList());

                if (selectedRepresentativeIDs.Count == 0)
                {
                    return;
                }

                // If user asked any of given questions
                var existingUserQuestions = context.UserRepresentativeQuestions
                                            .Where(x => x.ApplicationUserID == user.Id &&
                                                   selectedPredefinedQuestionIDs.Contains(x.QuestionID) &&
                                                   selectedRepresentativeIDs.Contains(x.RepresentativeID))
                                            .ToList();

                // should check if any of users asked same question to skip creating token
                var existingQuestions = context.UserRepresentativeQuestions
                                        .Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID) &&
                                               selectedRepresentativeIDs.Contains(x.RepresentativeID))
                                        .ToList();

                // Prepare custom question if needed
                DomainModels.Question customQuestion = null;

                if (!string.IsNullOrWhiteSpace(model.Text))
                {
                    customQuestion = new DomainModels.Question
                    {
                        LawID         = law.LawID,
                        Text          = model.Text,
                        IsSuggested   = false,
                        CreateTimeUtc = DateTime.UtcNow
                    };
                    context.Questions.Add(customQuestion);
                }

                var         newQuestions = new List <DomainModels.UserRepresentativeQuestion>();
                AnswerToken answerToken  = null;
                foreach (var rep in selectedRepresentativeIDs.Distinct())
                {
                    foreach (var question in selectedPredefinedQuestionIDs)
                    {
                        answerToken = null;
                        if (existingUserQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any())
                        {
                            continue;
                        }

                        if (!existingQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any())
                        {
                            answerToken = new AnswerToken
                            {
                                RepresentativeID = rep,
                                QuestionID       = question,
                                Token            = Guid.NewGuid(),
                                CreateTimeUtc    = DateTime.UtcNow
                            };
                            context.AnswerTokens.Add(answerToken);
                        }

                        var newPredefinedQuestion = new DomainModels.UserRepresentativeQuestion
                        {
                            ApplicationUserID = user.Id,
                            QuestionID        = question,
                            RepresentativeID  = rep,
                            CreateTimeUtc     = DateTime.UtcNow,
                            AnswerToken       = answerToken
                        };
                        context.UserRepresentativeQuestions.Add(newPredefinedQuestion);
                        // Notifications are being processed only for predefined questions at this point.
                        if (answerToken != null)
                        {
                            newQuestions.Add(newPredefinedQuestion);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(model.Text))
                    {
                        answerToken = new AnswerToken
                        {
                            RepresentativeID = rep,
                            Question         = customQuestion,
                            Token            = Guid.NewGuid(),
                            CreateTimeUtc    = DateTime.UtcNow
                        };
                        context.AnswerTokens.Add(answerToken);

                        context.UserRepresentativeQuestions.Add(
                            new DomainModels.UserRepresentativeQuestion
                        {
                            ApplicationUserID = user.Id,
                            RepresentativeID  = rep,
                            CreateTimeUtc     = DateTime.UtcNow,
                            Question          = customQuestion,
                            AnswerToken       = answerToken
                        });
                    }
                }

                context.SaveChanges();
                new NotificationService().ProcessNewQuestions(newQuestions);
            }
        }