public void GetAll_PollsExist_ShouldReturnPublicPolls() { var poll1 = new Poll { Id = 1, Question = "Question1", ExpirationDate = DateTime.Now.AddDays(1), Visibility = Visibility.Public }; var poll2 = new Poll { Id = 2, Question = "Question2", ExpirationDate = DateTime.Now.AddDays(1), Visibility = Visibility.Public }; var poll3 = new Poll { Id = 3, Question = "Question3", ExpirationDate = DateTime.Now.AddDays(1), Visibility = Visibility.Public }; var poll4 = new Poll { Id = 4, Question = "Question4", ExpirationDate = DateTime.Now.AddDays(1), Visibility = Visibility.Private }; _mockPolls.SetSource(new[] { poll1, poll2, poll3, poll4 }); var pollTableInfo = new PollTableInfo { PagingInfo = new PagingInfo { CurrentPage = 1, ItemsPerPage = 20 }, SearchText = "", SortBy = SortBy.Id, SortDirection = SortDirection.Ascending }; var allPolls = _pollRepository.GetAll(pollTableInfo); allPolls.Should().NotBeNull(); allPolls.Should().HaveCount(3); }
public void GetAll_UserDoesntHavePolls_ShouldReturnEmptyIEnumerable() { var poll1 = new Poll { Id = 1, Question = "Question1", UserId = "1" }; var poll2 = new Poll { Id = 2, Question = "Question2", UserId = "2" }; var poll3 = new Poll { Id = 3, Question = "Question2.1", UserId = "2" }; var poll4 = new Poll { Id = 4, Question = "Question3", UserId = "3" }; _mockPolls.SetSource(new[] { poll1, poll2, poll3, poll4 }); var pollTableInfo = new PollTableInfo { PagingInfo = new PagingInfo { CurrentPage = 1, ItemsPerPage = 20 }, SearchText = "", SortBy = SortBy.Id, SortDirection = SortDirection.Ascending }; var allPolls = _pollRepository.GetAll(pollTableInfo, "4"); allPolls.Should().NotBeNull(); allPolls.Should().HaveCount(0); }
public void GetAllFiltered_ValidRequest_ShouldReturnFittingPolls() { var poll1 = new Poll { Id = 1, Question = "What is it?", User = new ApplicationUser { Id = "1", UserName = "******" }, Visibility = Visibility.Public }; var poll2 = new Poll { Id = 2, Question = "What is your favorite color?", User = new ApplicationUser { Id = "2", UserName = "******" }, Visibility = Visibility.Public }; var poll3 = new Poll { Id = 3, Question = "Do you like winter?", User = new ApplicationUser { Id = "2", UserName = "******" }, Visibility = Visibility.Public }; var poll4 = new Poll { Id = 4, Question = "Is it better or worse?", User = new ApplicationUser { Id = "3", UserName = "******" }, Visibility = Visibility.Public }; _mockPolls.SetSource(new[] { poll1, poll2, poll3, poll4 }); var tableInfo = new PollTableInfo { PagingInfo = new PagingInfo { ItemsPerPage = 20, CurrentPage = 1 }, SearchText = "4", SortBy = SortBy.Id, SortDirection = SortDirection.Ascending }; var polls = _pollRepository.GetAll(tableInfo); polls.Should().HaveCount(1); tableInfo.SearchText = "Anonymous"; polls = _pollRepository.GetAll(tableInfo); polls.Should().HaveCount(1); tableInfo.SearchText = "2"; polls = _pollRepository.GetAll(tableInfo); polls.Should().NotBeNull(); polls.Should().HaveCount(2); tableInfo.SearchText = "What is"; polls = _pollRepository.GetAll(tableInfo); polls.Should().HaveCount(2); tableInfo.SearchText = "is"; polls = _pollRepository.GetAll(tableInfo); polls.Should().HaveCount(2); }
public IEnumerable <ApplicationUser> GetAll(PollTableInfo tableInfo) { var usersQuery = Context.Users .Where(u => u.Id.Contains(tableInfo.SearchText) || u.UserName.Contains(tableInfo.SearchText) || u.Email.Contains(tableInfo.SearchText) || tableInfo.SearchText == "" ); IEnumerable <ApplicationUser> users = null; switch (tableInfo.SortBy) { case SortBy.Id: users = tableInfo.SortDirection == SortDirection.Ascending ? usersQuery.OrderBy(u => u.Id) : usersQuery.OrderByDescending(u => u.Id); break; case SortBy.UserName: users = tableInfo.SortDirection == SortDirection.Ascending ? usersQuery.OrderBy(u => u.UserName) : usersQuery.OrderByDescending(u => u.UserName); break; case SortBy.Email: users = tableInfo.SortDirection == SortDirection.Ascending ? usersQuery.OrderBy(u => u.Email) : usersQuery.OrderByDescending(u => u.Email); break; } tableInfo.PagingInfo.AllItems = users.Count(); tableInfo.PagingInfo.AllPages = (int)Math.Ceiling((double)tableInfo.PagingInfo.AllItems / tableInfo.PagingInfo.ItemsPerPage); return(users .Skip((tableInfo.PagingInfo.CurrentPage - 1) * tableInfo.PagingInfo.ItemsPerPage) .Take(tableInfo.PagingInfo.ItemsPerPage) .ToList()); }
public void GetAll_NoPollExists_ShouldReturnEmptyIEnumerable() { _mockPolls.SetSource(new List <Poll>()); var pollTableInfo = new PollTableInfo { PagingInfo = new PagingInfo { CurrentPage = 1, ItemsPerPage = 20 }, SearchText = "", SortBy = SortBy.Id, SortDirection = SortDirection.Ascending }; var allPolls = _pollRepository.GetAll(pollTableInfo); allPolls.Should().HaveCount(0); }
public IEnumerable <Poll> GetAll(PollTableInfo tableInfo, string userId = "", bool isAdmin = false) { IQueryable <Poll> pollQuery; if (userId == "" && !isAdmin) { pollQuery = Context.Polls .Include(p => p.User) .Where(p => p.Question.Contains(tableInfo.SearchText) || p.Id.ToString().Contains(tableInfo.SearchText) || p.Visibility.ToString().Contains(tableInfo.SearchText) || p.User.UserName.Contains(tableInfo.SearchText) || (p.User.UserName == null && "Anonymous".Contains(tableInfo.SearchText)) || tableInfo.SearchText == "" ) .Where(p => (p.ExpirationDate != null && p.ExpirationDate.Value > DateTime.Now || p.ExpirationDate == null) && p.Visibility == Visibility.Public); } else if (isAdmin) { pollQuery = Context.Polls .Include(p => p.User) .Where(p => p.Question.Contains(tableInfo.SearchText) || p.Id.ToString().Contains(tableInfo.SearchText) || p.Visibility.ToString().Contains(tableInfo.SearchText) || p.User.UserName.Contains(tableInfo.SearchText) || (p.User.UserName == null && "Anonymous".Contains(tableInfo.SearchText)) || tableInfo.SearchText == "" ); } else { pollQuery = Context.Polls .Where(p => p.UserId == userId || userId == "") .Where(p => p.Question.Contains(tableInfo.SearchText) || p.Id.ToString().Contains(tableInfo.SearchText) || p.Visibility.ToString().Contains(tableInfo.SearchText) || tableInfo.SearchText == "" ); } IEnumerable <Poll> polls = null; switch (tableInfo.SortBy) { case SortBy.Id: polls = tableInfo.SortDirection == SortDirection.Ascending ? pollQuery.OrderBy(p => p.Id) : pollQuery.OrderByDescending(p => p.Id); break; case SortBy.Question: polls = tableInfo.SortDirection == SortDirection.Ascending ? pollQuery.OrderBy(p => p.Question) : pollQuery.OrderByDescending(p => p.Question); break; case SortBy.UserName: polls = tableInfo.SortDirection == SortDirection.Ascending ? pollQuery.OrderBy(p => p.User.UserName) : pollQuery.OrderByDescending(p => p.User.UserName); break; case SortBy.Visibility: polls = tableInfo.SortDirection == SortDirection.Ascending ? pollQuery.OrderBy(p => p.Visibility) : pollQuery.OrderByDescending(p => p.Visibility); break; } tableInfo.PagingInfo.AllItems = polls.Count(); tableInfo.PagingInfo.AllPages = (int)Math.Ceiling((double)tableInfo.PagingInfo.AllItems / tableInfo.PagingInfo.ItemsPerPage); return(polls .Skip((tableInfo.PagingInfo.CurrentPage - 1) * tableInfo.PagingInfo.ItemsPerPage) .Take(tableInfo.PagingInfo.ItemsPerPage) .ToList()); }