Example #1
0
        public async Task <ActionResult <PersonQuestions> > GetQuestionsAndAnswers(
            int?PageSize,
            int?PageNumber)
        {
            var questions = _context.Question.Include(q => q.Person).OrderByDescending(q => q.DateTime);
            var answers   = _context.Answer.Include(a => a.Question);
            var person    = _context.Person.FirstOrDefault(p => p.Email == User.Identity.Name);

            if (PageSize != null && PageNumber != null)
            {
                questions = (IOrderedQueryable <Question>)questions.Skip(((int)PageNumber - 1) * (int)PageSize).Take((int)PageSize);
            }

            var personQuestions = new PersonQuestions();

            try
            {
                var questionAndAnswers = questions
                                         .GroupJoin(
                    answers,
                    question => question,
                    answer => answer.Question,
                    (q, ansCollection) => new QuestionAndAnswers
                {
                    Question = q,
                    Answers  = ansCollection.OrderBy(a => a.DateTime).ToList()
                });

                return(new PersonQuestions()
                {
                    QuestionAndAnswers = await questionAndAnswers.ToListAsync(),
                    Person = person
                });
            }
            catch (Exception ex)
            {
                return(new PersonQuestions());
            }
        }
Example #2
0
        // GET: AppealCitizens
        public async Task <IActionResult> Index(
            int?PageSize,
            int?PageNumber)
        {
            PersonQuestions personQuestions = new PersonQuestions();

            string url        = "api/AppealCitizens",
                   route      = "",
                   routeCount = "";

            IConfigurationSection pageSizeListSection = Startup.Configuration.GetSection("PageSizeList");
            var pageSizeList = pageSizeListSection.AsEnumerable().Where(p => p.Value != null);

            ViewBag.PageSizeList = new SelectList(pageSizeList.OrderBy(p => p.Key)
                                                  .Select(p =>
            {
                return(new KeyValuePair <string, string>(p.Value ?? "0", p.Value));
            }), "Key", "Value");
            if (PageSize == null)
            {
                PageSize = Convert.ToInt32(pageSizeList.Min(p => p.Value));
            }
            if (PageSize == 0)
            {
                PageSize = null;
            }
            if (PageSize != null)
            {
                route += string.IsNullOrEmpty(route) ? "?" : "&";
                route += $"PageSize={PageSize.ToString()}";
                if (PageNumber == null)
                {
                    PageNumber = 1;
                }
            }
            if (PageNumber != null)
            {
                route += string.IsNullOrEmpty(route) ? "?" : "&";
                route += $"PageNumber={PageNumber.ToString()}";
            }

            HttpResponseMessage response      = await _HttpApiClient.GetAsync(url + "/GetQuestionsAndAnswers" + route),
                                responseCount = await _HttpApiClient.GetAsync(url + "/Count" + routeCount);

            if (response.IsSuccessStatusCode)
            {
                personQuestions = await response.Content.ReadAsAsync <PersonQuestions>();
            }
            int questionsCount = 0;

            if (responseCount.IsSuccessStatusCode)
            {
                questionsCount = await responseCount.Content.ReadAsAsync <int>();
            }

            ViewBag.PersonQuestions = personQuestions;
            ViewBag.PageSize        = PageSize;
            ViewBag.PageNumber      = PageNumber != null ? PageNumber : 1;
            ViewBag.TotalPages      = PageSize != null ? (int)Math.Ceiling(questionsCount / (decimal)PageSize) : 1;
            ViewBag.StartPage       = PageNumber - 5;
            ViewBag.EndPage         = PageNumber + 4;
            if (ViewBag.StartPage <= 0)
            {
                ViewBag.EndPage  -= (ViewBag.StartPage - 1);
                ViewBag.StartPage = 1;
            }
            if (ViewBag.EndPage > ViewBag.TotalPages)
            {
                ViewBag.EndPage = ViewBag.TotalPages;
                if (ViewBag.EndPage > 10)
                {
                    ViewBag.StartPage = ViewBag.EndPage - 9;
                }
            }

            return(View());
        }