|
| 1 | +using System; |
| 2 | +using JsonApiDotNetCore.Extensions; |
| 3 | +using JsonApiDotNetCoreExample.Data; |
| 4 | +using Microsoft.AspNetCore.Builder; |
| 5 | +using Microsoft.AspNetCore.Hosting; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | + |
| 11 | +namespace OperationsExample |
| 12 | +{ |
| 13 | + public class Startup |
| 14 | + { |
| 15 | + public readonly IConfiguration Config; |
| 16 | + |
| 17 | + public Startup(IHostingEnvironment env) |
| 18 | + { |
| 19 | + var builder = new ConfigurationBuilder() |
| 20 | + .SetBasePath(env.ContentRootPath) |
| 21 | + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
| 22 | + .AddEnvironmentVariables(); |
| 23 | + |
| 24 | + Config = builder.Build(); |
| 25 | + } |
| 26 | + |
| 27 | + public virtual IServiceProvider ConfigureServices(IServiceCollection services) |
| 28 | + { |
| 29 | + var loggerFactory = new LoggerFactory(); |
| 30 | + loggerFactory.AddConsole(LogLevel.Warning); |
| 31 | + |
| 32 | + services.AddSingleton<ILoggerFactory>(loggerFactory); |
| 33 | + |
| 34 | + services.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Scoped); |
| 35 | + |
| 36 | + services.AddJsonApi<AppDbContext>(opt => opt.EnableOperations = true); |
| 37 | + |
| 38 | + return services.BuildServiceProvider(); |
| 39 | + } |
| 40 | + |
| 41 | + public virtual void Configure( |
| 42 | + IApplicationBuilder app, |
| 43 | + IHostingEnvironment env, |
| 44 | + ILoggerFactory loggerFactory, |
| 45 | + AppDbContext context) |
| 46 | + { |
| 47 | + context.Database.EnsureCreated(); |
| 48 | + |
| 49 | + loggerFactory.AddConsole(Config.GetSection("Logging")); |
| 50 | + app.UseJsonApi(); |
| 51 | + } |
| 52 | + |
| 53 | + public string GetDbConnectionString() => Config["Data:DefaultConnection"]; |
| 54 | + } |
| 55 | +} |
0 commit comments