private static async Task SeedOrdersOfUser(FitStoreDbContext database, IEnumerable <OrderSupplements> supplementsInOrder, User user) { DateTime previousOrderPurchaseDate = database.Orders.LastOrDefault() == null ? DateTime.UtcNow : database.Orders.Last().PurchaseDate; Order order = new Order { UserId = user.Id, PurchaseDate = previousOrderPurchaseDate.AddDays(-45) }; await database.Orders.AddAsync(order); await database.SaveChangesAsync(); foreach (OrderSupplements supplementInOrder in supplementsInOrder) { OrderSupplements supplement = new OrderSupplements { OrderId = order.Id, SupplementId = supplementInOrder.SupplementId, Quantity = supplementInOrder.Quantity, Price = supplementInOrder.Price }; order.Supplements.Add(supplement); } order.TotalPrice = order.Supplements.Sum(s => s.Quantity * s.Price); }
private static void SeedReviews(UserManager <User> userManager, FitStoreDbContext database) { Task.Run(async() => { if (!await database.Reviews.AnyAsync()) { User firstUser = await userManager.FindByNameAsync("User_1"); foreach (Review review in reviewsFirstPart) { review.AuthorId = firstUser.Id; } await database.AddRangeAsync(reviewsFirstPart); User secondUser = await userManager.FindByNameAsync("User_2"); foreach (Review review in reviewsSecondPart) { review.Author = secondUser; } await database.AddRangeAsync(reviewsSecondPart); await database.SaveChangesAsync(); } }) .GetAwaiter() .GetResult(); }
private static void SeedOrders(UserManager <User> userManager, FitStoreDbContext database) { Task.Run(async() => { if (!await database.Orders.AnyAsync()) { User firstUser = await userManager.FindByNameAsync("User_1"); User secondUser = await userManager.FindByNameAsync("User_2"); await SeedOrdersOfUser(database, supplementsInOrderFirstPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderSecondPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderThirdPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderFourthPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderFifthPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderSixPart, firstUser); await SeedOrdersOfUser(database, supplementsInOrderFirstPart, secondUser); await SeedOrdersOfUser(database, supplementsInOrderSecondPart, secondUser); await SeedOrdersOfUser(database, supplementsInOrderThirdPart, secondUser); await SeedOrdersOfUser(database, supplementsInOrderFourthPart, secondUser); await SeedOrdersOfUser(database, supplementsInOrderFifthPart, secondUser); await SeedOrdersOfUser(database, supplementsInOrderSixPart, secondUser); await database.SaveChangesAsync(); } }) .GetAwaiter() .GetResult(); }
private static void SeedCategories(FitStoreDbContext database) { Task.Run(async() => { string[] categoryNames = new[] { Protein, AminoAcids, Creatine, Vitamins }; foreach (string categoryName in categoryNames) { bool isCategoryExisting = await database.Categories.AnyAsync(c => c.Name == categoryName); if (!isCategoryExisting) { Category category = new Category { Name = categoryName }; await database.Categories.AddAsync(category); await database.SaveChangesAsync(); } } }) .GetAwaiter() .GetResult(); }
private static void SeedSuplements(FitStoreDbContext database) { Task.Run(async() => { if (!await database.Supplements.AnyAsync()) { await database.Supplements.AddRangeAsync(proteinSupplements); await database.Supplements.AddRangeAsync(aminoAcidsSupplements); await database.Supplements.AddRangeAsync(creatineSupplements); await database.Supplements.AddRangeAsync(vitaminsSupplements); await database.SaveChangesAsync(); } }) .GetAwaiter() .GetResult(); }
private static void SeedManufacturers(FitStoreDbContext database) { Task.Run(async() => { string[] manufacturerNames = new[] { OptimumNutrition, ControlledLabs, MusclePharm, BulkPowders, Gaspari, Bsn }; string[] manufacturerAddressNames = new[] { OptimumNutritionAddress, ControlledLabsAddress, MusclePharmAddress, BulkPowdersAddress, GaspariAddress, BsnAddress }; int count = 0; foreach (string manufacturerName in manufacturerNames) { bool isManufacturerExisting = await database.Manufacturers.AnyAsync(m => m.Name == manufacturerName); if (!isManufacturerExisting) { Manufacturer manufacturer = new Manufacturer { Name = manufacturerName, Address = manufacturerAddressNames[count++] }; await database.Manufacturers.AddAsync(manufacturer); await database.SaveChangesAsync(); } } }) .GetAwaiter() .GetResult(); }
private static void SeedComments(UserManager <User> userManager, FitStoreDbContext database) { Task.Run(async() => { if (!await database.Comments.AnyAsync()) { User firstUser = await userManager.FindByNameAsync("User_1"); User secondUser = await userManager.FindByNameAsync("User_2"); IEnumerable <Supplement> supplements = database.Supplements; bool firstUserComment = true; foreach (Supplement supplement in supplements) { foreach (Comment commentInSupplement in comentsInSupplement) { Comment comment = new Comment { PublishDate = commentInSupplement.PublishDate, Content = commentInSupplement.Content, SupplementId = supplement.Id }; comment.AuthorId = firstUserComment ? firstUser.Id : secondUser.Id; firstUserComment = !firstUserComment; supplement.Comments.Add(comment); } } await database.SaveChangesAsync(); } }) .GetAwaiter() .GetResult(); }
private static void SeedSubcategories(FitStoreDbContext database) { Task.Run(async() => { string[] proteinSubcategoryNames = new[] { Whey, Gainer, Isolate, }; int proteinCategoryId = await database.Categories.Where(c => c.Name == Protein).Select(c => c.Id).FirstAsync(); foreach (string proteinSubcategoryName in proteinSubcategoryNames) { bool isSubcategoryExisting = await database.Subcategories.AnyAsync(s => s.Name == proteinSubcategoryName); if (!isSubcategoryExisting) { Subcategory subcategory = new Subcategory { Name = proteinSubcategoryName, CategoryId = proteinCategoryId }; await database.Subcategories.AddAsync(subcategory); await database.SaveChangesAsync(); } } string[] aminoAcidsSubcategoryNames = new[] { Bcaa, Glutamine, PreWorkouts, }; int aminoAcidsCategoryId = await database.Categories.Where(c => c.Name == AminoAcids).Select(c => c.Id).FirstAsync(); foreach (string aminoAcidsSubcategoryName in aminoAcidsSubcategoryNames) { bool isSubcategoryExisting = await database.Subcategories.AnyAsync(s => s.Name == aminoAcidsSubcategoryName); if (!isSubcategoryExisting) { Subcategory subcategory = new Subcategory { Name = aminoAcidsSubcategoryName, CategoryId = aminoAcidsCategoryId }; await database.Subcategories.AddAsync(subcategory); await database.SaveChangesAsync(); } } string[] creatineSubcategoryNames = new[] { Monohydrate, Matrix }; int creatineCategoryId = await database.Categories.Where(c => c.Name == Creatine).Select(c => c.Id).FirstAsync(); foreach (string creatineSubcategoryName in creatineSubcategoryNames) { bool isSubcategoryExisting = await database.Subcategories.AnyAsync(s => s.Name == creatineSubcategoryName); if (!isSubcategoryExisting) { Subcategory subcategory = new Subcategory { Name = creatineSubcategoryName, CategoryId = creatineCategoryId }; await database.Subcategories.AddAsync(subcategory); await database.SaveChangesAsync(); } } string[] vitaminsSubcategoryNames = new[] { Multivitamins, FishOil }; int vitaminsCategoryId = await database.Categories.Where(c => c.Name == Vitamins).Select(c => c.Id).FirstAsync(); foreach (string vitaminsSubcategoryName in vitaminsSubcategoryNames) { bool isSubcategoryExisting = await database.Subcategories.AnyAsync(s => s.Name == vitaminsSubcategoryName); if (!isSubcategoryExisting) { Subcategory subcategory = new Subcategory { Name = vitaminsSubcategoryName, CategoryId = vitaminsCategoryId }; await database.Subcategories.AddAsync(subcategory); await database.SaveChangesAsync(); } } }) .GetAwaiter() .GetResult(); }