Пример #1
0
        public static void SeedUsers(IUserDataContext userDataContext, IPhotosDataContext photosDataContext)
        {
            if (!userDataContext.GetAllUsersByLastActive().Result.Any())
            {
                System.Console.WriteLine("Seeding users...");
                var userData = System.IO.File.ReadAllText("Data/UserSeedData.json");
                var users    = JsonConvert.DeserializeObject <List <User> >(userData);
                foreach (var user in users)
                {
                    byte[] passwordHash, passwordSalt;
                    CreatePasswordHash("password", out passwordHash, out passwordSalt);

                    user.PasswordHash = passwordHash;
                    user.PasswordSalt = passwordSalt;
                    user.Username     = user.Username.ToLower();
                    userDataContext.Add(user);

                    foreach (var photo in user.PhotosFromSeed)
                    {
                        photo.UserId = userDataContext.GetByUsername(user.Username).Result.Id;
                        photosDataContext.Add(photo);
                    }
                }
                System.Console.WriteLine("User Seed complete.");
            }
        }
        public async Task <PagedList <User> > GetUsers(UserParams userParams)
        {
            IEnumerable <User> users;

            users = await _userDataContext.GetAllUsersByLastActive();

            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 (userParams.SameCountry)
            {
                var currentUser = await _userDataContext.GetById(userParams.UserId);

                users = users.Where(u => u.Country == currentUser.Country);
            }

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

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

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