Beispiel #1
0
        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.MessageSent);
            return(PagedList <Message> .Create(messages.ToList(), messageParams.PageNumber, messageParams.PageSize));
        }
Beispiel #2
0
        public async Task <PagedList <User> > GetUsers(UserParams userParams)
        {
            var users = _context.Users.Include(u => u.Photos).Include(u => u.UsersTemplate).Include(u => u.Preferences)
                        .OrderByDescending(u => u.LastActive)
                        .AsQueryable();

            var preferences = await _context.Preferences.Where(p => p.UserId == userParams.UserId).FirstOrDefaultAsync();

            //Filters for get users
            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);
            }

            if (!string.IsNullOrEmpty(userParams.OrderBy))
            {
                switch (userParams.OrderBy)
                {
                case "created":
                    users = users.OrderByDescending(u => u.Created);
                    break;

                case "preferences":
                    //Z tą pierwszą metodą jest coś nie tak
                    List <UserToSort> usersToSort = new List <UserToSort>();
                    UserToSort        userToSort;
                    foreach (var user in users)
                    {
                        userToSort = new UserToSort
                        {
                            User     = user,
                            Accuracy = CountAccuracy(user.UsersTemplate, preferences)
                        };
                        usersToSort.Add(userToSort);
                    }
                    usersToSort = usersToSort.OrderByDescending(u => u.Accuracy).ToList();
                    users       = usersToSort.Select(u => u.User).AsQueryable();
                    break;

                case "appearance":
                    List <UserToSort> usersToSortForAppearance = new List <UserToSort>();
                    UserToSort        userToSortForAppearance;
                    foreach (var user in users)
                    {
                        userToSort = new UserToSort
                        {
                            User     = user,
                            Accuracy = CountAccuracyForAppearance(user.UsersTemplate, preferences)
                        };
                        usersToSortForAppearance.Add(userToSort);
                    }
                    usersToSort = usersToSortForAppearance.OrderByDescending(u => u.Accuracy).ToList();
                    users       = usersToSort.Select(u => u.User).AsQueryable();
                    break;

                default:
                    users = users.OrderByDescending(u => u.LastActive);
                    break;
                }
            }

            return(PagedList <User> .Create(users.ToList(), userParams.PageNumber, userParams.PageSize));
        }