Skip to content

Commit 071b922

Browse files
committed
example: Add People and relationship to TodoItems
1 parent 87531cb commit 071b922

File tree

10 files changed

+174
-2
lines changed

10 files changed

+174
-2
lines changed

JsonApiDotNetCore/Services/JsonApiService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ private static Dictionary<string, object> GetAttributesFromResource(IJsonApiReso
139139
private static void SendResponse(HttpContext context, ObjectResult result)
140140
{
141141
context.Response.StatusCode = result.StatusCode ?? 500;
142-
context.Response.WriteAsync(result.Value.ToString());
143142
context.Response.ContentType = "application/vnd.api+json";
143+
context.Response.WriteAsync(result.Value.ToString());
144144
context.Response.Body.Flush();
145145
}
146146

@@ -166,7 +166,7 @@ private Dictionary<string, object> BuildRelationshipsObject(JsonApiContext jsonA
166166
jsonApiContext.Route.Model.GetProperties().Where(propertyInfo => propertyInfo.GetMethod.IsVirtual).ToList().ForEach(
167167
virtualProperty =>
168168
{
169-
relationships.Add(virtualProperty.Name, GetRelationshipLinks(jsonApiContext, resource, virtualProperty.Name));
169+
relationships.Add(virtualProperty.Name, GetRelationshipLinks(jsonApiContext, resource, virtualProperty.Name.ToCamelCase()));
170170
});
171171
return relationships;
172172
}

JsonApiDotNetCoreExample/Data/ApplicationDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public ApplicationDbContext (DbContextOptions options)
1010
{ }
1111

1212
public DbSet<TodoItem> TodoItems { get; set; }
13+
public DbSet<Person> People { get; set; }
1314
}
1415
}

JsonApiDotNetCoreExample/Migrations/20160824204356_AddPeople.Designer.cs

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace JsonApiDotNetCoreExample.Migrations
6+
{
7+
public partial class AddPeople : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.CreateTable(
12+
name: "People",
13+
columns: table => new
14+
{
15+
Id = table.Column<int>(nullable: false)
16+
.Annotation("Npgsql:ValueGeneratedOnAdd", true),
17+
Name = table.Column<string>(nullable: true)
18+
},
19+
constraints: table =>
20+
{
21+
table.PrimaryKey("PK_People", x => x.Id);
22+
});
23+
24+
migrationBuilder.AddColumn<int>(
25+
name: "PersonId",
26+
table: "TodoItems",
27+
nullable: false,
28+
defaultValue: 0);
29+
30+
migrationBuilder.CreateIndex(
31+
name: "IX_TodoItems_PersonId",
32+
table: "TodoItems",
33+
column: "PersonId");
34+
35+
migrationBuilder.AddForeignKey(
36+
name: "FK_TodoItems_People_PersonId",
37+
table: "TodoItems",
38+
column: "PersonId",
39+
principalTable: "People",
40+
principalColumn: "Id",
41+
onDelete: ReferentialAction.Cascade);
42+
}
43+
44+
protected override void Down(MigrationBuilder migrationBuilder)
45+
{
46+
migrationBuilder.DropForeignKey(
47+
name: "FK_TodoItems_People_PersonId",
48+
table: "TodoItems");
49+
50+
migrationBuilder.DropIndex(
51+
name: "IX_TodoItems_PersonId",
52+
table: "TodoItems");
53+
54+
migrationBuilder.DropColumn(
55+
name: "PersonId",
56+
table: "TodoItems");
57+
58+
migrationBuilder.DropTable(
59+
name: "People");
60+
}
61+
}
62+
}

JsonApiDotNetCoreExample/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,41 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1515
modelBuilder
1616
.HasAnnotation("ProductVersion", "1.0.0-rtm-21431");
1717

18+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Person", b =>
19+
{
20+
b.Property<int>("Id")
21+
.ValueGeneratedOnAdd();
22+
23+
b.Property<string>("Name");
24+
25+
b.HasKey("Id");
26+
27+
b.ToTable("People");
28+
});
29+
1830
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
1931
{
2032
b.Property<int>("Id")
2133
.ValueGeneratedOnAdd();
2234

2335
b.Property<string>("Name");
2436

37+
b.Property<int>("PersonId");
38+
2539
b.HasKey("Id");
2640

41+
b.HasIndex("PersonId");
42+
2743
b.ToTable("TodoItems");
2844
});
45+
46+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
47+
{
48+
b.HasOne("JsonApiDotNetCoreExample.Models.Person", "Owner")
49+
.WithMany("TodoItems")
50+
.HasForeignKey("PersonId")
51+
.OnDelete(DeleteBehavior.Cascade);
52+
});
2953
}
3054
}
3155
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Attributes;
3+
using JsonApiDotNetCoreExample.Resources;
4+
5+
namespace JsonApiDotNetCoreExample.Models
6+
{
7+
[JsonApiResource(typeof(PersonResource))]
8+
public class Person
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
13+
public virtual List<TodoItem> TodoItems { get; set; }
14+
}
15+
}

JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ public class TodoItem
88
{
99
public int Id { get; set; }
1010
public string Name { get; set; }
11+
12+
public int PersonId { get; set; }
13+
public virtual Person Owner { get; set; }
1114
}
1215
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using JsonApiDotNetCore.Abstractions;
2+
3+
namespace JsonApiDotNetCoreExample.Resources
4+
{
5+
public class PersonResource : IJsonApiResource
6+
{
7+
public string Id { get; set; }
8+
public string Name { get; set; }
9+
}
10+
}

JsonApiDotNetCoreExample/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void ConfigureServices(IServiceCollection services)
4242
config.DefineResourceMapping(new MapperConfiguration(cfg =>
4343
{
4444
cfg.CreateMap<TodoItem, TodoItemResource>();
45+
cfg.CreateMap<Person, PersonResource>();
4546
}));
4647
});
4748
}

0 commit comments

Comments
 (0)