Skip to content

Commit dfc3f4a

Browse files
author
Bart Koelman
committed
Updated to JADNC v5 (without tests, for now)
1 parent ead3a58 commit dfc3f4a

34 files changed

+991
-365
lines changed

Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
<PropertyGroup>
33
<TargetFrameworkName>net6.0</TargetFrameworkName>
44
<AspNetVersion>6.0.*</AspNetVersion>
5-
<JsonApiDotNetCoreVersion>4.2.0</JsonApiDotNetCoreVersion>
5+
<JsonApiDotNetCoreVersion>5.0.0-pre1</JsonApiDotNetCoreVersion>
66
<MongoDBDriverVersion>2.14.*</MongoDBDriverVersion>
77
<JsonApiDotNetCoreMongoDbVersionPrefix>5.0.0</JsonApiDotNetCoreMongoDbVersionPrefix>
88
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
99
<WarningLevel>9999</WarningLevel>
10+
<Nullable>enable</Nullable>
1011
<ImplicitUsings>enable</ImplicitUsings>
1112
<IsPackable>false</IsPackable>
1213
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>

JsonApiDotNetCore.MongoDb.sln

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "src\Examp
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExample", "src\Examples\JsonApiDotNetCoreMongoDbExample\JsonApiDotNetCoreMongoDbExample.csproj", "{11CC33C8-27D7-44D2-B402-76E3A33285A0}"
1515
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExampleTests", "test\JsonApiDotNetCoreMongoDbExampleTests\JsonApiDotNetCoreMongoDbExampleTests.csproj", "{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}"
17-
EndProject
1816
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.MongoDb", "src\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj", "{FD312677-2A62-4B8F-A965-879B059F1755}"
1917
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbTests", "test\JsonApiDotNetCoreMongoDbTests\JsonApiDotNetCoreMongoDbTests.csproj", "{911271DD-29BC-40BC-A9CC-29BE7163B66B}"
19+
EndProject
2020
Global
2121
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2222
Debug|Any CPU = Debug|Any CPU
@@ -51,18 +51,6 @@ Global
5151
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x64.Build.0 = Release|Any CPU
5252
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x86.ActiveCfg = Release|Any CPU
5353
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x86.Build.0 = Release|Any CPU
54-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
56-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x64.ActiveCfg = Debug|Any CPU
57-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x64.Build.0 = Debug|Any CPU
58-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x86.ActiveCfg = Debug|Any CPU
59-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x86.Build.0 = Debug|Any CPU
60-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
61-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|Any CPU.Build.0 = Release|Any CPU
62-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x64.ActiveCfg = Release|Any CPU
63-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x64.Build.0 = Release|Any CPU
64-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x86.ActiveCfg = Release|Any CPU
65-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x86.Build.0 = Release|Any CPU
6654
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6755
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.Build.0 = Debug|Any CPU
6856
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -75,15 +63,27 @@ Global
7563
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x64.Build.0 = Release|Any CPU
7664
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.ActiveCfg = Release|Any CPU
7765
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.Build.0 = Release|Any CPU
66+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|x64.ActiveCfg = Debug|Any CPU
69+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|x64.Build.0 = Debug|Any CPU
70+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|x86.ActiveCfg = Debug|Any CPU
71+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Debug|x86.Build.0 = Debug|Any CPU
72+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|x64.ActiveCfg = Release|Any CPU
75+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|x64.Build.0 = Release|Any CPU
76+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|x86.ActiveCfg = Release|Any CPU
77+
{911271DD-29BC-40BC-A9CC-29BE7163B66B}.Release|x86.Build.0 = Release|Any CPU
7878
EndGlobalSection
7979
GlobalSection(SolutionProperties) = preSolution
8080
HideSolutionNode = FALSE
8181
EndGlobalSection
8282
GlobalSection(NestedProjects) = preSolution
8383
{AA148569-62FF-4E1A-8E16-0E529FA38040} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
8484
{11CC33C8-27D7-44D2-B402-76E3A33285A0} = {AA148569-62FF-4E1A-8E16-0E529FA38040}
85-
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1} = {19A533AA-E006-496D-A476-364DF2B637A1}
8685
{FD312677-2A62-4B8F-A965-879B059F1755} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
86+
{911271DD-29BC-40BC-A9CC-29BE7163B66B} = {19A533AA-E006-496D-A476-364DF2B637A1}
8787
EndGlobalSection
8888
GlobalSection(ExtensibilityGlobals) = postSolution
8989
SolutionGuid = {83EBEC34-0CE5-4D16-93CF-EF5B5CCBC538}

src/Examples/GettingStarted/Controllers/BooksController.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Examples/GettingStarted/Models/Book.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
namespace GettingStarted.Models;
66

77
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
8+
[Resource]
89
public sealed class Book : MongoIdentifiable
910
{
1011
[Attr]
11-
public string Title { get; set; }
12+
public string Title { get; set; } = null!;
1213

1314
[Attr]
14-
public int PublishYear { get; set; }
15+
public string Author { get; set; } = null!;
1516

1617
[Attr]
17-
public string Author { get; set; }
18+
public int PublishYear { get; set; }
1819
}
Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,72 @@
1-
namespace GettingStarted;
1+
using GettingStarted.Models;
2+
using JsonApiDotNetCore.Configuration;
3+
using JsonApiDotNetCore.MongoDb.Configuration;
4+
using JsonApiDotNetCore.MongoDb.Repositories;
5+
using MongoDB.Driver;
26

3-
internal static class Program
7+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
11+
builder.Services.AddSingleton(_ =>
412
{
5-
public static void Main(string[] args)
6-
{
7-
CreateHostBuilder(args).Build().Run();
8-
}
13+
var client = new MongoClient(builder.Configuration.GetSection("DatabaseSettings:ConnectionString").Value);
14+
return client.GetDatabase(builder.Configuration.GetSection("DatabaseSettings:Database").Value);
15+
});
16+
17+
builder.Services.AddJsonApi(ConfigureJsonApiOptions, resources: resourceGraphBuilder =>
18+
{
19+
resourceGraphBuilder.Add<Book, string?>();
20+
});
21+
22+
builder.Services.AddJsonApiMongoDb();
23+
24+
builder.Services.AddResourceRepository<MongoRepository<Book, string?>>();
25+
26+
WebApplication app = builder.Build();
27+
28+
// Configure the HTTP request pipeline.
29+
30+
app.UseRouting();
31+
app.UseJsonApi();
32+
app.MapControllers();
933

10-
private static IHostBuilder CreateHostBuilder(string[] args)
34+
var database = app.Services.GetRequiredService<IMongoDatabase>();
35+
await CreateSampleDataAsync(database);
36+
37+
app.Run();
38+
39+
static void ConfigureJsonApiOptions(JsonApiOptions options)
40+
{
41+
options.Namespace = "api";
42+
options.UseRelativeLinks = true;
43+
options.IncludeTotalResourceCount = true;
44+
options.SerializerOptions.WriteIndented = true;
45+
}
46+
47+
static async Task CreateSampleDataAsync(IMongoDatabase database)
48+
{
49+
await database.DropCollectionAsync(nameof(Book));
50+
51+
await database.GetCollection<Book>(nameof(Book)).InsertManyAsync(new[]
1152
{
12-
return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
53+
new Book
54+
{
55+
Title = "Frankenstein",
56+
PublishYear = 1818,
57+
Author = "Mary Shelley"
58+
},
59+
new Book
60+
{
61+
Title = "Robinson Crusoe",
62+
PublishYear = 1719,
63+
Author = "Daniel Defoe"
64+
},
65+
new Book
1366
{
14-
webBuilder.UseStartup<Startup>();
15-
});
16-
}
67+
Title = "Gulliver's Travels",
68+
PublishYear = 1726,
69+
Author = "Jonathan Swift"
70+
}
71+
});
1772
}

src/Examples/GettingStarted/Startup.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/Examples/GettingStarted/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"LogLevel": {
88
"Default": "Warning",
99
"Microsoft.Hosting.Lifetime": "Information",
10-
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
10+
"Microsoft.EntityFrameworkCore": "Critical"
1111
}
1212
},
1313
"AllowedHosts": "*"

src/Examples/JsonApiDotNetCoreMongoDbExample/Controllers/OperationsController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace JsonApiDotNetCoreMongoDbExample.Controllers;
88

99
public sealed class OperationsController : JsonApiOperationsController
1010
{
11-
public OperationsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IOperationsProcessor processor, IJsonApiRequest request,
12-
ITargetedFields targetedFields)
13-
: base(options, loggerFactory, processor, request, targetedFields)
11+
public OperationsController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IOperationsProcessor processor,
12+
IJsonApiRequest request, ITargetedFields targetedFields)
13+
: base(options, resourceGraph, loggerFactory, processor, request, targetedFields)
1414
{
1515
}
1616
}

src/Examples/JsonApiDotNetCoreMongoDbExample/Controllers/TodoItemsController.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.ComponentModel;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCore.Configuration;
4+
using JsonApiDotNetCore.Middleware;
5+
using JsonApiDotNetCore.Queries.Expressions;
6+
using JsonApiDotNetCore.Resources;
7+
using JsonApiDotNetCoreMongoDbExample.Models;
8+
using Microsoft.AspNetCore.Authentication;
9+
10+
namespace JsonApiDotNetCoreMongoDbExample.Definitions;
11+
12+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
13+
public sealed class TodoItemDefinition : JsonApiResourceDefinition<TodoItem, string?>
14+
{
15+
private readonly ISystemClock _systemClock;
16+
17+
public TodoItemDefinition(IResourceGraph resourceGraph, ISystemClock systemClock)
18+
: base(resourceGraph)
19+
{
20+
_systemClock = systemClock;
21+
}
22+
23+
public override SortExpression OnApplySort(SortExpression? existingSort)
24+
{
25+
return existingSort ?? GetDefaultSortOrder();
26+
}
27+
28+
private SortExpression GetDefaultSortOrder()
29+
{
30+
return CreateSortExpressionFromLambda(new PropertySortOrder
31+
{
32+
(todoItem => todoItem.Priority, ListSortDirection.Descending),
33+
(todoItem => todoItem.LastModifiedAt, ListSortDirection.Descending)
34+
});
35+
}
36+
37+
public override Task OnWritingAsync(TodoItem resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
38+
{
39+
if (writeOperation == WriteOperationKind.CreateResource)
40+
{
41+
resource.CreatedAt = _systemClock.UtcNow;
42+
}
43+
else if (writeOperation == WriteOperationKind.UpdateResource)
44+
{
45+
resource.LastModifiedAt = _systemClock.UtcNow;
46+
}
47+
48+
return Task.CompletedTask;
49+
}
50+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
using System.ComponentModel.DataAnnotations;
12
using JetBrains.Annotations;
23
using JsonApiDotNetCore.MongoDb.Resources;
34
using JsonApiDotNetCore.Resources.Annotations;
45

56
namespace JsonApiDotNetCoreMongoDbExample.Models;
67

78
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
9+
[Resource]
810
public sealed class TodoItem : MongoIdentifiable
911
{
1012
[Attr]
11-
public string Description { get; set; }
13+
public string Description { get; set; } = null!;
1214

1315
[Attr]
16+
[Required]
17+
public TodoItemPriority? Priority { get; set; }
18+
19+
[Attr(Capabilities = AttrCapabilities.AllowFilter | AttrCapabilities.AllowSort | AttrCapabilities.AllowView)]
1420
public DateTimeOffset CreatedAt { get; set; }
1521

16-
[Attr]
17-
public DateTimeOffset? CompletedAt { get; set; }
22+
[Attr(PublicName = "modifiedAt", Capabilities = AttrCapabilities.AllowFilter | AttrCapabilities.AllowSort | AttrCapabilities.AllowView)]
23+
public DateTimeOffset? LastModifiedAt { get; set; }
1824
}

0 commit comments

Comments
 (0)