Example #1
0
        public async Task <IActionResult> Get(int?page, int?pageSize)
        {
            PaginationSet <WalkViewModel> pagedSet = new PaginationSet <WalkViewModel>();

            try
            {
                //if (_signInManager.IsSignedIn(User))
                if (true)
                {
                    int currentPage     = page.Value;
                    int currentPageSize = pageSize.Value;

                    List <Walk> _walks      = null;
                    int         _totalWalks = new int();

                    //if (await _authorizationService.AuthorizeAsync(User, "AdminOnly"))
                    if (true)
                    {
                        _walks = _walkRepository
                                 .AllIncluding(w => w.WalkSights)
                                 .OrderBy(w => w.Id)
                                 .Skip(currentPage * currentPageSize)
                                 .Take(currentPageSize)
                                 .ToList();

                        _totalWalks = _walkRepository.GetAll().Count();
                    }
                    else
                    {
                        _walks = _walkRepository
                                 .FindBy(w => w.UserId == _userManager.GetUserId(User))
                                 .OrderBy(w => w.Id)
                                 .Skip(currentPage * currentPageSize)
                                 .Take(currentPageSize)
                                 .ToList();

                        _totalWalks = _walks.Count();
                    }

                    IEnumerable <WalkViewModel> _walksVM = Mapper.Map <IEnumerable <Walk>, IEnumerable <WalkViewModel> >(_walks);

                    pagedSet = new PaginationSet <WalkViewModel>()
                    {
                        Page       = currentPage,
                        TotalCount = _totalWalks,
                        TotalPages = (int)Math.Ceiling((decimal)_totalWalks / currentPageSize),
                        Items      = _walksVM
                    };
                }
                else
                {
                    CodeResultStatus _codeResult = new CodeResultStatus(401);
                    return(new ObjectResult(_codeResult));
                }
            }
            catch (Exception ex)
            {
                _loggingRepository.Add(new Error()
                {
                    Message = ex.Message, StackTrace = ex.StackTrace, CreatedDate = DateTime.Now
                });
                _loggingRepository.Commit();
            }

            return(new ObjectResult(pagedSet));
        }