public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } //Init Seed Data var cities = new List <City>() { new City() { Name = "Lagos", Description = "Center of excellence", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Ikeja", Description = "Computer Village" }, new PointOfInterest() { Name = "Victoria Island", Description = "Home of the rich men" } } }, new City() { Name = "Abeokuta", Description = "Rockcity", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Ayetoro", Description = "Jenifer's Village" }, new PointOfInterest() { Name = "Itoku", Description = "Home of adire" } } }, new City() { Name = "Ibadan", Description = "Center of amala dudu", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Challenge", Description = "Main garage" }, new PointOfInterest() { Name = "Jerico GRA", Description = "Estate for the rich kid" } } }, }; context.Cities.AddRange(cities); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); app.UseCors("CorsPolicy"); app.UseMvc(); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { //ILoggerFactory is a built-in service which allows us to create different type of logging in our project. loggerFactory.AddConsole(); loggerFactory.AddDebug(); loggerFactory.AddNLog(); //for other providers, you should add it like this: loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } cityInfoContext.EnsureSeedDataForContext(); //AutoMapper is convention based, it will map properties with the same name in the source and destination. //By default, it will ignore NullReference Exceptions from source to target. AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointsOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); //This will handle the status pages when it's needed. app.UseStatusCodePages(); /* We add this after the exception handler, so that middleware can potentially catch exceptions before * handling the request over to MVC and, more importantly, handle exceptions and return the correct response * when an exception happens in the MVC-related code we'll write. * At this moment, MVC middleware will handle MVC requests */ app.UseMvc(); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <City, CityWithoutPointsOfInterestDto>(); cfg.CreateMap <City, CityDto>(); cfg.CreateMap <PointOfInterest, PointOfInterestDto>(); cfg.CreateMap <PointOfInterestForCreationDto, PointOfInterest>(); cfg.CreateMap <PointOfInterestForUpdateDto, PointOfInterest>(); cfg.CreateMap <PointOfInterest, PointOfInterestForUpdateDto>(); }); app.UseMvc(); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); // But there's a shortcut with most providers loggerFactory.AddNLog(); // Anything logged gets automagically logged with NLog as well, since we've added it here if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } cityInfoContext.EnsureSeedDataForContext(); //app.UseStatusCodePages(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); // Convention based; will map property names on the source object to the same names on the destination object. // If property doesn't exist, it will be ignored. cfg.CreateMap <Entities.City, Models.CityDto>(); // CityDto includes pointOfInterests, so these need mapping too even if no requests are made for the pointOfInterest cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); // automapper ignores additional properties cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); app.UseMvc(); // Added to the pipeline after logger so that exceptions can be caught before handing the request over to MVC. And so exceptions can be caught in the MVC code //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with the big park.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "Most visited park in the USA." }, new PointOfInterest() { Name = "Empire State Building", Description = "A big skyscraper in Manhattan." } } }, new City() { Name = "Antwerp", Description = "The one with the unfinished cathedra.l", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cathedral of Our Lady", Description = "A gothic style cathedral." }, new PointOfInterest() { Name = "Antwerp Central Station", Description = "Finest example of railway architecture in Belgium." } } }, new City() { Name = "Paris", Description = "The one with the big tower.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffel Tower", Description = "A wrought iron lattice tower on the Champ de Mars." }, new PointOfInterest() { Name = "The Louvre", Description = "The world's largest museum." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } // Init seed data #region SeedData var cities = new List <City>() { new City() { Name = "New York City", Description = "The one that big park", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." } } }, new City() { Name = "Antwrep", Description = "The one that big park", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." }, } }, new City() { Name = "Paris", Description = "The one that big park", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." }, } } }; #endregion context.Cities.AddRange(cities); context.SaveChanges(); }
//todo ****************************************************************************************** //todo ***********************************Configure********************************************** //todo ****************************************************************************************** //! This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /* It uses services that are registered and configured in that method. It's used to specify how an ASP.NET * Core application will respond to individual HTTP requests */ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) //Add new input parameter to be accepted by dependency injection container { //todo************* L o g g e r *********************!// // The below line added by default in .NET 2 //xloggerFactory.AddConsole(); // This log into the console window // The below line added by default in .NET 2 //x loggerFactory.AddDebug(); // This logs to debug window // This configures where the logger logs to //x loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); // // The above line has the below shortcut loggerFactory.AddNLog(); //todo/////////////////////////////////////////////////////// //todo************* Exceptions Handler *********************!// //! The exception handler middleware can potentially catch exceptions before //! handing the request over to MVC and, more importantly handle exceptions //! and returns response when an exception happens in the MVC-related code. if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //todo//////////////////////////////////////////////////// //todo**************** Seed Data *********************!// cityInfoContext.EnsureSeedDataForContext(); //todo//////////////////////////////////////////////////// //todo**************** Status codes *********************!// //! To show the status code as a text on the web page app.UseStatusCodePages(); //todo//////////////////////////////////////////////////// //todo************* Auto Mapper *********************!// AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPOIDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointsOfInterest, Models.PointsOfInterestsDto>(); cfg.CreateMap <Models.PointOfInterestsForCreationDto, Entities.PointsOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointsOfInterest>(); cfg.CreateMap <Entities.PointsOfInterest, Models.PointOfInterestForUpdateDto>(); }); //todo//////////////////////////////////////////////////// //todo************* M V C *********************!// //! Added to the request pipeline, after exceptions are handled. So then //! MVC will handle http requests app.UseMvc(); //todo//////////////////////////////////////////////////// //todo ************** Unneeded code ******************// //xapp.Run(async (context) => //x{ //x throw new Exception("EXample exception"); //x //await context.Response.WriteAsync("Hello World!"); //x}); //todo//////////////////////////////////////////////////// }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } //seed data var cities = new List <City>() { new City() { Name = "Cleveland", Description = "Party wit da best in the land", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "R&R Hall Of Fame", Description = "Weowweow", }, new PointOfInterest() { Name = "The Flats", Description = "Drink Drank Drunk", } } }, new City() { Name = "Shittsburgh", Description = "Party wit nobody cool", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Mountain", Description = "Pretty", }, new PointOfInterest() { Name = "Steelers Fans", Description = "Not pretty", } } }, new City() { Name = "Detroit", Description = "Maaaaan that be real gross", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cars", Description = "Omg wow cars", }, new PointOfInterest() { Name = "The Hood", Description = "Gun violence", } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var Cities = new List <City>() { new City() { Name = "New York City", Description = "The one with the big park.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urbain park in the USA" }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper in Midtown." } } }, new City() { Name = "Antwerp", Description = "The one with the cathedral they never finished.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cethedral of Our Lady", Description = "Gothic style cathedral" }, new PointOfInterest() { Name = "Antwerp Central Station", Description = "A fine railway structure" } } }, new City() { Name = "Paris", Description = "The one with the point tower.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "The Eiffel Town", Description = "A wrought iron lattice tower" }, new PointOfInterest() { Name = "The Louvre", Description = "The world's Largest museum." } } } }; context.Cities.AddRange(Cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } // init seed data... var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with that big park.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in US." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper." } } }, new City() { Name = "Antwerp", Description = "The one with the cathedral that was never really finished.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cathedral", Description = "Not finished." }, new PointOfInterest() { Name = "Something", Description = "Bla bla bla." } } }, new City() { Name = "Paris", Description = "The one with that big tower.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffel Tower", Description = "Famous tower." }, new PointOfInterest() { Name = "Something", Description = "Bla bla bla." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { //loggerFactory.AddConsole(); //loggerFactory.AddDebug(); //loggerFactory.AddProvider(new NLogLoggerProvider()); loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/error"); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); app.UseMvc(); Mapper.Initialize(cfg => { // Way 1 cfg.CreateMap <City, CityWithoutPointsOfInterestDto>(); cfg.CreateMap <City, CityDto>(); cfg.CreateMap <PointOfInterestForCreationDto, PointOfInterest>(); cfg.CreateMap <PointOfInterest, PointOfInterestDto>(); cfg.CreateMap <PointOfInterestForUpdateDto, PointOfInterest>(); cfg.CreateMap <PointOfInterest, PointOfInterestForUpdateDto>(); // cfg.CreateMap<Entities.PointOfInterest, Models.PointOfInterestDto>(); // Don't need for AutoMapper 7.x.x // Way 2 //cfg.CreateMap(typeof(City), typeof(CityWithoutPointsOfInterestDto)); }); // Convention-based routing // Typically for using MVC framework to build a web application with HTML-returning views. // For web api, using attribute-based routing. //app.UseMvc(config => //{ // config.MapRoute( // name: "Default", // template: "{controller}/{action}/{id?}", // defaults: new { controller = "Home", action = "Index" }); //}); //app.Run(async (context) => //{ // throw new Exception("example exception"); // await context.Response.WriteAsync("Hello World!"); //}); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); // Auto map our entities to our Dto's AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); app.UseMvc(); }
public static void EnsureSeedForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with the big park", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." } } }, new City() { Name = "Antwerp", Description = "The one with the cathederal that was never really finished", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cathedral of Our Lady", Description = "A Gothic style cathedral." }, new PointOfInterest() { Name = "Antwerp Central Station", Description = "The finest example of railway architecture in Belgium." } } }, new City() { Name = "Paris", Description = "The one with the big tower", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffel Tower", Description = "A wrought iron lattice tower on the Champ de Mars." }, new PointOfInterest() { Name = "The Louvre", Description = "The worlds larget museum." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var cities = new List <City>() { new City() { Name = "New York", Description = "The one with that big park.", PointsOfinterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited one." }, new PointOfInterest() { Name = "Empire State Building", Description = "A very big building." } } }, new City() { Name = "Antwerp", Description = "The one with the cathedral that was never finished.", PointsOfinterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited one." }, new PointOfInterest() { Name = "Empire State Building", Description = "A very big building." } } }, new City() { Name = "Paris", Description = "The one with that big tower.", PointsOfinterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited one." }, new PointOfInterest() { Name = "Empire State Building", Description = "A very big building." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { // Log to the console loggerFactory.AddConsole(); // Log to the debug window loggerFactory.AddDebug(); loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } /// Call the ensure seed for database method cityInfoContext.EnsureSeedDataForContext(); /// Display status codes app.UseStatusCodePages(); /// Auto mapping from model entities to model dto's AutoMapper.Mapper.Initialize(config => { /// GET config.CreateMap <Infrastructure.Entities.City, Models.CityWithoutPointsOfInterestDto>(); config.CreateMap <Infrastructure.Entities.City, Models.CityDto>(); config.CreateMap <Infrastructure.Entities.PointOfInterest, Models.PointOfInterestDto>(); /// CREATE config.CreateMap <Models.PointOfInterestForCreationDto, Infrastructure.Entities.PointOfInterest>(); /// PUT config.CreateMap <Models.PointOfInterestForUpdateDto, Infrastructure.Entities.PointOfInterest>(); /// PATCH config.CreateMap <Infrastructure.Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); /// Use the core mvc pattern app.UseMvc(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } // init seed data var cities = new List <City>() { new City() { Name = "Court House Green", Description = "Austin Drive, Coventry, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "CV6 7NS" }, new PointOfInterest() { Name = "Telephone", Description = "024 7666 4477" }, } }, new City() { Name = "London Colney", Description = "Colney Fields Shopping Park, St. Albans, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "AL2 1AB" }, new PointOfInterest() { Name = "Telephone", Description = "01707 429220" }, } }, new City() { Name = "Nuneaton", Description = "Vicarage Street, Nuneaton, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "CV11 4XS" }, new PointOfInterest() { Name = "Telephone", Description = "02476 375880" }, } }, new City() { Name = "Milton Keynes", Description = "799 Witan Gate, Milton Keynes, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "MK9 2FW" }, new PointOfInterest() { Name = "Telephone", Description = "01908 674154" }, } }, new City() { Name = "Leicester North", Description = "501 Melton Road, Leicester, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "LE4 7SJ" }, new PointOfInterest() { Name = "Telephone", Description = "0116 2018180" }, } }, new City() { Name = "Southampton Portswood", Description = "224 Portswood Road, Southampton, England", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Post Code:", Description = "SO17 2LB" }, new PointOfInterest() { Name = "Telephone", Description = "023 81242821" }, } }, }; context.Cities.AddRange(cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with that big park", PointsOfInterest = new List <PointOfInterest> { new PointOfInterest() { Name = "Cental Park", Description = "The most visited park in US" }, new PointOfInterest() { Name = "Empire State Building", Description = "A skyscraper located in midtown of Manhattan" } } }, new City() { Name = "Toronto", Description = "The one with one of the tallest towers", PointsOfInterest = new List <PointOfInterest> { new PointOfInterest() { Name = "CN Tower", Description = "The highest tower in North America" }, new PointOfInterest() { Name = "High Park", Description = "The best park to visit in Toronto" } } }, new City() { Name = "Montreal", Description = "The one where everybody speaks French", PointsOfInterest = new List <PointOfInterest> { new PointOfInterest() { Name = "The River", Description = "A well-known river of the city of Montreal" }, new PointOfInterest() { Name = "The Castle", Description = "A huge and beatiful castle" } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } //init seed data var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with the big park.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." } } }, new City() { Name = "Rapa Nui", Description = "The one with the stone men sculptures.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Anakena", Description = "The most visited beach located in Rapa Nui." }, new PointOfInterest() { Name = "Rano Raraku", Description = "The most visited volcano located in Rapa Nui." } } }, new City() { Name = "Paris", Description = "The one with that big tower", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffel Tower", Description = "A wrought iron lattice tower on the Champ de Mars." }, new PointOfInterest() { Name = "The louvre", Description = "The world's largest museum." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <CityEntity, CityDto>(); cfg.CreateMap <CityEntity, CityWithPointOfInterestDto>(); cfg.CreateMap <PointOfInterestEntity, PointOfInterestDto>(); cfg.CreateMap <PointOfInterest, PointOfInterestEntity>(); }); app.UseMvc(); }
// ConfigureServices is an optional method. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext context) { // dependecy injection // built-in logger system //loggerFactory.AddConsole(); // log to the console window //loggerFactory.AddDebug(); // log to debug window // add NLog from Nuget //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); // or loggerFactory.AddNLog(); // it writes to source\repos\CityInfo.API\CityInfo.API\bin\Debug\netcoreapp2.1 // Configure method uses services that are registered and configured in that method // it's used to specify how an asp.net core app will respond to individual HTTP requests. // use MVC for handling HTTP request if (env.IsDevelopment()) { // this configures request pipeline by adding the developer exception page middleware // So, when an exception is thrown this middleware will handle it app.UseDeveloperExceptionPage(); } else { // IHostingEnvironment env gives us info about our hosting environment, app name vs // şu an development environmentındayız ancak istersek bunu DEBUG // altından production'a da çevirebilirz // eğer development'ta değilsek hata buraya düşecek app.UseExceptionHandler(); } context.EnsureSeedDataForContext(); // add data to database // add statuscode middleware to the pipeline app.UseStatusCodePages(); // configure automapper AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); // add mvc middleware to the pipeline app.UseMvc(); //exception handler middleware // show only in development envirenment (not in production environment) //app.Run((context) => //{ // throw new Exception("Sample Exception"); //}); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); loggerFactory.AddNLog(); //üstteki yerine kullanılabilir. if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } //Cors Config - her şeye izin verildi. app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()); cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); //Json çıktı verecek olan middleware'ı ekler. app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); } ); //automapper ile entity'mizi model'e mapledik. source -> entities.city, destination -> models.city... //bu demek oluyor ki, biz onu controller'da kullanabiliriz. //it'll map property names on the source object to the same property names on the destination object //default olarak, no reference exceptionlarını ignore'lar from source to target //eğer bir property doesnt exist ise, it'll be ignored. AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); app.UseMvc(); app.Run(async(context) => { await context.Response.WriteAsync("Hello World!"); }); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { //If we already have data, we don't insert anything. if (context.Cities.Any()) { return; } // init seed data var cities = new List <City>() { new City() { Name = "Buenos Aires", Description = "The Paris of South America", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Obelisco", Description = "Big Obelisk in the city center" }, new PointOfInterest() { Name = "Pink House", Description = "The government house" }, } }, new City() { Name = "Resistencia", Description = "The City of Scultures", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cathedral", Description = "A modern style cathedral" }, new PointOfInterest() { Name = "Democracy Park", Description = "Green space perfect for open activities" }, } }, new City() { Name = "Corrientes", Description = "The City with 'Paje' ", PointOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Costanera", Description = "A space near the river" } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/error"); } cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); app.UseMvc(); //app.Run((context) => //{ // throw new Exception("Example exception"); //}); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } // init seed data var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with that big park.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhattan." }, } }, new City() { Name = "Antwerp", Description = "The one with the cathedral that was never really finished.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Cathedral", Description = "A Gothic style cathedral, conceived by architects Jan and Pieter Appelmans." }, new PointOfInterest() { Name = "Antwerp Central Station", Description = "The the finest example of railway architecture in Belgium." }, } }, new City() { Name = "Paris", Description = "The one with that big tower.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffel Tower", Description = "A wrought iron lattice tower on the Champ de Mars, named after engineer Gustave Eiffel." }, new PointOfInterest() { Name = "The Louvre", Description = "The world's largest museum." }, } } }; context.Cities.AddRange(cities); context.SaveChanges(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { loggerFactory.AddConsole(); loggerFactory.AddDebug(); //Long way to add the nLogger extension to loggerFactory //loggerFactory.AddProvider(new NLog.Extensions.Logging.NLogLoggerProvider()); //Short-cut. Now we're also logging out to a file. loggerFactory.AddNLog(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } //Seed the database with data cityInfoContext.EnsureSeedDataForContext(); //Create mappings from the application's DTOs to their respective entity classes returned from Sql. AutoMapper.Mapper.Initialize(cfg => { //Syntax is CreateMap(SourceType, DestinationType). In other words: From an Entity to a DTO object cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); //You can create multiple mappings. In this case, getting a city back should match to both the CityDto and the CityWithoutPointsOfInterestDto cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); //For POST (i.e. creating a POI, the arguments are reveresed because the Entities.POI is the destination type). cfg.CreateMap <Models.PointOfInterestForCreation, Entities.PointOfInterest>(); //For updating with PUT cfg.CreateMap <Models.PointOfInterestForUpdate, Entities.PointOfInterest>(); //For updating with Patch. We need the other way around since we're mapping an entity to a POIForUpdate dto. cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdate>(); }); // Show Error pages when the consuming browser gets an error (e.g. instead of a silent 404 error) //app.UseStatusCodePages(); app.UseMvc(); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, CityInfoContext cityInfoContext) { //loggerFactory.AddNLog(); - 7/9/18 - Not supported by 2.0 if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } // seed the database cityInfoContext.EnsureSeedDataForContext(); app.UseStatusCodePages(); // database row to model information (return only the data we need) AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap <Entities.City, Models.CityWithoutPointsOfInterestDto>(); cfg.CreateMap <Entities.City, Models.CityDto>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestDto>(); cfg.CreateMap <Models.PointOfInterestForCreationDto, Entities.PointOfInterest>(); cfg.CreateMap <Models.PointOfInterestForUpdateDto, Entities.PointOfInterest>(); cfg.CreateMap <Entities.PointOfInterest, Models.PointOfInterestForUpdateDto>(); }); app.UseMvc(); }
public static void EnsureSeedDataForContext(this CityInfoContext context) { if (context.Cities.Any()) { return; } var cities = new List <City>() { new City() { Name = "New York City", Description = "The one with that big park, hey now!", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Central Park", Description = "The most visited urban park in the United States." }, new PointOfInterest() { Name = "Empire State Building", Description = "A 102-story skyscraper located in Midtown Manhatten." } } }, new City() { Name = "Antwerp", Description = "The one with the cathedral that was never really finished.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Some Cathedral", Description = "They ran out of money!" }, new PointOfInterest() { Name = "Antwerp Zoo", Description = "Oldest animal park in the counry and one of the oldest in the world.." } } }, new City() { Name = "Paris", Description = "The one with that big tower.", PointsOfInterest = new List <PointOfInterest>() { new PointOfInterest() { Name = "Eiffle Tower", Description = "Wrought iron lattice tower on the Champ de Mars in Paris." }, new PointOfInterest() { Name = "Nothign else", Description = "Please move on." } } } }; context.Cities.AddRange(cities); context.SaveChanges(); }