Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions TodoListAPI/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using OpenIddict.Abstractions;
using TodoListAPI.Models;

Expand All @@ -17,6 +18,8 @@ protected override void OnModelCreating(ModelBuilder builder)

builder.Entity<TodoItem>()
.HasQueryFilter(todoItem => !IsInHttpRequest() || todoItem.Owner.Id == GetCurrentUserId());

AdjustDeleteBehaviorForJsonApi(builder);
}

private bool IsInHttpRequest()
Expand All @@ -35,4 +38,21 @@ private string GetCurrentUserId()

throw new InvalidOperationException("Could not find current user.");
}

private static void AdjustDeleteBehaviorForJsonApi(ModelBuilder builder)
{
foreach (IMutableForeignKey foreignKey in builder.Model.GetEntityTypes()
.SelectMany(entityType => entityType.GetForeignKeys()))
{
if (foreignKey.DeleteBehavior == DeleteBehavior.ClientSetNull)
{
foreignKey.DeleteBehavior = DeleteBehavior.SetNull;
}

if (foreignKey.DeleteBehavior == DeleteBehavior.ClientCascade)
{
foreignKey.DeleteBehavior = DeleteBehavior.Cascade;
}
}
}
}
16 changes: 15 additions & 1 deletion TodoListAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using OpenIddict.EntityFrameworkCore.Models;
using TodoListAPI.Data;
using TodoListAPI.Models;

Expand Down Expand Up @@ -43,7 +44,20 @@
options.IncludeExceptionStackTraceInErrors = true;
options.IncludeRequestBodyInErrors = true;
#endif
}, discovery => discovery.AddCurrentAssembly());
}, discovery => discovery.AddCurrentAssembly(), resourceGraphBuilder =>
{
resourceGraphBuilder
.SuppressWarningNotIdentifiable<IdentityRole>()
.SuppressWarningNotIdentifiable<IdentityRoleClaim<string>>()
.SuppressWarningNotIdentifiable<IdentityUserClaim<string>>()
.SuppressWarningNotIdentifiable<IdentityUserLogin<string>>()
.SuppressWarningNotIdentifiable<IdentityUserRole<string>>()
.SuppressWarningNotIdentifiable<IdentityUserToken<string>>()
.SuppressWarningNotIdentifiable<OpenIddictEntityFrameworkCoreApplication>()
.SuppressWarningNotIdentifiable<OpenIddictEntityFrameworkCoreAuthorization>()
.SuppressWarningNotIdentifiable<OpenIddictEntityFrameworkCoreScope>()
.SuppressWarningNotIdentifiable<OpenIddictEntityFrameworkCoreToken>();
});

// Register the Identity services.
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
2 changes: 1 addition & 1 deletion TodoListAPI/TodoListAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonApiDotNetCore" Version="5.11.0" />
<PackageReference Include="JsonApiDotNetCore" Version="5.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.12" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
<PackageReference Include="OpenIddict.AspNetCore" Version="7.7.0" />
Expand Down
3 changes: 1 addition & 2 deletions TodoListAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
"JsonApiDotNetCore.Configuration.ResourceGraphBuilder": "Error"
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
}
},
"AllowedHosts": "*"
Expand Down
Loading