public SearchTO Search(string searchString, string userName) { var films = new List <Film>(); List <FilmShortDescriptionTO> filmsModel = new List <FilmShortDescriptionTO>(); var filmsCache = ApplicationCache <Film> .GetCache(); if (searchString.ToLower().Contains("filmy z")) { // get actor name var actorName = searchString.Remove(0, 8); // get search actor id var filmIds = _filmRepository.GetActorFilmIds(actorName); // get films with mentioned above actor if (filmIds.Count > 0) { films = _filmRepository.GetMany(filmIds); } } else { var cachedFilms = filmsCache.Where(d => d.Name.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0).ToList(); var cachedFilmsByGenre = filmsCache.Where(d => d.Genre.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0).Take(30).ToList(); if (cachedFilms.Count > 0) { films = cachedFilms; // get count of search films int filmsCount = films.Count; // calculate how many films we need to download (by genre) int additionalFilmsGet = 30 - filmsCount; if (additionalFilmsGet > 0) { films.AddRange(cachedFilmsByGenre.Take(additionalFilmsGet)); } } else if (cachedFilmsByGenre.Count > 0) { films = cachedFilmsByGenre; } else { films = _listRepository.SearchFilms(searchString); } } foreach (var film in films) { filmsModel.Add(new FilmShortDescriptionTO() { Id = film.Id, Name = film.Name, OriginalName = film.OriginalName, ReleaseDate = film.ReleaseDate, Genre = film.Genre, Description = film.Description, Cover = film.Cover, FilmwebUrl = film.FilmwebUrl }); } List <ListSearchDetailsTO> listsResult = new List <ListSearchDetailsTO>(); // get lists var lists = _listRepository.SearchLists(searchString); foreach (var list in lists) { // get list films list.Films = _listRepository.GetListFilms(list.Id); // map List to ListSearchDetailsTO var singleListDetails = new ListSearchDetailsTO(); singleListDetails.Id = list.Id; singleListDetails.Name = list.Name; singleListDetails.Likes = list.Likes; singleListDetails.DisLikes = list.Dislikes; singleListDetails.Films = new List <FilmSearchDetailsTO>(); // calculate votes count for each film List <int> filmIds = new List <int>(); foreach (var film in list.Films) { filmIds.Add(film.Id); } Dictionary <int, int> votes = _listRepository.CalculateVotes(singleListDetails.Id, filmIds); foreach (var film in list.Films) { FilmSearchDetailsTO singleFilmSearchDetails = new FilmSearchDetailsTO(); singleFilmSearchDetails.Id = film.Id; singleFilmSearchDetails.Name = film.Name; singleFilmSearchDetails.OriginalName = film.OriginalName; singleFilmSearchDetails.ReleaseDate = film.ReleaseDate; singleFilmSearchDetails.Genre = film.Genre; singleFilmSearchDetails.Description = film.Description; singleFilmSearchDetails.Cover = film.Cover; singleFilmSearchDetails.FilmwebUrl = film.FilmwebUrl; if (votes.ContainsKey(film.Id)) { singleFilmSearchDetails.Votes = votes[film.Id]; } else { singleFilmSearchDetails.Votes = 0; } singleListDetails.Films.Add(singleFilmSearchDetails); } listsResult.Add(singleListDetails); } if (userName != "") { string userId = ""; //get user id userId = _authRepository.GetUserId(userName); foreach (var list in listsResult) { List <int> votedFilmsIds = new List <int>(); if (userId != "") { //check if user already voted for films votedFilmsIds = _listRepository.CheckIfFilmsAreVoted(list.Id, userId); // check if user already liked/dislikes list list.Voted = _listRepository.CheckIfListIsVoted(list.Id, userId); // check if user already added list to favourites list.IsFavourite = _authRepository.CheckIfFavouriteExists(list.Id, userId); foreach (var film in list.Films) { if (votedFilmsIds.Contains(film.Id)) { film.isVoted = true; } else { film.isVoted = false; } } } list.Films.OrderByDescending(d => d.Votes); } } listsResult.OrderByDescending(d => d.Likes); var searchResult = new SearchTO() { SearchString = searchString, Films = filmsModel, Lists = listsResult }; return(searchResult); }