public NationalityRepository(NationalityContext context) { _context = context; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, NationalityContext nationalityContext, PlayerContext leagueContext, MatchContext matchContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } nationalityContext.EnsureSeedDataForContext(); leagueContext.EnsureSeedDataForContext(); matchContext.EnsureSeedDataForContext(); app.UseCors("MyPolicy"); app.UseStaticFiles(); app.UseSpaStaticFiles(); Mapper.Initialize(cfg => { cfg.CreateMap <Player, PlayerDto>(MemberList.Destination) .ForMember(dest => dest.PositionName, op => op.MapFrom(src => src.Position.ShortCode)) .ForMember(dest => dest.TeamName, op => op.MapFrom(src => src.Team.Name)) .ForMember(dest => dest.DateOfBirth, op => op.MapFrom(src => src.DateOfBirth.ToString("MM/dd/yyyy"))); cfg.CreateMap <Manager, ManagerDto>(MemberList.Destination); cfg.CreateMap <Player, PlayerNameSurnameDto>(MemberList.Destination); cfg.CreateMap <Nationality, NationalityNameDto>(MemberList.Destination); cfg.CreateMap <League, LeagueNameNationalityDto>(MemberList.Destination); cfg.CreateMap <Player, PlayerExtendedDto>(MemberList.Destination) .ForMember(dest => dest.DateOfBirth, op => op.MapFrom(src => src.DateOfBirth.ToString("d"))) .ForMember(dest => dest.PositionName, op => op.MapFrom(src => src.Position.PositionName)) .ForMember(dest => dest.LeagueName, op => op.MapFrom(src => src.Team.League.Name)) .ForMember(dest => dest.TeamName, op => op.MapFrom(src => src.Team.Name)) .ForMember(dest => dest.NationalLeagueName, op => op.MapFrom(src => src.Team.League.Nationality.Name)) .ForMember(dest => dest.FlagNational, op => op.MapFrom(src => src.Nationality.PngImage)) .ForMember(dest => dest.FlagNationalLeague, op => op.MapFrom(src => src.Team.League.Nationality.PngImage)); cfg.CreateMap <Match, MatchDto>(MemberList.Destination) .ForMember(dest => dest.MatchDay, op => op.MapFrom(src => src.MatchDay.ToString("MM/dd/yyyy"))); cfg.CreateMap <Team, TeamDto>(MemberList.Destination) .ForMember(dest => dest.NameStadium, op => op.MapFrom(src => src.Stadium.Name)) .ForMember(dest => dest.CapacityStadium, op => op.MapFrom(src => src.Stadium.Capacity)) .ForMember(dest => dest.NameLeague, op => op.MapFrom(src => src.League.Name)); cfg.CreateMap <Match, MatchDetailsDto>(MemberList.Destination) .ForMember(dest => dest.MatchDay, op => op.MapFrom(src => src.MatchDay.ToString("MM/dd/yyyy"))) .ForMember(dest => dest.Attendance, op => op.MapFrom(src => src.MatchDetails.Attendance)) .ForMember(dest => dest.HomeTeamShots, op => op.MapFrom(src => src.MatchDetails.HomeTeamShots)) .ForMember(dest => dest.AwayTeamShots, op => op.MapFrom(src => src.MatchDetails.AwayTeamShots)) .ForMember(dest => dest.HomeTeamShotsOnTarget, op => op.MapFrom(src => src.MatchDetails.HomeTeamShotsOnTarget)) .ForMember(dest => dest.AwayTeamShotsOnTarget, op => op.MapFrom(src => src.MatchDetails.AwayTeamShotsOnTarget)) .ForMember(dest => dest.HomeTeamWoodWork, op => op.MapFrom(src => src.MatchDetails.HomeTeamWoodWork)) .ForMember(dest => dest.AwayTeamWoodWork, op => op.MapFrom(src => src.MatchDetails.AwayTeamWoodWork)) .ForMember(dest => dest.HomeTeamCorners, op => op.MapFrom(src => src.MatchDetails.HomeTeamCorners)) .ForMember(dest => dest.AwayTeamCorners, op => op.MapFrom(src => src.MatchDetails.AwayTeamCorners)) .ForMember(dest => dest.HomeTeamFoulsCommitted, op => op.MapFrom(src => src.MatchDetails.HomeTeamFoulsCommitted)) .ForMember(dest => dest.AwayTeamFoulsCommitted, op => op.MapFrom(src => src.MatchDetails.AwayTeamFoulsCommitted)) .ForMember(dest => dest.HomeTeamOffsides, op => op.MapFrom(src => src.MatchDetails.HomeTeamOffsides)) .ForMember(dest => dest.AwayTeamOffsides, op => op.MapFrom(src => src.MatchDetails.AwayTeamOffsides)) .ForMember(dest => dest.HomeYellowCards, op => op.MapFrom(src => src.MatchDetails.HomeYellowCards)) .ForMember(dest => dest.AwayYellowCards, op => op.MapFrom(src => src.MatchDetails.AwayYellowCards)) .ForMember(dest => dest.HomeTeamRedCards, op => op.MapFrom(src => src.MatchDetails.HomeTeamRedCards)) .ForMember(dest => dest.AwayTeamRedCards, op => op.MapFrom(src => src.MatchDetails.AwayTeamRedCards)); }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); }
public static void EnsureSeedDataForContext(this NationalityContext context) { if (context.Nationalities.Any()) { return; } var html = @"https://www.worldatlas.com/aatlas/ctycodes.htm"; HtmlWeb web = new HtmlWeb { AutoDetectEncoding = false, OverrideEncoding = Encoding.GetEncoding("iso-8859-1") }; var htmldoc = web.Load(html); var nodesCountries = htmldoc.DocumentNode.SelectNodes($"//main/div/article/div/table//tr/td"); var linkCoutries = new List <Nationality>(); var fifaCodes = AddFifaCodes(); for (int i = 5; i < nodesCountries.Count - 1; i++) { var wikiData = SeedWikipedia.GetWiki(Regex.Replace(nodesCountries[i].InnerText, $"\\r\\n\\s", "").Trim()).Result; linkCoutries.Add(item: new Nationality { Name = Regex.Replace(nodesCountries[i].InnerText, $"\\r\\n\\s", "").Trim(), CodeCountryTwoChars = Regex.Replace(nodesCountries[++i].InnerText, $"\\r\\n\\s", "").Trim(), PngImage = GetImageAsBase64Url(Regex.Replace(nodesCountries[i].InnerText, $"\\r\\n\\s", "").Trim()).Result, CodeCountryThreeChars = nodesCountries[++i].InnerText, Description = wikiData.Description, WikiLink = wikiData.Link, FifaCodeCountry = fifaCodes.FirstOrDefault(f => f.Key == nodesCountries[i].InnerText).Value }); i += 2; } var wikiScot = SeedWikipedia.GetWiki("Scotland").Result; linkCoutries.Add(new Nationality { Name = "Scotland", CodeCountryThreeChars = "SCO", CodeCountryTwoChars = "SC", Description = wikiScot.Description, WikiLink = wikiScot.Link, PngImage = "No data", FifaCodeCountry = "SCO" }); wikiScot = SeedWikipedia.GetWiki("Wales").Result; linkCoutries.Add(new Nationality { Name = "Wales", CodeCountryThreeChars = "WAL", CodeCountryTwoChars = "WL", Description = wikiScot.Description, WikiLink = wikiScot.Link, PngImage = "No data", FifaCodeCountry = "WAL" }); wikiScot = SeedWikipedia.GetWiki("Northern Ireland").Result; linkCoutries.Add(new Nationality { Name = "Northern Ireland", CodeCountryThreeChars = "NIR", CodeCountryTwoChars = "NX", Description = wikiScot.Description, PngImage = "No data", WikiLink = wikiScot.Link, FifaCodeCountry = "NIR" }); wikiScot = SeedWikipedia.GetWiki("Kosovo").Result; linkCoutries.Add(new Nationality { Name = "Kosovo", CodeCountryThreeChars = "KVX", CodeCountryTwoChars = "KV", Description = wikiScot.Description, PngImage = "No data", WikiLink = wikiScot.Link, FifaCodeCountry = "KVX" }); context.Nationalities.AddRange(linkCoutries); context.SaveChanges(); }