public IActionResult NavigateByGames([FromBody] GamesNavBind navigationBind) { GamesNavigationModel navigationModel = ValidationNavParams.ValidateNavigationBind(navigationBind, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } navigationModel = _gameService.NavigateByGames(navigationModel); GamesNavViewModel gamesNavViewModel = new GamesNavViewModel { Pages = navigationModel.PagesInfo, Filters = navigationModel.Filters, Games = AutoMapper.Mapper.Map <IEnumerable <BLGame>, IEnumerable <GameViewModel> >(navigationModel .SelectedGames) }; return(Ok(gamesNavViewModel)); }
public GamesNavigationModel NavigateByGames(GamesNavigationModel navigationModel) { if (navigationModel.Filters == null || navigationModel.PagesInfo == null) { throw new ArgumentException("Wrong filter or paging model"); } var filterExpression = NavigationGameService.GetFilterExpression(navigationModel.Filters, navigationModel.PagesInfo); Expression <Func <Game, object> > sortByExpression = NavigationGameService.GetExpressionForSorting(navigationModel.Filters.SortBy); List <Game> games = (_gameRepository.GetGamesByNavigation(filterExpression, sortByExpression, navigationModel.PagesInfo.ToSkip, navigationModel.PagesInfo.ToTake)).ToList(); if (navigationModel.Filters.SortBy == SortByType.PriceDesc || navigationModel.Filters.SortBy == SortByType.MostCommented) { games.Reverse(); } navigationModel.SelectedGames = ToBlEntity(games); navigationModel.PagesInfo.ItemsAmount = _gameRepository.CountAllGames(filterExpression); return(navigationModel); }
public static GamesNavigationModel ValidateNavigationBind(GamesNavBind bindModel, ModelStateDictionary modelState) { GamesFiltersModel filters = new GamesFiltersModel(); GamesPagingModel pages = new GamesPagingModel(); if (bindModel.Genres != null && bindModel.Genres.Length > 0) { foreach (var genre in bindModel.Genres) { if (genre != null) { filters.Genres.Add(genre); } else { modelState.AddModelError("Genre", "One of genre models was nullable"); } } } if (bindModel.Platforms != null && bindModel.Platforms.Length > 0) { foreach (var platform in bindModel.Platforms) { if (platform != null) { filters.Platforms.Add(platform); } else { modelState.AddModelError("Platform", "One of platform models was nullable"); } } } if (bindModel.Publishers != null && bindModel.Publishers.Length > 0) { foreach (var publisher in bindModel.Publishers) { if (publisher != null) { filters.Publishers.Add(publisher); } else { modelState.AddModelError("Publisher", "One of publisher models was nullable"); } } } filters.SetSortingBy(bindModel.SortBy); if (filters.SortBy.ToString() == "Newest" && bindModel.SortBy != "Newest") { modelState.AddModelError("SortBy", "Chose method of sorting was not found"); } if (bindModel.PriceFrom < 0 || bindModel.PriceTo < bindModel.PriceFrom) { modelState.AddModelError("Price", "Chose price range is incorrect"); } else { filters.SetPriceRange(bindModel.PriceFrom, bindModel.PriceTo); } if (bindModel.PartOfName != null) { if (bindModel.PartOfName.Length > 2) { filters.FindByNamePart(bindModel.PartOfName); } else { modelState.AddModelError("Search by name", "Passed part of name must be more than 2 symbols"); } } if (bindModel.AmountToShow > 0) { pages.ToTake = bindModel.AmountToShow; } else { modelState.AddModelError("Capacity of shown items", "Capacity of shown items must be more than 0"); } if (bindModel.PageNumber > 0) { pages.PageNumber = bindModel.PageNumber; } else { modelState.AddModelError("Page number", "Number of the choose page must be more than 0"); } GamesNavigationModel navigationModel = new GamesNavigationModel { PagesInfo = pages, Filters = filters }; return(navigationModel); }