public async Task GetSearchedArticlesShouldReturnCorrectArticles() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new ApplicationDbContext(optionsBuilder.Options); var articleService = new ArticleService(dbContext, null); var searchedArticle = new Article { Content = "testContent", ImageUrl = "testImageUrl", Title = "testTitle", Id = 1, UserId = "Icaka99", CreatedOn = DateTime.Now, User = new ApplicationUser { UserName = "******", }, }; var notSearchedArticle = new Article { Content = "notTestContent", ImageUrl = "notTestImageUrl", Title = "doesntContainTheWord", Id = 2, UserId = "notIcaka99", CreatedOn = DateTime.Now, User = new ApplicationUser { UserName = "******", }, }; var searchedString = new SearchStringInputModel { SearchString = "testTitle", }; await dbContext.AddAsync(searchedArticle); await dbContext.AddAsync(notSearchedArticle); await dbContext.SaveChangesAsync(); var result = articleService.GetSearchedArticles(searchedString, 1); Assert.NotNull(result); Assert.Single(result); Assert.Equal("testTitle", result.First().Title); }
public IEnumerable <ArticleViewModel> GetSearchedArticles(SearchStringInputModel input, int page, int itemsPerPage = 1) { if (input.SearchString == null) { input.SearchString = string.Empty; } return(this.db.Articles .Where(x => x.Title.ToLower().Contains(input.SearchString.ToLower())) .OrderByDescending(x => x.CreatedOn) .Select(x => new ArticleViewModel { Title = x.Title, Content = x.Content, CreatedOn = x.CreatedOn, Id = x.Id, UserUserName = x.User.UserName, ImageUrl = x.ImageUrl, }).ToList()); }
public IActionResult List(SearchStringInputModel input, int id) { if (id == 0) { id = 1; } var articles = this.articleService.GetSearchedArticles(input, id, ItemsPerPage); var orderedArticles = this.articleService.GetAllOrderedArticles(); var model = new ArticlesListViewModel { Articles = articles, ArticlesCount = articles.Count(), ItemsPerPage = ItemsPerPage, PageNumber = id, OrderedArticles = orderedArticles, SearchString = input.SearchString, Categories = this.categoryService.GetRandomCategories(6), }; return(this.View(nameof(this.Blog), model)); }