public 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.MessageSent); return(PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _Context.Messages.AsQueryable(); switch (messageParams.MessageContainer) { case "Inbox": messages = messages.Where(m => m.RecipientId == messageParams.UserId && !m.RecipientDeleted); break; case "Outbox": messages = messages.Where(m => m.SenderId == messageParams.UserId && !m.SenderDeleted); break; default: messages = messages.Where(m => (m.RecipientId == messageParams.UserId) && !m.IsRead && !m.RecipientDeleted); break; } messages = messages.OrderByDescending(m => m.MessageSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserQueryParams userQueryParams) { var results = _context.Users.Include(u => u.Photos).Where(u => u.Id != userQueryParams.CurrentUserId && u.Gender == userQueryParams.Gender); var minDateOfBirth = DateTime.Today.AddYears(-userQueryParams.MaxAge - 1); var maxDateOfBirth = DateTime.Today.AddYears(-userQueryParams.MinAge); results = results.Where(u => u.DateOfBirth >= minDateOfBirth && u.DateOfBirth <= maxDateOfBirth); if (!string.IsNullOrWhiteSpace(userQueryParams.OrderBy)) { results = results.OrderByDescending(u => userQueryParams.OrderBy.Equals("created", StringComparison.InvariantCultureIgnoreCase) ? u.Created : u.LastActive); } else { results = results.OrderByDescending(u => u.LastActive); } return(await PagedList <User> .CreateAsync(results, userQueryParams.PageNumber, userQueryParams.PageSize)); }
public async Task <PagedList <Message> > GetUserMessages(MessageParams messageParams) { var messages = _context.Messages .Include(m => m.Sender).ThenInclude(u => u.Photos) .Include(m => m.Receiver).ThenInclude(u => u.Photos) .AsQueryable(); if (messageParams.MessageContainer == "Inbox") { messages = messages.Where(m => m.ReceiverId == messageParams.UserId && m.ReceiverDeleted == false); } else if (messageParams.MessageContainer == "Outbox") { messages = messages.Where(m => m.SenderId == messageParams.UserId && m.SenderDeleted == false); } else { messages = messages.Where(m => m.ReceiverId == messageParams.UserId && m.IsRead == false && m.ReceiverDeleted == false); } messages = messages.OrderByDescending(m => m.DateSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _context .Messages .AsQueryable(); if (messageParams.MessageContainer == "Inbox") { messages = messages.Where(prm => prm.RecipientId == messageParams.UserId && prm.RecipientDeleted == false); } else if (messageParams.MessageContainer == "Outbox") { messages = messages.Where(prm => prm.SenderId == messageParams.UserId && prm.SenderDeleted == false); } else { messages = messages.Where(prm => prm.RecipientId == messageParams.UserId && prm.IsRead == false && prm.RecipientDeleted == false); } messages = messages.OrderBy(prm => prm.DateMessageSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <MemberDto> > GetMembersAsync(UserParams userParams) { var query = _context.Users.AsQueryable(); query = query.Where(u => u.UserName != userParams.CurrentUsername); query = query.Where(u => u.Gender == userParams.Gender); var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDob = DateTime.Today.AddYears(-userParams.MinAge); query = query.Where(u => u.DateOfBirth >= minDob && u.DateOfBirth <= maxDob); query = userParams.OrderBy switch { "created" => query.OrderByDescending(u => u.Created), _ => query.OrderByDescending(u => u.LastActive) }; return(await PagedList <MemberDto> .CreateAsync( query.ProjectTo <MemberDto>(_mapper.ConfigurationProvider).AsNoTracking(), userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = _ctx.Users .Include(p => p.Photos) .OrderByDescending(u => u.LastActive).AsQueryable(); if (userParams.MinAge != 18 || userParams.MaxAge != 99) { var minDateofBirth = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDateofBirth = DateTime.Today.AddYears(-userParams.MinAge); users = users.Where(u => u.Id != userParams.UserId && u.Gender == userParams.Gender && u.DateOfBirth >= minDateofBirth && u.DateOfBirth <= maxDateofBirth); } else { users = users.Where(u => u.Id != userParams.UserId && u.Gender == userParams.Gender); } if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { // Указываем, какие данные из связанных таблиц нужно извлекать // (в нашем случае, это фотографии отправителей и получателей сообщений) var messages = _context.Messages .Include(u => u.Sender).ThenInclude(p => p.Photos) .Include(u => u.Recipient).ThenInclude(p => p.Photos) .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.MessageSent); // Отбирать следует только сообщения, попадающие в определённое // информационное окно return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _context.Messages .Include(message => message.Sender).ThenInclude(user => user.Photos) .Include(message => message.Recipient).ThenInclude(user => user.Photos) .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.MessageSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = _context.Users .OrderByDescending(prm => prm.LastActive) .AsQueryable(); users = users.Where(prm => prm.Id != userParams.UserId && prm.Gender == userParams.Gender); if (userParams.Likers) { var userLikers = await GetUserLikes(userParams.UserId, userParams.Likers); users = users.Where(prm => userLikers.Contains(prm.Id)); } if (userParams.Likees) { var userLikees = await GetUserLikes(userParams.UserId, userParams.Likers); users = users.Where(prm => userLikees.Contains(prm.Id)); } if (userParams.MinAge != 18 && userParams.MaxAge != 99) { var minBirthDate = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxBirthDate = DateTime.Today.AddYears(-userParams.MinAge); users = users.Where(prm => prm.BirthDate >= minBirthDate && prm.BirthDate <= maxBirthDate); } if (!string.IsNullOrEmpty(userParams.OrderBy)) { users = userParams.OrderBy == "created" ? users.OrderByDescending(prm => prm.CreatedOn) : users.OrderByDescending(prm => prm.LastActive); } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
// Method for the http request to get users // This will return a pagedlist of user based on user parameters public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = _context.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable(); // Remove the user making the request from the response users = users.Where(u => u.Id != userParams.UserId); // Remove opposite gender from user users = users.Where(u => u.Gender == userParams.Gender); // Check if the user wants a custom age filter if (userParams.minAge != 18 || userParams.maxAge != 99) { var minDOB = DateTime.Today.AddYears(-userParams.maxAge - 1); var maxDOB = DateTime.Today.AddYears(-userParams.minAge); users = users.Where(users => users.DateOfBirth >= minDOB && users.DateOfBirth <= maxDOB); } // Get the user's ordering preference (if exists) if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _context.Messages.Include(u => u.Sender).ThenInclude(p => p.Photos) .Include(u => u.Recipient).ThenInclude(p => p.Photos).AsQueryable(); switch (messageParams.MessageContainer) { // If I'am the user and I'm seeing the Inbox then my (user Id) == recipient Id and vice versa in outbox 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.MessageSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { // ThenInclude(): message include sender (user) then sender (user) include photos // tuong tu 'recipient' // convert IEnumerable to LinQ.IQueryable de su dung cac ham filter cua linq var messages = _context.Messages .Include(u => u.Sender).ThenInclude(p => p.Photos) .Include(u => u.Recipient).ThenInclude(p => p.Photos) .AsQueryable(); switch (messageParams.MessageContainer) { case "Inbox": // filter nhung message co recipient = id params // van hien thi trong inbox message neu ng gui da xoa nhung ng nhan chua xoa messages = messages.Where(u => u.RecipientId == messageParams.UserId && u.RecipientDeleted == false); break; case "Outbox": // van hien thi trong outbox message neu ng nhan da xoa nhung ng gui chua xoa messages = messages.Where(u => u.SenderId == messageParams.UserId && u.SenderDeleted == false); break; default: // // van hien thi trong unread message neu ng gui da xoa nhung ng nhan chua xoa messages = messages.Where(u => u.RecipientId == messageParams.UserId && u.RecipientDeleted == false && u.IsRead == false); break; } // oderby theo time message gui messages = messages.OrderByDescending(d => d.MessageSent); // return ve list messages va thong so config pagination return(PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MessageParams messageParams) { var messages = _context.Messages //.Include(u => u.Sender) //.ThenInclude(p => p.Photos) //.Include(u => u.Recipient) //.ThenInclude(p => p.Photos) .AsQueryable(); // this will allow to add .Where like below 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; // unread messages } messages = messages.OrderByDescending(d => d.MessageSent); return(await PagedList <Message> .CreateAsync(messages, messageParams.PageNumber, messageParams.PageSize)); }
public async Task <PagedList <Messages> > GetMessages(MessageParams messageParams) { var messages = _context.Messages .Include(u => u.Sender).ThenInclude(p => p.Photos) .Include(u => u.Recipient).ThenInclude(p => p.Photos) .AsQueryable(); switch (messageParams.MessageContainer) { case "Inbox": messages = messages.Where(u => u.RecipientUniqueId == messageParams.UserId); break; case "Outbox": messages = messages.Where(u => u.SenderUniqueId == messageParams.UserId); break; default: messages = messages.Where(u => u.RecipientUniqueId == messageParams.UserId && u.IsRead == false); break; } messages = messages.OrderBy(d => d.MessageSent); return(await PagedList <Messages> .CreateAsync(messages, messageParams.PageNUmber, messageParams.PageSize)); }
public async Task <PagedList <Message> > GetMessagesForUser(MsgParams msgParams) { var msg = _context.Messages .Include(u => u.Sender).ThenInclude(u => u.Photos) .Include(u => u.Recipient).ThenInclude(u => u.Photos).AsQueryable(); switch (msgParams.MessageContainer) { case "Inbox": msg = msg.Where(m => m.RecipientId == msgParams.UserId && !m.recipientDelete); break; case "Outbox": msg = msg.Where(m => m.SenderId == msgParams.UserId && !m.senderDelete); break; default: msg = msg.Where(m => m.RecipientId == msgParams.UserId && !m.isRead && !m.recipientDelete); break; } msg = msg.OrderByDescending(d => d.DateSent); return(await PagedList <Message> .CreateAsync(msg, msgParams.PageNumber, msgParams.PageSize)); }
public async Task <PagedList <Message> > GetUserMessages(MessageParams msgParams) { var messages = _context.Messages .Include(u => u.Sender) .ThenInclude(u => u.Photos) .Include(u => u.Recipient) .ThenInclude(u => u.Photos) .AsQueryable(); switch (msgParams.Container) { case "Inbox": messages = messages .Where(u => u.RecipientId == msgParams.UserId && !u.RecipientDeleted); break; case "Outbox": messages = messages .Where(u => u.SenderId == msgParams.UserId && !u.SenderDeleted); break; default: messages = messages .Where(u => u.RecipientId == msgParams.UserId && !u.IsRead && !u.RecipientDeleted); break; } messages = messages.OrderByDescending(d => d.DateSent); return(await PagedList <Message> .CreateAsync( messages, msgParams.PageNumber, msgParams.PageSize )); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { //var users = _context.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable(); // ... Includes are not needed anymore and are removed because of Lazy Loading: var users = _context.Users.OrderByDescending(u => u.LastActive).AsQueryable(); users = users.Where(u => u.Id != userParams.UserId); users = users.Where(u => u.Gender == userParams.Gender); 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)); } if (userParams.MinAge != 18 || userParams.MaxAge != 99) { var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDob = DateTime.Today.AddYears(-userParams.MinAge); users = users.Where(u => u.DateOfBirth >= minDob && u.DateOfBirth <= maxDob); } // ... OR create orderBy function and add to expression tree: //Func<User, DateTime> f = (u) => { return u.Created; }; if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; case "dob": users = users.OrderByDescending(u => u.DateOfBirth); break; case "username": users = users.OrderByDescending(u => u.UserName); break; case "id": users = users.OrderByDescending(u => u.Id); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } // ... PagedList.CreatyeAsync() - static method to load IQueryable source (users) and create instance of PagedList with List<T> base class var pagedUsers = await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize); return(pagedUsers); }
//public async Task<IEnumerable<User>> GetUsers() //{ // var users = await _context.Users.Include(p => p.Photos).ToListAsync(); // return users; //} public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = await _context.Users.Include(p => p.Photos).ToListAsync(); return(await PagedList <User> .CreateAsync((IQueryable <User>) users, userParams.PageNumber, userParams.PageSize)); //users; }
public async Task <PagedList <User> > GetUsers(int pageNumber, int pageSize) { var results = _context.Users.Include(u => u.Photos); return(await PagedList <User> .CreateAsync(results, pageNumber, pageSize)); }
public async Task <PagedList <Actividad> > GetActividades(UserParams userParams) { var actividades = _context.Actividades.Where(a => a.UserId != userParams.UserId); return(await PagedList <Actividad> .CreateAsync(actividades, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = _context.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable(); users = users.Where(u => u.Id != userParams.UserId); users = users.Where(u => u.Gender == userParams.Gender); 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)); } if (userParams.Blockers) { var userBlockers = await GetUserBlocks(userParams.UserId, userParams.Blockers); users = users.Where(u => !(userBlockers.Contains(u.Id))); Console.WriteLine("Blocker" + users); } if (userParams.Blockees) { var userBlockers = await GetUserBlocks(userParams.UserId, userParams.Blockers); users = users.Where(u => (userBlockers.Contains(u.Id))); Console.WriteLine("Blocker" + users); } if (userParams.Dislikers) { var userDislikers = await GetUserDislikes(userParams.UserId, userParams.Dislikers); users = users.Where(u => userDislikers.Contains(u.Id)); } if (userParams.Dislikees) { var userDislikees = await GetUserDislikes(userParams.UserId, userParams.Dislikers); users = users.Where(u => userDislikees.Contains(u.Id)); } if (userParams.MinAge != 18 || userParams.MaxAge != 99) { var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDob = DateTime.Today.AddYears(-userParams.MinAge - 1); users = users.Where(users => users.DateOfBirth >= minDob && users.DateOfBirth <= maxDob); } if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <User> > GetUsersAsync(UserParams userParams) { var query = _db.Users .Include(i => i.Photos) .Include(i => i.Likers) .Include(i => i.Likees) .AsQueryable(); query = query.Where(w => (w.Gender == userParams.Gender || userParams.Gender == null) && w.Id != userParams.UserId); if (userParams.Likers) { query = query.Where(w => w.Likees.Any(a => a.LikeeId == userParams.UserId)); } if (userParams.Likees) { query = query.Where(w => w.Likers.Any(a => a.LikerId == userParams.UserId)); } DateTime?minDob = null; DateTime?maxDob = null; if (userParams.MaxAge is int max) { minDob = DateTime.Today.AddYears(-max - 1); } if (userParams.MinAge is int min) { maxDob = DateTime.Today.AddYears(-min); } query = query.Where(w => (w.DateOfBirth >= minDob || minDob == null) && (w.DateOfBirth <= maxDob || maxDob == null)); // Još neki od slučajeva sa pattern matchingom //var minDob2 = userParams.MaxAge !=null ? DateTime.Today.AddYears((int)-userParams.MaxAge - 1) : (DateTime?)null; //var minDob3 = userParams.MaxAge is int maxx // ? DateTime.Today.AddYears((int) -userParams.MaxAge - 1) // : (DateTime?) null; if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": query = query.OrderBy(o => o.Created); break; case "createdDesc": query = query.OrderByDescending(o => o.Created); break; case "age": query = query.OrderBy(o => o.DateOfBirth); break; case "ageDesc": query = query.OrderByDescending(o => o.DateOfBirth); break; default: query = query.OrderByDescending(o => o.LastActive); break; } } var pagedList = await PagedList <User> .CreateAsync(query, userParams.PageNumber, userParams.PageSize); return(pagedList); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { var users = context.Users.Include(p => p.Photos) .OrderByDescending(u => u.LastActive).AsQueryable(); users = users.Where(u => u.Id != userParams.UserId); users = users.Where(u => u.Gender == userParams.Gender); 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)); } DateTime?minDateOfBirth = null; DateTime?maxDateOfBirth = null; if (userParams.MinAge.HasValue) { maxDateOfBirth = DateTime.Today.AddYears(-userParams.MinAge.Value); } if (userParams.MaxAge.HasValue) { minDateOfBirth = DateTime.Today.AddYears(-userParams.MaxAge.Value - 1); } if (minDateOfBirth.HasValue && maxDateOfBirth.HasValue) { users = users.Where(u => u.DateOfBirth >= minDateOfBirth && u.DateOfBirth <= maxDateOfBirth); } else if (minDateOfBirth.HasValue) { users = users.Where(u => u.DateOfBirth >= minDateOfBirth); } else if (maxDateOfBirth.HasValue) { users = users.Where(u => u.DateOfBirth <= maxDateOfBirth); } if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { // We don't execute .ToListAsync() here since we will // do it on the PagedList<T> class on the // CreateAsync method var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDob = DateTime.Today.AddYears(-userParams.MinAge); var users = _context.Users.Include(i => i.Photos) .Where(w => w.Id != userParams.UserId) .Where(w => w.Gender == userParams.Gender) .Where(w => w.DateOfBirth >= minDob && w.DateOfBirth <= maxDob) .OrderByDescending(o => o.LastActive) .AsQueryable(); // Our 'Like' entity just contain list of integers // Therefore, we will retrieve those list of integers // using a private method, and then we will query those // 'User' information based on those list of integers // which are also User Ids. 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)); } //if (userParams.MinAge != 18 || userParams.MaxAge != 99) //{ // var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); // var maxDob = DateTime.Today.AddYears(-userParams.MinAge); // users = users.Where(w => w.DateOfBirth >= minDob // && w.DateOfBirth <= maxDob); //} if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task<PagedList<User>> GetUsers(UserParams userParams) { var users = this.context.Users.Include(p => p.Photos); return await PagedList<User>.CreateAsync(users, userParams.PageNumber, userParams.PageSize); }
// action updated for pagination in section 14 lecture 139 public async Task <PagedList <User> > GetUsers(UserParams userParams) { // previously we had toListAsync in the foll. line, but due to the pagination code addition, this has been deferred to the PagedList class // ASQueryable: https://weblogs.asp.net/zeeshanhirani/using-asqueryable-with-linq-to-objects-and-linq-to-sql // https://stackoverflow.com/questions/17366907/what-is-the-purpose-of-asqueryable var users = _context.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable(); users = users.Where(u => u.Id != userParams.UserID); // this would filter out the current logged in user from their matches list users = users.Where(u => u.Gender == userParams.Gender); // this would filter out users acc.to gender // if the query string from the SPA has likers/likees, we filter only the users that either have a liker or a likee id from the likes table if (userParams.Likers) { var userLikers = await GetUserLikes(userParams.UserID, userParams.Likers); // filtering the users that like the currently logged user users = users.Where(u => userLikers.Contains(u.Id)); } if (userParams.Likees) { var userLikees = await GetUserLikes(userParams.UserID, userParams.Likers); // it is fine to use userParams.likers here, // since it is anyway a boolean that gets sent to GetUserLikes // filtering the users that are liked by the currently logged in user users = users.Where(u => userLikees.Contains(u.Id)); } // if user customizes the filter for age if (userParams.MinAge != 18 || userParams.MaxAge != 99) { // since we have to calculate the age using date of birth for minDOB, we are subtracting maxAge from today // the -1 is because we are asking for maximum year of a person // That is, for eg, if user searches with maxAge as 30 on Jan 18 2019, then AddYears(-maxAge) will give Jan 18, 1989. But // suppose someone turns 31 tomorrow i.e. on Jan 19, 2019, he should still show up in the user's search of maxAge 30. // Which means the DOB of that person would be Jan 19 1988. That's why we have -1 in the formula. Basically since a person is // the same age for 364 days, we include the -1. // https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c var minDOB = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDOB = DateTime.Today.AddYears(-userParams.MinAge); users = users.Where(u => u.DateOfBirth >= minDOB && u.DateOfBirth <= maxDOB); } // if user wants to see the results in a specific order if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } // since CreateAsync is a static class, we can call it directly as below return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }
public async Task <PagedList <User> > GetUsers(UserParams userParams) { // var users1 = (from a in _context.Likes // join b in _context.Users on a.LikeeId equals b.Id // join c in _context.Likes on b.Id equals c.LikerId // where a.LikerId == 1 // let likeeId = c.LikeeId // select new { a.LikeeId, likeeId }); // var users2 = (from a in _context.Users // join b in _context.Likes on a.Id equals b.LikeeId // join c in _context.Users on b.LikerId equals c.Id // where c.Id == 1 // let knownAs = c.KnownAs // select new {a.KnownAs, knownAs}); // var users22 = (from a in _context.Users // join b in _context.Likes on a.Id equals b.LikeeId // join c in _context.Users on b.LikerId equals c.Id // let knownAs = c.KnownAs // select new {a.KnownAs, knownAs}); // var users3 = (from a in _context.Users // join c in _context.Likes on a.Id equals c.LikeeId // where a.Id == 4 // select c.Liker.KnownAs); // var users4 = (from a in _context.Users // join c in _context.Likes on a.Id equals c.LikeeId // where a.Id == 1 // select c.Liker.KnownAs); // var users5 = (from a in _context.Users // join c in _context.Likes on a.Id equals c.LikerId // where c.LikeeId == 1 // select a.Id); var users = _context.Users.Include(p => p.Photos).OrderByDescending(u => u.LastActive).AsQueryable(); users = users.Where(u => u.Id != userParams.UserId); if (userParams.Gender == "male" || userParams.Gender == "female") { users = users.Where(u => u.Gender == userParams.Gender); } // users = users.Where(u => u.Gender == userParams.Gender); 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)); } if (userParams.MinAge != 18 || userParams.MaxAge != 99) { var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); var maxDob = DateTime.Today.AddYears(-userParams.MinAge); users = users.Where(u => u.DateOfBirth >= minDob && u.DateOfBirth <= maxDob); } if (!string.IsNullOrEmpty(userParams.OrderBy)) { switch (userParams.OrderBy) { case "created": users = users.OrderByDescending(u => u.Created); break; default: users = users.OrderByDescending(u => u.LastActive); break; } } return(await PagedList <User> .CreateAsync(users, userParams.PageNumber, userParams.PageSize)); }