public async Task <IActionResult> SearchByCategory(Guid id) { try { var userRoles = await base.GetUserRoles(); var category = await this.categoriesRepository.GetAsync(id); var articles = await this.articlesRetriever.GetCategoryArticlesAsync(id, userRoles); var viewModel = new SearchArticlesViewModel(articles); viewModel.SearchPattern = category.Name; return(View("Index", viewModel)); } catch (Exception ex) { if (base.log.IsErrorEnabled) { base.log.Error(ex); } return(base.NotFound()); } }
/// <summary> /// Get all articles matching the criterieas in the search model /// </summary> /// <param name="searchModel">The object contains the criterias for searching</param> /// <returns>IQueryable<Article></returns> private IQueryable <Article> SearchByCriterias(SearchArticlesViewModel searchModel) { var articles = Data.Articles.All(); if (searchModel.CategoryId != null) { articles = articles.Where(article => article.CateoryId == searchModel.CategoryId); //We check if the user gave us incorrect category id if (articles.Count() == 0) { CheckIfCategoryExists((int)searchModel.CategoryId); } } if (articles.Count() != 0 && searchModel.FromPrice != null) { articles = articles.Where(article => article.Price >= searchModel.FromPrice); } if (articles.Count() != 0 && searchModel.ToPrice != null) { articles = articles.Where(article => article.Price <= searchModel.ToPrice); } if (articles.Count() != 0 && !string.IsNullOrWhiteSpace(searchModel.SearchSubstring)) { articles = articles .Where(article => CurrentCulture == Culture.bg.ToString() ? article.NameBg.Contains(searchModel.SearchSubstring) : article.NameEn.Contains(searchModel.SearchSubstring)); } return(articles); }
public void SearchResults_WithWrongCategoryId_MustThrowHttpException() { //Mock articles repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock categories repository int categoryId = 50; var mockedCategories = new Mock <IRepository <Category> >(); mockedCategories.Setup(repo => repo.GetById(50)).Returns(_categories.FirstOrDefault(cat => cat.Id == categoryId)); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Categories).Returns(() => { return(mockedCategories.Object); }); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); var searchModel = new SearchArticlesViewModel { CategoryId = categoryId }; var results = articlesController.SearchResults(searchModel) as ViewResult; }
public async Task <IActionResult> Search(SearchArticlesViewModel viewModel) { try { if (viewModel == null) { viewModel = new SearchArticlesViewModel(); } if (!String.IsNullOrEmpty(viewModel.SearchPattern)) { var userRoles = await base.GetUserRoles(); var articles = await this.articlesRetriever.FindArticlesAsync(viewModel.SearchPattern, userRoles); viewModel.Articles.AddRange(articles); } return(View("Index", viewModel)); } catch (Exception ex) { if (base.log.IsErrorEnabled) { base.log.Error(ex); } return(base.NotFound()); } }
public async Task <IActionResult> Search(string slug, int page) { if (slug is null) { return(this.RedirectToAction(nameof(this.All))); } var pagination = Paginator.GetPagination(page, ArticlesPerPage); var articles = await this.articlesService .GetPaginatedByTitleKeywordsAsync <CompactArticleViewModel>( pagination.Skip, pagination.Take, slug); var model = new SearchArticlesViewModel { Articles = articles, PaginationViewModel = new PaginationViewModel( page, ArticlesPerPage, await this.articlesService.GetCountByTitleKeywordsAsync(slug), allRouteDataObject: new { slug }), Slug = slug, }; return(this.View(model)); }
public void SearchResults_WithFromPriceBiggerThanToPrice_MustReturnTheSameSearchModel() { //Mock repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); var searchModel = new SearchArticlesViewModel { FromPrice = 50, ToPrice = 20 }; var results = articlesController.SearchResults(searchModel) as ViewResult; var model = results.Model as SearchArticlesViewModel; Assert.AreSame(model, searchModel); }
public void SearchResults_OnlySearchSubstring_MustReturnOnlyArticlesWhoseNameForTheCurrentCultureContainsThisSubstring() { //Mock repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); articlesController.CurrentCulture = Culture.bg.ToString(); var searchModel = new SearchArticlesViewModel { SearchSubstring = "странно" }; var results = articlesController.SearchResults(searchModel) as ViewResult; var model = results.Model as IList <ArticleViewModel>; Assert.AreEqual(2, model.Count); foreach (var article in model) { Assert.IsTrue(article.Name.Contains(searchModel.SearchSubstring)); } }
public void SearchResults_OnlyToPRiceTwenty_MustReturnOnlyArticlesWithPriceLowerThanTwenty() { //Mock repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); var searchModel = new SearchArticlesViewModel { ToPrice = 20 }; var results = articlesController.SearchResults(searchModel) as ViewResult; var model = results.Model as IList <ArticleViewModel>; Assert.AreEqual(3, model.Count); foreach (var article in model) { Assert.IsTrue(article.Price <= searchModel.ToPrice); } }
public void SearchResults_OnlyCategoryId_MustReturnOnlyArticlesFromThisCategory() { //Mock repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); var searchModel = new SearchArticlesViewModel { CategoryId = 1 }; var results = articlesController.SearchResults(searchModel) as ViewResult; var model = results.Model as IList <ArticleViewModel>; Assert.AreEqual(2, model.Count); Assert.AreEqual(1, model[0].Id); Assert.AreEqual(4, model[1].Id); }
public async Task <IActionResult> Search(string keyWord, int?pageIndex = null) { keyWord = new HtmlSanitizer().Sanitize(keyWord); var model = new SearchArticlesViewModel { Articles = await this.articlesService.GetArticlesByKeyWordAsync(keyWord, pageIndex), KeyWord = keyWord, PageTitle = string.Format(GlobalConstants.SearchPageTitle, keyWord), NoResultsMessage = string.Format(GlobalConstants.SearchPageNoResultsMessage, keyWord), }; return(this.View(model)); }
public ActionResult SearchResults(SearchArticlesViewModel searchModel) { if (searchModel.FromPrice != null && searchModel.ToPrice != null) { if (searchModel.FromPrice >= searchModel.ToPrice) { ModelState.AddModelError("ToPrice", Errors.GreaterThan); return(View("SearchArticles", searchModel)); } } var articles = SearchByCriterias(searchModel); if (searchModel.PageNumber == 0) { searchModel.PageNumber = 1; } var numberOfPages = Math.Ceiling((double)articles.Count() / PageSize); var articlesToDisplay = articles .OrderBy(article => article.Price) .Select(ArticleViewModel.FromArticle(CurrentCulture)) .Skip((searchModel.PageNumber - 1) * PageSize).Take(PageSize).ToList(); if (articlesToDisplay.Count == 0) { return(RedirectToAction("SearchArticles") .WithInfo(Labels.NoArticlesFound)); } ViewBag.FromPrice = searchModel.FromPrice; ViewBag.ToPrice = searchModel.ToPrice; ViewBag.CategoryId = searchModel.CategoryId; ViewBag.SearchSubstirng = searchModel.SearchSubstring; ViewBag.Pages = numberOfPages; ViewBag.PageNumber = searchModel.PageNumber; return(View(articlesToDisplay)); }
public void SearchResults_WithFullSearchModel_MustReturnOnlyArticlesThatMatchAllTheCriterias() { //Mock repository var mockedArticles = new Mock <IRepository <Article> >(); mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable()); //Mock data var mockedUow = new Mock <IUowData>(); mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); }); // Act ArticlesController articlesController = new ArticlesController(mockedUow.Object); articlesController.CurrentCulture = Culture.bg.ToString(); var searchModel = new SearchArticlesViewModel { SearchSubstring = "странно", CategoryId = 2, FromPrice = 20, ToPrice = 30, }; var results = articlesController.SearchResults(searchModel) as ViewResult; var model = results.Model as IList <ArticleViewModel>; Assert.AreEqual(1, model.Count); Assert.IsTrue(model[0].Name.Contains(searchModel.SearchSubstring)); Assert.IsTrue(model[0].Price >= searchModel.FromPrice); Assert.IsTrue(model[0].Price <= searchModel.ToPrice); Assert.IsTrue(model[0].Price <= searchModel.ToPrice); //We dont have property for category id in the view model, so we check if the id of the result is //one of the ids of the two articles that we know are from the category we search for Assert.IsTrue(model[0].Id == _articles[1].Id || model[0].Id == _articles[2].Id); }
public SearchArticlesView(SearchArticlesViewModel viewModel) { InitializeComponent(); BindingContext = viewModel; }