コード例 #1
0
        public async Task <IActionResult> GetAllMenus(int pageNumber, int pageSize)
        {
            try
            {
                int UserId = (Request.Headers.ContainsKey("CustomerId") ? int.Parse(HttpContext.Request.Headers["CustomerId"]) : 0);
                loggerService.LogMessage("MenuList received at endpoint : api/MenuList : UserID : " + UserId);
                var MenuList = await Task <IQueryable <MenuList> > .Run(() => business_Repo.GetMenuList());

                int count = MenuList.Count();

                // Parameter is passed from Query string if it is null then it default Value will be pageNumber:1
                int CurrentPage = pageNumber;

                // Parameter is passed from Query string if it is null then it default Value will be pageSize:20
                int PageSize = pageSize;

                // Display TotalCount to Records to User
                int TotalCount = count;

                // Calculating Totalpage by Dividing (No of Records / Pagesize)

                int TotalPages = (int)Math.Ceiling(count / (double)PageSize);

                // Returns List of Customer after applying Paging
                var items = MenuList.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();

                // if CurrentPage is greater than 1 means it has previousPage
                var previousPage = CurrentPage > 1 ? "Yes" : "No";

                // if TotalPages is greater than CurrentPage means it has nextPage
                var nextPage = CurrentPage < TotalPages ? "Yes" : "No";

                // Object which we are going to send in header
                var paginationMetadata = new
                {
                    totalCount  = TotalCount,
                    pageSize    = PageSize,
                    currentPage = CurrentPage,
                    totalPages  = TotalPages,
                    previousPage,
                    nextPage
                };
                // Returing List of Customers Collections
                if (MenuList.Count() == 0)
                {
                    return(NotFound("MenuList is empty"));
                }
                return(Ok(items));
            }
            catch (Exception ex)
            {
                loggerService.LogException(ex);
                return(this.StatusCode((int)HttpStatusCode.InternalServerError, string.Empty));
            }
        }