Skip to content

Add example projects and tests #4

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 43 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
99e7d50
Add example projects and tests
mrnkr Jan 1, 2021
c53096a
throw proper error when fetching relationships
mrnkr Jan 2, 2021
b42b381
relationship creation tests
mrnkr Jan 2, 2021
c815c29
fix: unsupported relationships message
mrnkr Jan 2, 2021
c3f9dcf
test to-many relationship creation
mrnkr Jan 2, 2021
f348ff9
test unsupported filters
mrnkr Jan 2, 2021
1b49d01
test for readonly attributes
mrnkr Jan 2, 2021
fc94301
suggested changes to validation and IModel implementation
mrnkr Jan 3, 2021
ab7fed5
cleanup last commit
mrnkr Jan 3, 2021
970692b
remove unnecessary MongoDBRefs
mrnkr Jan 3, 2021
3ec805d
added update relationship tests
mrnkr Jan 3, 2021
0a3604e
address review comments
mrnkr Jan 4, 2021
ca2588d
update README.md
mrnkr Jan 4, 2021
1bcef1f
address review comments
mrnkr Jan 6, 2021
e384b4e
bring latest changes in IntegrationTestContext
mrnkr Jan 6, 2021
aa140cb
Fixed handling DateTimes with ambiguous kind.
Jan 6, 2021
310b972
Added test for resource meta
Jan 6, 2021
f3e63b2
Cleanup solution: Move GettingStarted to the root and make it the def…
Jan 6, 2021
09456b0
Simplified code
Jan 6, 2021
465d720
fix README.md
mrnkr Jan 6, 2021
054501b
remove unnecessary test for sparse fieldsets
mrnkr Jan 6, 2021
2446ae4
remove unnecessary tests from CreateResourceTests
mrnkr Jan 6, 2021
a7b8713
address review comments
mrnkr Jan 6, 2021
c97d773
fix: custom query parameter support
mrnkr Jan 7, 2021
78f058c
fix: sort on HasMany test passing
mrnkr Jan 7, 2021
72430bc
added tests for relationship support in SparseFieldSetTests
mrnkr Jan 7, 2021
99192c1
remove unnecessary ReadWrite tests
mrnkr Jan 7, 2021
afb71a5
fix: sort ids descending test
mrnkr Jan 7, 2021
a3742bd
cleanup sparse fieldset tests
mrnkr Jan 7, 2021
5341ba9
move MongoDatabaseExtensions to test project
mrnkr Jan 8, 2021
27f72f7
fix: sparse fieldset test
mrnkr Jan 8, 2021
e4b94bd
cleanup tests
mrnkr Jan 8, 2021
aae4eeb
latest requested changes minus ReadWrite tests
mrnkr Jan 9, 2021
0f3272b
add missing ReadWrite tests
mrnkr Jan 9, 2021
a9e1bc8
Fixed failing tests
Jan 10, 2021
4e5bf56
address review comments
mrnkr Jan 10, 2021
33add8b
Fixed: do not return relationship links in response json
Jan 11, 2021
2a73164
Fixed: the Task returned from Collection.InsertOneAsync was not await…
Jan 11, 2021
11646fc
address review comments
mrnkr Jan 11, 2021
461eb32
Cleanup IoC registrations to properly detect when non-string IDs are …
Jan 12, 2021
4e50bc3
Removed unused types
Jan 12, 2021
d7c7520
Updated example
Jan 12, 2021
3451619
rename public static class ServiceCollectionExtensions.cs to ServiceC…
mrnkr Jan 12, 2021
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
90 changes: 71 additions & 19 deletions JsonApiDotNetCore.MongoDb.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DDAB9F03-9137-4BA5-9932-96C006C88583}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCore.MongoDb", "src\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj", "{E8C38068-3E3E-477D-A09A-D536D662FC1C}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{19A533AA-E006-496D-A476-364DF2B637A1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{AA148569-62FF-4E1A-8E16-0E529FA38040}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExample", "src\Examples\JsonApiDotNetCoreMongoDbExample\JsonApiDotNetCoreMongoDbExample.csproj", "{11CC33C8-27D7-44D2-B402-76E3A33285A0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExampleTests", "test\JsonApiDotNetCoreMongoDbExampleTests\JsonApiDotNetCoreMongoDbExampleTests.csproj", "{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.MongoDb", "src\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj", "{FD312677-2A62-4B8F-A965-879B059F1755}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -16,24 +26,66 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x64.ActiveCfg = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x64.Build.0 = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x86.ActiveCfg = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x86.Build.0 = Debug|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|Any CPU.Build.0 = Release|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x64.ActiveCfg = Release|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x64.Build.0 = Release|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x86.ActiveCfg = Release|Any CPU
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x86.Build.0 = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x64.ActiveCfg = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x64.Build.0 = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x86.ActiveCfg = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x86.Build.0 = Debug|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|Any CPU.Build.0 = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x64.ActiveCfg = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x64.Build.0 = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x86.ActiveCfg = Release|Any CPU
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x86.Build.0 = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x64.ActiveCfg = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x64.Build.0 = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x86.ActiveCfg = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Debug|x86.Build.0 = Debug|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|Any CPU.Build.0 = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x64.ActiveCfg = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x64.Build.0 = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x86.ActiveCfg = Release|Any CPU
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}.Release|x86.Build.0 = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x64.ActiveCfg = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x64.Build.0 = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x86.ActiveCfg = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x86.Build.0 = Debug|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|Any CPU.Build.0 = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x64.ActiveCfg = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x64.Build.0 = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.ActiveCfg = Release|Any CPU
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x64.ActiveCfg = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x64.Build.0 = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x86.ActiveCfg = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x86.Build.0 = Debug|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|Any CPU.Build.0 = Release|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x64.ActiveCfg = Release|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x64.Build.0 = Release|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x86.ActiveCfg = Release|Any CPU
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E8C38068-3E3E-477D-A09A-D536D662FC1C} = {DDAB9F03-9137-4BA5-9932-96C006C88583}
{AA148569-62FF-4E1A-8E16-0E529FA38040} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
{11CC33C8-27D7-44D2-B402-76E3A33285A0} = {AA148569-62FF-4E1A-8E16-0E529FA38040}
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1} = {19A533AA-E006-496D-A476-364DF2B637A1}
{FD312677-2A62-4B8F-A965-879B059F1755} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83EBEC34-0CE5-4D16-93CF-EF5B5CCBC538}
EndGlobalSection
EndGlobal
64 changes: 53 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ dotnet add package JsonApiDotNetCore.MongoDb
### Models

```cs
public sealed class Book : IIdentifiable<string>
public sealed class Book : MongoDbIdentifiable
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[Attr]
public string Id { get; set; }

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

[BsonIgnore]
public string StringId { get => Id; set => Id = value; }
}
```

Expand Down Expand Up @@ -54,12 +46,13 @@ public class Startup
return client.GetDatabase(Configuration.GetSection("DatabaseSettings:Database").Value);
});

services.AddResourceRepository<MongoDbRepository<Book>>();

services.AddJsonApi(resources: builder =>
{
builder.Add<Book, string>();
});
services.AddJsonApiMongoDb();

services.AddResourceRepository<MongoDbRepository<Book, string>>();
}

public void Configure(IApplicationBuilder app)
Expand All @@ -70,6 +63,55 @@ public class Startup
}
}
```
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
registering all MongoDB resources and repositories individually, you can use:
```cs
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// ...

services.AddJsonApi(facade => facade.AddCurrentAssembly());
services.AddJsonApiMongoDb();

services.AddScoped(typeof(IResourceReadRepository<>), typeof(MongoDbRepository<>));
services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoDbRepository<,>));
services.AddScoped(typeof(IResourceWriteRepository<>), typeof(MongoDbRepository<>));
services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoDbRepository<,>));
services.AddScoped(typeof(IResourceRepository<>), typeof(MongoDbRepository<>));
services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoDbRepository<,>));
}
}
```

## Development

Restore all NuGet packages with:

```bash
dotnet restore
```

### Testing

You don't need to have a running instance of MongoDB on your machine. To run the tests just type the following command in your terminal:

```bash
dotnet test
```

If you want to run the examples and explore them on your own **you are** going to need that running instance of MongoDB. If you have docker installed you can launch it like this:

```bash
docker run -p 27017:27017 -d mongo:latest
```

And then to run the API:

```bash
dotnet run
```

## Limitations

Expand Down
9 changes: 9 additions & 0 deletions run-docker-mongodb.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Requires -Version 7.0

# This script starts a docker container with MongoDB database, used for running tests.

docker container stop jsonapi-dotnet-core-mongodb-testing

docker run --rm --name jsonapi-dotnet-core-mongodb-testing `
-p 27017:27017 `
mongo:latest
16 changes: 16 additions & 0 deletions src/Examples/GettingStarted/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GettingStarted.Models;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace GettingStarted.Controllers
{
public sealed class BooksController : JsonApiController<Book, string>
{
public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Book, string> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
14 changes: 14 additions & 0 deletions src/Examples/GettingStarted/GettingStarted.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonApiDotNetCore" Version="$(JsonApiDotNetCoreVersion)" />
<PackageReference Include="MongoDB.Driver" Version="$(MongoDBDriverVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions src/Examples/GettingStarted/Models/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JsonApiDotNetCore.MongoDb.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace GettingStarted.Models
{
public sealed class Book : MongoDbIdentifiable
{
[Attr]
public string Title { get; set; }

[Attr]
public int PublishYear { get; set; }

[Attr]
public string Author { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/Examples/GettingStarted/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace GettingStarted
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
31 changes: 31 additions & 0 deletions src/Examples/GettingStarted/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52498",
"sslPort": 44343
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "api/books",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Kestrel": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "api/books",
"applicationUrl": "http://localhost:14141",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

14 changes: 14 additions & 0 deletions src/Examples/GettingStarted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Sample project

## Usage

`dotnet run` to run the project

You can verify the project is running by checking this endpoint:
`localhost:14141/api/people`

For further documentation and implementation of a JsonApiDotnetCore Application see the documentation or GitHub page:

Repository: https://github.com/json-api-dotnet/JsonApiDotNetCore

Documentation: http://www.jsonapi.net
Loading