|
| 1 | +namespace eShopWeb.Infrastructure |
| 2 | +{ |
| 3 | + using eShopWeb.Models; |
| 4 | + using Microsoft.AspNetCore.Builder; |
| 5 | + using Microsoft.EntityFrameworkCore; |
| 6 | + using Microsoft.Extensions.Logging; |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Threading.Tasks; |
| 11 | + |
| 12 | + public class CatalogContextSeed |
| 13 | + { |
| 14 | + public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0) |
| 15 | + { |
| 16 | + int retryForAvaiability = retry.Value; |
| 17 | + try |
| 18 | + { |
| 19 | + var context = (CatalogContext)applicationBuilder |
| 20 | + .ApplicationServices.GetService(typeof(CatalogContext)); |
| 21 | + |
| 22 | + //context.Database.Migrate(); |
| 23 | + context.Database.EnsureCreated(); |
| 24 | + |
| 25 | + if (!context.CatalogBrands.Any()) |
| 26 | + { |
| 27 | + context.CatalogBrands.AddRange( |
| 28 | + GetPreconfiguredCatalogBrands()); |
| 29 | + |
| 30 | + await context.SaveChangesAsync(); |
| 31 | + } |
| 32 | + |
| 33 | + if (!context.CatalogTypes.Any()) |
| 34 | + { |
| 35 | + context.CatalogTypes.AddRange( |
| 36 | + GetPreconfiguredCatalogTypes()); |
| 37 | + |
| 38 | + await context.SaveChangesAsync(); |
| 39 | + } |
| 40 | + |
| 41 | + if (!context.CatalogItems.Any()) |
| 42 | + { |
| 43 | + context.CatalogItems.AddRange( |
| 44 | + GetPreconfiguredItems()); |
| 45 | + |
| 46 | + await context.SaveChangesAsync(); |
| 47 | + } |
| 48 | + } |
| 49 | + catch (Exception ex) |
| 50 | + { |
| 51 | + if (retryForAvaiability < 10) |
| 52 | + { |
| 53 | + retryForAvaiability++; |
| 54 | + var log = loggerFactory.CreateLogger("catalog seed"); |
| 55 | + log.LogError(ex.Message); |
| 56 | + await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static IEnumerable<CatalogBrand> GetPreconfiguredCatalogBrands() |
| 62 | + { |
| 63 | + return new List<CatalogBrand>() |
| 64 | + { |
| 65 | + new CatalogBrand() { Brand = "Azure"}, |
| 66 | + new CatalogBrand() { Brand = ".NET" }, |
| 67 | + new CatalogBrand() { Brand = "Visual Studio" }, |
| 68 | + new CatalogBrand() { Brand = "SQL Server" }, |
| 69 | + new CatalogBrand() { Brand = "Other" } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + static IEnumerable<CatalogType> GetPreconfiguredCatalogTypes() |
| 74 | + { |
| 75 | + return new List<CatalogType>() |
| 76 | + { |
| 77 | + new CatalogType() { Type = "Mug"}, |
| 78 | + new CatalogType() { Type = "T-Shirt" }, |
| 79 | + new CatalogType() { Type = "Sheet" }, |
| 80 | + new CatalogType() { Type = "USB Memory Stick" } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + static IEnumerable<CatalogItem> GetPreconfiguredItems() |
| 85 | + { |
| 86 | + return new List<CatalogItem>() |
| 87 | + { |
| 88 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" }, |
| 89 | + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" }, |
| 90 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" }, |
| 91 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" }, |
| 92 | + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" }, |
| 93 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" }, |
| 94 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" }, |
| 95 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" }, |
| 96 | + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" }, |
| 97 | + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" }, |
| 98 | + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" }, |
| 99 | + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" } |
| 100 | + }; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments