Skip to content

Commit ec4e76e

Browse files
authored
Merge pull request #478 from wisepotato/feat/#477
Resource hooks
2 parents 5941f19 + 6ccb1bb commit ec4e76e

File tree

103 files changed

+6362
-19642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+6362
-19642
lines changed

JsonApiDotnetCore.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio Version 16
43
VisualStudioVersion = 16.0.28606.126

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ Running tests locally requires access to a postgresql database.
9191
If you have docker installed, this can be propped up via:
9292

9393
```bash
94-
docker run --rm --name jsonapi-dotnet-core-testing \
95-
-e POSTGRES_DB=JsonApiDotNetCoreExample \
96-
-e POSTGRES_USER=postgres \
97-
-e POSTGRES_PASSWORD=postgres \
98-
-p 5432:5432 \
99-
postgres
94+
docker run --rm --name jsonapi-dotnet-core-testing -e POSTGRES_DB=JsonApiDotNetCoreExample -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres
10095
```
10196

10297
And then to run the tests:

docs/usage/extensibility/services.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public class TodoItemService : EntityResourceService<TodoItem>
1717

1818
public TodoItemService(
1919
IJsonApiContext jsonApiContext,
20-
IEntityRepository<T, TId> repository,
20+
IEntityRepository<TodoItem, int> repository,
2121
ILoggerFactory loggerFactory,
2222
INotificationService notificationService)
2323
: base(jsonApiContext, repository, loggerFactory)
2424
{
2525
_notificationService = notificationService;
2626
}
2727

28-
public override async Task<TEntity> CreateAsync(TEntity entity)
28+
public override async Task<TodoItem> CreateAsync(TodoItem entity)
2929
{
3030
// call the base implementation which uses Entity Framework
3131
var newEntity = await base.CreateAsync(entity);

docs/usage/resources/hooks.md

Lines changed: 680 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:56042/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"GettingStarted": {
19+
"commandName": "Project",
20+
"launchBrowser": true
21+
}
22+
}
23+
}

src/Examples/GettingStarted/ResourceDefinitionExample/ModelDefinition.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Internal;
23
using JsonApiDotNetCore.Models;
34

45
namespace GettingStarted.ResourceDefinitionExample
56
{
67
public class ModelDefinition : ResourceDefinition<Model>
78
{
9+
public ModelDefinition(IResourceGraph graph) : base(graph)
10+
{
11+
}
12+
813
// this allows POST / PATCH requests to set the value of a
914
// property, but we don't include this value in the response
1015
// this might be used if the incoming value gets hashed or
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JsonApiDotNetCore.Controllers;
2+
using JsonApiDotNetCore.Services;
3+
using JsonApiDotNetCoreExample.Models;
4+
5+
namespace JsonApiDotNetCoreExample.Controllers
6+
{
7+
public class PassportsController : JsonApiController<Passport>
8+
{
9+
public PassportsController(
10+
IJsonApiContext jsonApiContext,
11+
IResourceService<Passport> resourceService)
12+
: base(jsonApiContext, resourceService)
13+
{ }
14+
}
15+
}

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,60 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4141
modelBuilder.Entity<ArticleTag>()
4242
.HasKey(bc => new { bc.ArticleId, bc.TagId });
4343

44+
modelBuilder.Entity<IdentifiableArticleTag>()
45+
.HasKey(bc => new { bc.ArticleId, bc.TagId });
46+
47+
modelBuilder.Entity<Person>()
48+
.HasOne(t => t.StakeHolderTodo)
49+
.WithMany(t => t.StakeHolders)
50+
.HasForeignKey(t => t.StakeHolderTodoId)
51+
.OnDelete(DeleteBehavior.Cascade);
4452

4553
modelBuilder.Entity<TodoItem>()
4654
.HasOne(t => t.DependentTodoItem);
47-
55+
4856
modelBuilder.Entity<TodoItem>()
4957
.HasMany(t => t.ChildrenTodoItems)
5058
.WithOne(t => t.ParentTodoItem)
5159
.HasForeignKey(t => t.ParentTodoItemId);
5260

61+
modelBuilder.Entity<Person>()
62+
.HasOne(p => p.Passport)
63+
.WithOne(p => p.Person)
64+
.HasForeignKey<Person>(p => p.PassportId);
65+
66+
modelBuilder.Entity<Passport>()
67+
.HasOne(p => p.Person)
68+
.WithOne(p => p.Passport)
69+
.HasForeignKey<Person>(p => p.PassportId);
5370

71+
modelBuilder.Entity<TodoItem>()
72+
.HasOne(p => p.ToOnePerson)
73+
.WithOne(p => p.ToOneTodoItem)
74+
.HasForeignKey<TodoItem>(p => p.ToOnePersonId);
75+
76+
modelBuilder.Entity<Person>()
77+
.HasOne(p => p.ToOneTodoItem)
78+
.WithOne(p => p.ToOnePerson)
79+
.HasForeignKey<TodoItem>(p => p.ToOnePersonId);
5480
}
5581

5682
public DbSet<TodoItem> TodoItems { get; set; }
83+
public DbSet<Passport> Passports { get; set; }
5784
public DbSet<Person> People { get; set; }
5885
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
5986
public DbSet<CamelCasedModel> CamelCasedModels { get; set; }
6087
public DbSet<Article> Articles { get; set; }
6188
public DbSet<Author> Authors { get; set; }
6289
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
6390
public DbSet<User> Users { get; set; }
64-
6591
public DbSet<CourseEntity> Courses { get; set; }
6692
public DbSet<DepartmentEntity> Departments { get; set; }
6793
public DbSet<CourseStudentEntity> Registrations { get; set; }
6894
public DbSet<StudentEntity> Students { get; set; }
6995
public DbSet<PersonRole> PersonRoles { get; set; }
7096
public DbSet<ArticleTag> ArticleTags { get; set; }
97+
public DbSet<IdentifiableArticleTag> IdentifiableArticleTags { get; set; }
7198
public DbSet<Tag> Tags { get; set; }
7299
}
73100
}

src/Examples/JsonApiDotNetCoreExample/JsonApiDotNetCoreExample.csproj

100755100644
File mode changed.

src/Examples/JsonApiDotNetCoreExample/Models/Article.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ public class Article : Identifiable
1717
[HasManyThrough(nameof(ArticleTags))]
1818
public List<Tag> Tags { get; set; }
1919
public List<ArticleTag> ArticleTags { get; set; }
20+
21+
22+
[NotMapped]
23+
[HasManyThrough(nameof(IdentifiableArticleTags))]
24+
public List<Tag> IdentifiableTags { get; set; }
25+
public List<IdentifiableArticleTag> IdentifiableArticleTags { get; set; }
2026
}
2127
}

0 commit comments

Comments
 (0)