|
| 1 | +using GettingStarted.Data; |
| 2 | +using GettingStarted.Models; |
| 3 | +using JsonApiDotNetCore; |
1 | 4 | using Microsoft.AspNetCore.Builder;
|
2 |
| -using Microsoft.Extensions.DependencyInjection; |
3 | 5 | using Microsoft.EntityFrameworkCore;
|
4 |
| -using JsonApiDotNetCore; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
5 | 7 |
|
6 | 8 | namespace GettingStarted
|
7 | 9 | {
|
8 | 10 | public sealed class Startup
|
9 | 11 | {
|
| 12 | + // This method gets called by the runtime. Use this method to add services to the container. |
10 | 13 | public void ConfigureServices(IServiceCollection services)
|
11 | 14 | {
|
12 |
| - services.AddDbContext<SampleDbContext>(options => |
13 |
| - { |
14 |
| - options.UseSqlite("Data Source=sample.db"); |
15 |
| - }); |
| 15 | + services.AddDbContext<SampleDbContext>( |
| 16 | + options => options.UseSqlite("Data Source=sample.db")); |
16 | 17 |
|
17 |
| - var mvcBuilder = services.AddMvcCore(); |
18 |
| - services.AddJsonApi( |
19 |
| - options => options.Namespace = "api", |
20 |
| - discover => discover.AddCurrentAssembly(), mvcBuilder: mvcBuilder); |
| 18 | + services.AddJsonApi<SampleDbContext>( |
| 19 | + options => options.Namespace = "api"); |
21 | 20 | }
|
22 | 21 |
|
| 22 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
23 | 23 | public void Configure(IApplicationBuilder app, SampleDbContext context)
|
24 | 24 | {
|
25 |
| - // indices need to be reset |
26 | 25 | context.Database.EnsureDeleted();
|
27 | 26 | context.Database.EnsureCreated();
|
| 27 | + CreateSampleData(context); |
| 28 | + |
28 | 29 | app.UseJsonApi();
|
29 | 30 | }
|
| 31 | + |
| 32 | + private static void CreateSampleData(SampleDbContext context) |
| 33 | + { |
| 34 | + context.Articles.AddRange(new Article |
| 35 | + { |
| 36 | + Title = "What's new in JsonApiDotNetCore", |
| 37 | + Author = new Person |
| 38 | + { |
| 39 | + Name = "John Doe" |
| 40 | + } |
| 41 | + }, new Article |
| 42 | + { |
| 43 | + Title = ".NET Core Best Practices", |
| 44 | + Author = new Person |
| 45 | + { |
| 46 | + Name = "Microsoft" |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + context.SaveChanges(); |
| 51 | + } |
30 | 52 | }
|
31 | 53 | }
|
0 commit comments