public void Check_Searching_Posts_By_Criterias() { // Arrange var posts = new List <Post>(); var postService = new PostService(dbContext, mapper); for (int i = 0; i < NUMBER_OF_RECENT_POSTS; i++) { var newPost = new Post() { CategoryId = i, CreatedOn = DateTime.Now, TagId = i, Title = $"Random Title {i}", Description = $"Random Description {i}", }; posts.Add(newPost); } this.dbContext.Posts.AddRange(posts); this.dbContext.SaveChanges(); var searchQuery = new SearchPostDTO() { SearchQuery = "random" }; // Act var foundPosts = postService.SearchPosts(searchQuery); //Assert Assert.AreEqual(foundPosts.Count, posts.Count); }
/// <summary> /// Searching for posts using 3 properties - search query, category and tag. /// If it finds one of them, it will return a collection of posts matching the criteria. /// It is displayed on the home page /// </summary> /// <param name="searchParams"></param> /// <returns>A collection with posts</returns> public ICollection <PostDTO> SearchPosts(SearchPostDTO searchParams) { using (DbContext) { var posts = DbContext.Posts .Where(e => (e.Title.Contains(searchParams.SearchQuery) || e.Description.Contains(searchParams.SearchQuery)) || e.Tag.Name == searchParams.Tag || e.Category.Name == searchParams.Category) .Select(r => new PostDTO() { Category = r.Category.Name, CreatedOn = r.CreatedOn, Tag = r.Tag.Name, Title = r.Title, Description = r.Description, UserName = r.User.UserName, Id = r.Id }).ToList(); return(posts); } }