/// <summary> /// Displays a search result page with all media items containing a substring "searchQuery". /// </summary> /// <param name="searchQuery">The substring to search for in media titles</param> /// <returns></returns> public ActionResult Search(string searchQuery) { var model = new SearchMediaViewModel(); if (string.IsNullOrEmpty(searchQuery)) { ViewBag.Count = 0; return(View(model)); } var movies = _unitOfWork.MovieRepository.Get().Where(m => m.Title.Contains(searchQuery)); var shows = _unitOfWork.ShowRepository.Get().Where(m => m.Title.Contains(searchQuery)); var clips = _unitOfWork.ClipRepository.Get().Where(m => m.Title.Contains(searchQuery)); model.Movies = movies.ToList(); model.Shows = shows.ToList(); model.Clips = clips.ToList(); ViewBag.Count = movies.Count() + shows.Count() + clips.Count(); return(View(model)); }
/// <summary> /// Returns a list of all translations of a certain language /// </summary> /// <param name="id">The id of the language to display</param> /// <returns></returns> public ActionResult ByLanguage(int?id) { if (id == null) { throw new MissingParameterException(); } var translations = _unitOfWork.TranslationRepository.Get().Where(t => t.LanguageID == id).OrderBy(t => t.Media.Title).ToList(); if (translations == null) { throw new DataNotFoundException(); } var model = new SearchMediaViewModel(); foreach (var item in translations) { string type = item.Media.GetType().BaseType.Name; switch (type) { case "Movie": model.Movies.Add(item.Media as Movie); break; case "Show": model.Shows.Add(item.Media as Show); break; case "Clip": model.Clips.Add(item.Media as Clip); break; default: throw new ApplicationException(); } } ViewBag.Language = _unitOfWork.LanguageRepository.GetByID(id); ViewBag.LanguageID = id; return(View(model)); }
// Here we test the search method of the // Home controller public void TestSearch() { // Arrange var mockUnitOfWork = new MockUnitOfWork(); for (int i = 1; i < 4; i++) { mockUnitOfWork.MovieRepository.Insert(new Movie { ID = i, Title = "the cat" + i.ToString(), }); } for (int i = 4; i < 7; i++) { mockUnitOfWork.ShowRepository.Insert(new Show { ID = i, Title = "the horse" + i.ToString(), }); } for (int i = 7; i < 10; i++) { mockUnitOfWork.ClipRepository.Insert(new Clip { ID = i, Title = "the hero" + i.ToString(), }); } // Act var controller = new HomeController(mockUnitOfWork); // Assert // this search should return 3 clips 3 shows and no movies var resultWithThe_h = controller.Search("the h"); var viewresult1 = (ViewResult)resultWithThe_h; SearchMediaViewModel model1 = viewresult1.Model as SearchMediaViewModel; Assert.IsTrue(model1.Clips.Count == 3 && model1.Shows.Count == 3 && model1.Movies.Count == 0); for (int i = 0; i < model1.Clips.Count - 1; i++) { Assert.IsTrue(string.Compare(model1.Clips[i].Title, model1.Clips[i + 1].Title) < 0); } for (int i = 0; i < model1.Shows.Count - 1; i++) { Assert.IsTrue(string.Compare(model1.Shows[i].Title, model1.Shows[i + 1].Title) < 0); } // this search should only return 3 movies var resultWithThe_c = controller.Search("the c"); var viewresult2 = (ViewResult)resultWithThe_c; SearchMediaViewModel model2 = viewresult2.Model as SearchMediaViewModel; Assert.IsTrue(model2.Movies.Count == 3 && model2.Shows.Count == 0 && model2.Clips.Count == 0); for (int i = 0; i < model1.Movies.Count - 1; i++) { Assert.IsTrue(string.Compare(model1.Movies[i].Title, model1.Movies[i + 1].Title) < 0); } // this search should return no results var resultWithG = controller.Search("G"); var viewresult3 = (ViewResult)resultWithG; SearchMediaViewModel model3 = viewresult3.Model as SearchMediaViewModel; Assert.IsTrue(model3.Movies.Count == 0 && model3.Shows.Count == 0 && model3.Clips.Count == 0); // this should also return no results var resultWithNull = controller.Search(""); var viewresult4 = (ViewResult)resultWithNull; SearchMediaViewModel model4 = viewresult4.Model as SearchMediaViewModel; Assert.IsTrue(model4.Movies.Count == 0 && model4.Shows.Count == 0 && model4.Clips.Count == 0); }