Skip to content

Commit bbd2ccb

Browse files
committed
example(*): add todo-item-collection
1 parent 18f6bac commit bbd2ccb

File tree

8 files changed

+236
-3
lines changed

8 files changed

+236
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using JsonApiDotNetCore.Controllers;
2+
using JsonApiDotNetCore.Data;
3+
using JsonApiDotNetCore.Services;
4+
using JsonApiDotNetCoreExample.Models;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace JsonApiDotNetCoreExample.Controllers
8+
{
9+
public class TodoItemCollectionsController : JsonApiController<TodoItemCollection>
10+
{
11+
public TodoItemCollectionsController(
12+
IJsonApiContext jsonApiContext,
13+
IEntityRepository<TodoItemCollection> entityRepository,
14+
ILoggerFactory loggerFactory)
15+
: base(jsonApiContext, entityRepository, loggerFactory)
16+
{ }
17+
}
18+
}

src/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public AppDbContext(DbContextOptions<AppDbContext> options)
1111

1212
public DbSet<TodoItem> TodoItems { get; set; }
1313
public DbSet<Person> People { get; set; }
14+
public DbSet<TodoItemCollection> TodoItemCollection { get; set; }
1415
}
1516
}

src/JsonApiDotNetCoreExample/Migrations/20170228193414_AddTodoItemCollection.Designer.cs

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

src/JsonApiDotNetCoreExample/Migrations/AppDbContextModelSnapshot.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
3535
b.Property<int>("Id")
3636
.ValueGeneratedOnAdd();
3737

38+
b.Property<int?>("CollectionId");
39+
3840
b.Property<string>("Description");
3941

4042
b.Property<long>("Ordinal");
@@ -43,17 +45,47 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4345

4446
b.HasKey("Id");
4547

48+
b.HasIndex("CollectionId");
49+
4650
b.HasIndex("OwnerId");
4751

4852
b.ToTable("TodoItems");
4953
});
5054

55+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItemCollection", b =>
56+
{
57+
b.Property<int>("Id")
58+
.ValueGeneratedOnAdd();
59+
60+
b.Property<string>("Name");
61+
62+
b.Property<int>("OwnerId");
63+
64+
b.HasKey("Id");
65+
66+
b.HasIndex("OwnerId");
67+
68+
b.ToTable("TodoItemCollection");
69+
});
70+
5171
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
5272
{
73+
b.HasOne("JsonApiDotNetCoreExample.Models.TodoItemCollection", "Collection")
74+
.WithMany("TodoItems")
75+
.HasForeignKey("CollectionId");
76+
5377
b.HasOne("JsonApiDotNetCoreExample.Models.Person", "Owner")
5478
.WithMany("TodoItems")
5579
.HasForeignKey("OwnerId");
5680
});
81+
82+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItemCollection", b =>
83+
{
84+
b.HasOne("JsonApiDotNetCoreExample.Models.Person", "Owner")
85+
.WithMany("TodoItemCollections")
86+
.HasForeignKey("OwnerId")
87+
.OnDelete(DeleteBehavior.Cascade);
88+
});
5789
}
5890
}
5991
}

src/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace JsonApiDotNetCoreExample.Models
77
{
8-
public class Person : Identifiable<int>, IHasMeta
8+
public class Person : Identifiable, IHasMeta
99
{
1010
[Attr("first-name")]
1111
public string FirstName { get; set; }
@@ -14,6 +14,7 @@ public class Person : Identifiable<int>, IHasMeta
1414
public string LastName { get; set; }
1515

1616
public virtual List<TodoItem> TodoItems { get; set; }
17+
public virtual List<TodoItemCollection> TodoItemCollections { get; set; }
1718

1819
public Dictionary<string, object> GetMeta(IJsonApiContext context)
1920
{

src/JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
namespace JsonApiDotNetCoreExample.Models
55
{
6-
public class TodoItem : Identifiable<int>
6+
public class TodoItem : Identifiable
77
{
8-
98
[Attr("description")]
109
public string Description { get; set; }
1110

@@ -14,5 +13,8 @@ public class TodoItem : Identifiable<int>
1413

1514
public int? OwnerId { get; set; }
1615
public virtual Person Owner { get; set; }
16+
17+
public int? CollectionId { get; set; }
18+
public virtual TodoItemCollection Collection { get; set; }
1719
}
1820
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Models;
3+
4+
namespace JsonApiDotNetCoreExample.Models
5+
{
6+
public class TodoItemCollection : Identifiable
7+
{
8+
public string Name { get; set; }
9+
public virtual List<TodoItem> TodoItems { get; set; }
10+
public int OwnerId { get; set; }
11+
public virtual Person Owner { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)