public IActionResult SortAndFilter(string[] genresIds, string[] featuresIds, string[] pricesIds, string[] sortsIds, string searchStr) { searchStr = searchStr is null ? "" : searchStr; if (!string.IsNullOrEmpty(searchStr)) { ViewBag.isRedirectedFromOtherPageBySearchBtn = true; } IEnumerable <Game> foundGames = gamesTable.SearchGames(searchStr); // construct all filters by data received from page GameFilter?filter = new GameFilter { Genres = genresIds.Select(id => genresTable.GetGenreByUrlId(id)).ToList() !, Features = featuresIds.Select(id => featuresTable.GetFeatureByUrlId(id)).ToList() ! }; if (filter.Genres.Count == 0 && filter.Features.Count == 0) { filter = null; } // construct all categories by data received from page List <PriceCategory> categories = pricesIds.Select( id => priceCategoriesTable.GetCategoryByUrlId(id) ).ToList() !; // get sort type by data received from page SortingTypes sortType = HomeViewModel.SortingItems[sortsIds[0]].Value; // get filtered and sorted games IEnumerable <Game> filteredAndSortedGames = gamesTable .GetGamesByFilter(filter !, categories) .SortBy(sortType); IEnumerable <Game> result; // to avaoid multiple enumeration IEnumerable <Game> foundAsList = foundGames as Game[] ?? foundGames.ToArray(); IEnumerable <Game> filteredAndSortedAsList = filteredAndSortedGames as Game[] ?? filteredAndSortedGames.ToArray(); // find intersection between found games and filtered games result = filteredAndSortedAsList.Where( g => foundAsList.FirstOrDefault(fg => fg.Id == g.Id) != null ); IEnumerable <GameCard> gameCards = CreateGameCards(result); return(PartialView("_GamesListPartial", gameCards)); } }
public IActionResult SaveAddedGame(AddGameModel data) { try { List <Genre> genres = data.Genres.Select(g => genresTable.GetGenreByUrlId(g) !).ToList(); List <Feature> features = data.Features.Select(f => featuresTable.GetFeatureByUrlId(f) !).ToList(); var gameFilter = new GameFilter { Features = features, Genres = genres }; var gameMedia = new GameMedia { Screenshots = data.Screenshots, Trailers = data.Trailers }; var gameSystemRequirements = new GameSystemRequirement { MinCpu = data.MinCpu, MaxCpu = data.MaxCpu, MinRamMB = data.MinRamMB, MaxRamMB = data.MaxRamMB, MinGpu = data.MinGpu, MaxGpu = data.MaxGpu, DiskSpaceMB = data.DiskSpaceMB, DirectX = data.DirectX, SupportedOses = data.SupportedOses, ExtraInfo = data.ExtraInfo == null ? "" : data.ExtraInfo, SupportedTextLanguages = data.SupportedTextLanguages, SupportedVoiceLanguages = data.SupportedVoiceLanguages }; var discount = float.Parse(data.Discount.Replace('.', ',')); DateTime discountEndDate = discount != 0 && !string.IsNullOrEmpty(data.DiscountEndDate) ? Convert.ToDateTime(data.DiscountEndDate) : DateTime.MinValue; var releaseDate = Convert.ToDateTime(data.ReleaseDate); var gameInfo = new GameInfo { Cover = data.Cover, Price = data.Price, Discount = discount, DiscountEndDate = discountEndDate, ReleaseDate = releaseDate, Rating = data.Rating, ShortDescription = data.ShortDescription, DetailedDescription = data.DetailedDescription, LegalInfo = data.LegalInfo }; var game = new Game { Name = data.Name, Developer = data.Developer, GameUrl = data.GameUrl, GameInfo = gameInfo, GameSystemRequirements = gameSystemRequirements, GameMedia = gameMedia, GameFilter = gameFilter }; var success = gamesTable.AddGame(game); if (!string.IsNullOrEmpty(success)) { return(Content(success)); } } catch (Exception e) { return(Content($"{e.Message}\n{e.StackTrace}")); } return(Content("")); }