Пример #1
0
        public IActionResult Index([Bind("SearchName")] FilteringDTO filtering, SortingDTO sorting, PagingDTO paging)
        {
            ViewBag.CurrentPage  = paging.PageNumber;
            ViewBag.OrderBy      = sorting.OrderBy == "DESC" ? "ASC" : "DESC";
            ViewBag.CurrentOrder = sorting.OrderBy;
            ViewBag.CurrentSort  = sorting.SortBy;
            paging.CountItems    = MakeService.CountMakers();
            paging.PageSize      = 3;
            if (paging.PageNumber < 1)
            {
                paging.PageNumber = 1;
            }
            if (paging.HasNextPage)
            {
                ViewBag.HasNext = true;
            }
            else
            {
                ViewBag.HasNext = false;
            }
            if (paging.HasPreviousPage)
            {
                ViewBag.HasPrevious = true;
            }
            else
            {
                ViewBag.HasPrevious = false;
            }
            var filter = Mapper.Map <Filtering>(filtering);
            var sort   = Mapper.Map <Sorting>(sorting);
            var page   = Mapper.Map <Paging>(paging);
            var make   = MakeService.GetVehicleMakes(filter, sort, page);

            if (make == null)
            {
                return(NotFound(filtering.SearchName + " not Found"));
            }
            return(View(make.Select(Mapper.Map <VehicleMake, VehicleMakeDTO>)));
        }
        public IActionResult Index([Bind("SearchName")] Filtering makeFiltering, Sorting sorting, Paging paging)
        {
            if (paging.PageNumber == 1)
            {
                ViewBag.OrderBy = sorting.OrderBy == "DESC" ? "ASC" : "DESC";
                CurrentOrder    = sorting.OrderBy;
                CurrentSort     = sorting.SortBy;
            }

            if (String.IsNullOrEmpty(sorting.SortBy))
            {
                sorting.SortBy  = "Name";
                sorting.OrderBy = "ASC";
            }

            paging.TotalPages = MakeService.CountMakers();

            CurrentPage = paging.PageNumber;
            ElementsGot = paging.Elements;
            #region HasNextButton
            if ((ElementsGot + NumberOfElements) < paging.TotalPages)
            {
                ViewBag.HasNext = true;
            }
            else
            {
                ViewBag.HasNext = false;
            }
            #endregion
            #region HasPrevButton
            if (paging.HasPreviousPage)
            {
                ViewBag.HasPrevious = true;
            }
            else
            {
                ViewBag.HasPrevious = false;
            }
            #endregion
            #region Search
            if (!String.IsNullOrEmpty(makeFiltering.SearchName))
            {
                if (!MakeService.HasNameInDatabase(makeFiltering.SearchName))
                {
                    return(NotFound(makeFiltering.SearchName + " not Found"));
                }
                string countQuery = String.Format("SELECT * FROM dbo.VehicleMakers Where Name = '{0}'", makeFiltering.SearchName);
                paging.TotalPages = MakeService.GetVehicleMakes(countQuery).Count();
                ElementsGot       = 0;
                string searchQuery = String.Format("SELECT * FROM dbo.VehicleMakers Where Name = '{0}' ORDER BY {1} {2} OFFSET {3} ROWS FETCH NEXT {4} ROWS ONLY", makeFiltering.SearchName, sorting.SortBy, sorting.OrderBy, ElementsGot, NumberOfElements);
                var    searchMake  = MakeService.GetVehicleMakes(searchQuery).Select(Mapper.Map <VehicleMakeDTO>);
                #region HasNextButton
                if ((ElementsGot + NumberOfElements) < paging.TotalPages)
                {
                    ViewBag.HasNext = true;
                }
                else
                {
                    ViewBag.HasNext = false;
                }
                #endregion
                #region HasPrevButton
                if (paging.HasPreviousPage)
                {
                    ViewBag.HasPrevious = true;
                }
                else
                {
                    ViewBag.HasPrevious = false;
                }
                #endregion
                return(View(searchMake));
            }
            #endregion
            string query = String.Format("SELECT * FROM dbo.VehicleMakers ORDER BY {0} {1} OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY", sorting.SortBy, sorting.OrderBy, ElementsGot, NumberOfElements);
            var    make  = MakeService.GetVehicleMakes(query).Select(Mapper.Map <VehicleMake, VehicleMakeDTO>);
            return(View(make));
        }