public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = _context.Users.AsQueryable(); // Filter var minimumDateOfBirth = DateTime.Today.AddYears(-userParams.MaximumAge - 1 ?? -150); var maximumDateOfBirth = DateTime.Today.AddYears(-userParams.MinimumAge ?? -18); users = users.Where(u => u.Id != userParams.UserId) .Where(u => u.Gender == userParams.Gender) .Where(u => u.DateOfBirth >= minimumDateOfBirth && u.DateOfBirth <= maximumDateOfBirth); if (userParams.Likers) { var userLikers = await GetUserLikes(userParams.UserId, userParams.Likers); users = users.Where(u => userLikers.Contains(u.Id)); } if (userParams.Likees) { var userLikees = await GetUserLikes(userParams.UserId, userParams.Likers); users = users.Where(u => userLikees.Contains(u.Id)); } // Sorting switch (userParams.OrderBy?.ToLower()) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } // Create pagination page return(await PagedList <User> .CreatePageAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _context.Messages.AsQueryable(); switch (messageParams.MessageContainer) { case "Inbox": messages = messages.Where(u => u.RecipientId == messageParams.UserId && u.RecipientDeleted == false); break; case "Outbox": messages = messages.Where(u => u.SenderId == messageParams.UserId && u.SenderDeleted == false); break; default: messages = messages.Where(u => u.RecipientId == messageParams.UserId && u.RecipientDeleted == false && u.IsRead == false); break; } messages = messages.OrderByDescending(d => d.DateSend); return(await PagedList <Message> .CreatePageAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }