Exemple #1
0
        public static void CreatePurchases(IMovieRepository movieRepository, IMemberRepository memberRepository, IPurchaseRepository purchaseRepository)
        {
            if (purchaseRepository.CollectionExistsAsync().Result)
            {
                return;
            }

            var bogus          = new Faker();
            var allMembers     = memberRepository.GetAsync(CancellationToken.None).Result.ToList();
            var allMovies      = movieRepository.GetAsync(CancellationToken.None).Result.ToList();
            var bogusPurchases = new List <Purchase>();
            var random         = new Random();

            for (var i = 0; i <= 3000; i++)
            {
                var rentalDate       = bogus.Date.Between(new DateTime(2005, 1, 1), DateTime.Today);
                var rentalExpiryDate = rentalDate.Date.AddDays(random.Next(2, 4));
                var moviesRented     = allMovies.OrderBy(x => random.Next()).Take(random.Next(2, 7)).ToList();

                bogusPurchases.Add(new Purchase
                {
                    MemberId     = allMembers[random.Next(allMembers.Count - 1)].Id,
                    RentalDate   = rentalDate,
                    RentalExpiry = rentalExpiryDate,
                    CreatedOn    = rentalDate,
                    UpdatedOn    = rentalExpiryDate,
                    MovieIds     = moviesRented.Select(m => m.Id),
                    TotalCost    = moviesRented.Sum(m => m.RentalCost)
                });
            }

            purchaseRepository.CreateManyAsync(bogusPurchases);
        }