Skip to content

Moved hooks into test project, refreshed example project #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
public sealed class TagsController : JsonApiController<Tag>
{
public TagsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Tag> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
51 changes: 13 additions & 38 deletions src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace JsonApiDotNetCoreExample.Data
public sealed class AppDbContext : DbContext
{
public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
public DbSet<User> Users { get; set; }

public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
Expand All @@ -22,47 +18,26 @@ public AppDbContext(DbContextOptions<AppDbContext> options)

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.Assignee)
.WithMany(person => person.AssignedTodoItems);

builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.Owner)
.WithMany(person => person.TodoItems);

builder.Entity<ArticleTag>()
.HasKey(bc => new
{
bc.ArticleId,
bc.TagId
});

builder.Entity<IdentifiableArticleTag>()
.HasKey(bc => new
builder.Entity<TodoItemTag>()
.HasKey(todoItemTag => new
{
bc.ArticleId,
bc.TagId
todoItemTag.TodoItemId,
todoItemTag.TagId
});

// When deleting a person, un-assign him/her from existing todo items.
builder.Entity<Person>()
.HasOne(person => person.StakeHolderTodoItem)
.WithMany(todoItem => todoItem.StakeHolders)
.OnDelete(DeleteBehavior.Cascade);

builder.Entity<TodoItem>()
.HasMany(todoItem => todoItem.ChildTodoItems)
.WithOne(todoItem => todoItem.ParentTodo);

builder.Entity<Passport>()
.HasOne(passport => passport.Person)
.WithOne(person => person.Passport)
.HasForeignKey<Person>("PassportKey")
.HasMany(person => person.AssignedTodoItems)
.WithOne(todoItem => todoItem.Assignee)
.IsRequired(false)
.OnDelete(DeleteBehavior.SetNull);

// When deleting a person, the todo items he/she owns are deleted too.
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.OneToOnePerson)
.WithOne(person => person.OneToOneTodoItem)
.HasForeignKey<TodoItem>("OneToOnePersonKey");
.HasOne(todoItem => todoItem.Owner)
.WithMany()
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCoreExample.Models;
using Microsoft.AspNetCore.Authentication;

namespace JsonApiDotNetCoreExample.Definitions
{
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class TodoItemDefinition : JsonApiResourceDefinition<TodoItem>
{
private readonly ISystemClock _systemClock;

public TodoItemDefinition(IResourceGraph resourceGraph, ISystemClock systemClock)
: base(resourceGraph)
{
_systemClock = systemClock;
}

public override SortExpression OnApplySort(SortExpression existingSort)
{
return existingSort ?? GetDefaultSortOrder();
}

private SortExpression GetDefaultSortOrder()
{
return CreateSortExpressionFromLambda(new PropertySortOrder
{
(todoItem => todoItem.Priority, ListSortDirection.Descending),
(todoItem => todoItem.LastModifiedAt, ListSortDirection.Descending)
});
}

public override Task OnWritingAsync(TodoItem resource, OperationKind operationKind, CancellationToken cancellationToken)
{
if (operationKind == OperationKind.CreateResource)
{
resource.CreatedAt = _systemClock.UtcNow;
}
else if (operationKind == OperationKind.UpdateResource)
{
resource.LastModifiedAt = _systemClock.UtcNow;
}

return Task.CompletedTask;
}
}
}
16 changes: 1 addition & 15 deletions src/Examples/JsonApiDotNetCoreExample/Models/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@
namespace JsonApiDotNetCoreExample.Models
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class Person : Identifiable, IIsLockable
public sealed class Person : Identifiable
{
public bool IsLocked { get; set; }

[Attr]
public string FirstName { get; set; }

[Attr]
public string LastName { get; set; }

[HasMany]
public ISet<TodoItem> TodoItems { get; set; }

[HasMany]
public ISet<TodoItem> AssignedTodoItems { get; set; }

[HasOne]
public TodoItem OneToOneTodoItem { get; set; }

[HasOne]
public TodoItem StakeHolderTodoItem { get; set; }

[HasOne]
public Passport Passport { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;
Expand All @@ -7,6 +8,8 @@ namespace JsonApiDotNetCoreExample.Models
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class Tag : Identifiable
{
[Required]
[MinLength(1)]
[Attr]
public string Name { get; set; }
}
Expand Down
30 changes: 16 additions & 14 deletions src/Examples/JsonApiDotNetCoreExample/Models/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExample.Models
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class TodoItem : Identifiable, IIsLockable
public sealed class TodoItem : Identifiable
{
public bool IsLocked { get; set; }

[Attr]
public string Description { get; set; }

[Attr]
public TodoItemPriority Priority { get; set; }

[Attr(Capabilities = AttrCapabilities.AllowFilter | AttrCapabilities.AllowSort | AttrCapabilities.AllowView)]
public DateTimeOffset CreatedAt { get; set; }

[Attr(PublicName = "modifiedAt", Capabilities = AttrCapabilities.AllowFilter | AttrCapabilities.AllowSort | AttrCapabilities.AllowView)]
public DateTimeOffset? LastModifiedAt { get; set; }

[HasOne]
public Person Owner { get; set; }

[HasOne]
public Person Assignee { get; set; }

[HasOne]
public Person OneToOnePerson { get; set; }

[HasMany]
public ISet<Person> StakeHolders { get; set; }

// cyclical to-many structure
[HasOne]
public TodoItem ParentTodo { get; set; }
[NotMapped]
[HasManyThrough(nameof(TodoItemTags))]
public ISet<Tag> Tags { get; set; }

[HasMany]
public IList<TodoItem> ChildTodoItems { get; set; }
public ISet<TodoItemTag> TodoItemTags { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/TodoItemPriority.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCoreExample.Models
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public enum TodoItemPriority
{
Low,
Medium,
High
}
}
14 changes: 14 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/TodoItemTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCoreExample.Models
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class TodoItemTag
{
public int TodoItemId { get; set; }
public TodoItem TodoItem { get; set; }

public int TagId { get; set; }
public Tag Tag { get; set; }
}
}
33 changes: 20 additions & 13 deletions src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@ public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISystemClock, SystemClock>();

services.AddDbContext<AppDbContext>(options => options.UseNpgsql(_connectionString));

services.AddJsonApi<AppDbContext>(ConfigureJsonApiOptions, discovery => discovery.AddCurrentAssembly());
}
services.AddDbContext<AppDbContext>(options =>
{
options.UseNpgsql(_connectionString);
#if DEBUG
options.EnableSensitiveDataLogging();
options.EnableDetailedErrors();
#endif
});

private void ConfigureJsonApiOptions(JsonApiOptions options)
{
options.IncludeExceptionStackTraceInErrors = true;
options.Namespace = "api/v1";
options.DefaultPageSize = new PageSize(5);
options.IncludeTotalResourceCount = true;
options.ValidateModelState = true;
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
services.AddJsonApi<AppDbContext>(options =>
{
options.Namespace = "api/v1";
options.UseRelativeLinks = true;
options.ValidateModelState = true;
options.IncludeTotalResourceCount = true;
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
#if DEBUG
options.IncludeExceptionStackTraceInErrors = true;
#endif
}, discovery => discovery.AddCurrentAssembly());
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
12 changes: 6 additions & 6 deletions test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void Can_add_resources_from_assembly_to_graph()
// Assert
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();

ResourceContext personResource = resourceGraph.GetResourceContext(typeof(Person));
personResource.Should().NotBeNull();
ResourceContext personContext = resourceGraph.GetResourceContext(typeof(Person));
personContext.Should().NotBeNull();

ResourceContext articleResource = resourceGraph.GetResourceContext(typeof(Article));
articleResource.Should().NotBeNull();
ResourceContext todoItemContext = resourceGraph.GetResourceContext(typeof(TodoItem));
todoItemContext.Should().NotBeNull();
}

[Fact]
Expand All @@ -79,8 +79,8 @@ public void Can_add_resource_from_current_assembly_to_graph()
// Assert
IResourceGraph resourceGraph = _resourceGraphBuilder.Build();

ResourceContext resource = resourceGraph.GetResourceContext(typeof(TestResource));
resource.Should().NotBeNull();
ResourceContext testContext = resourceGraph.GetResourceContext(typeof(TestResource));
testContext.Should().NotBeNull();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Controllers
{
public sealed class ArticlesController : JsonApiController<Article>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Controllers
{
public sealed class AuthorsController : JsonApiController<Author>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Controllers
{
public sealed class PassportsController : JsonApiController<Passport>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceHooks.Controllers
{
public sealed class PeopleController : JsonApiController<Person>
{
public PeopleController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Person> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
Loading