Skip to content

Commit 08b6231

Browse files
mrnkrBart Koelman
and
Bart Koelman
authored
Add example projects and tests (#4)
Co-authored-by: Bart Koelman <[email protected]>
1 parent 659666f commit 08b6231

File tree

104 files changed

+6989
-95
lines changed

Some content is hidden

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

104 files changed

+6989
-95
lines changed

JsonApiDotNetCore.MongoDb.sln

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26124.0
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30804.86
55
MinimumVisualStudioVersion = 15.0.26124.0
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DDAB9F03-9137-4BA5-9932-96C006C88583}"
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCore.MongoDb", "src\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj", "{E8C38068-3E3E-477D-A09A-D536D662FC1C}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{19A533AA-E006-496D-A476-364DF2B637A1}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{AA148569-62FF-4E1A-8E16-0E529FA38040}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExample", "src\Examples\JsonApiDotNetCoreMongoDbExample\JsonApiDotNetCoreMongoDbExample.csproj", "{11CC33C8-27D7-44D2-B402-76E3A33285A0}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreMongoDbExampleTests", "test\JsonApiDotNetCoreMongoDbExampleTests\JsonApiDotNetCoreMongoDbExampleTests.csproj", "{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1}"
17+
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.MongoDb", "src\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj", "{FD312677-2A62-4B8F-A965-879B059F1755}"
919
EndProject
1020
Global
1121
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -16,24 +26,66 @@ Global
1626
Release|x64 = Release|x64
1727
Release|x86 = Release|x86
1828
EndGlobalSection
29+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
30+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x64.ActiveCfg = Debug|Any CPU
33+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x64.Build.0 = Debug|Any CPU
34+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x86.ActiveCfg = Debug|Any CPU
35+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Debug|x86.Build.0 = Debug|Any CPU
36+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x64.ActiveCfg = Release|Any CPU
39+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x64.Build.0 = Release|Any CPU
40+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x86.ActiveCfg = Release|Any CPU
41+
{1AB13DD0-1E72-40C4-9EED-D4FF83701B4A}.Release|x86.Build.0 = Release|Any CPU
42+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x64.ActiveCfg = Debug|Any CPU
45+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x64.Build.0 = Debug|Any CPU
46+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x86.ActiveCfg = Debug|Any CPU
47+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Debug|x86.Build.0 = Debug|Any CPU
48+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x64.ActiveCfg = Release|Any CPU
51+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x64.Build.0 = Release|Any CPU
52+
{11CC33C8-27D7-44D2-B402-76E3A33285A0}.Release|x86.ActiveCfg = Release|Any CPU
53+
{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
66+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x64.ActiveCfg = Debug|Any CPU
69+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x64.Build.0 = Debug|Any CPU
70+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x86.ActiveCfg = Debug|Any CPU
71+
{FD312677-2A62-4B8F-A965-879B059F1755}.Debug|x86.Build.0 = Debug|Any CPU
72+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x64.ActiveCfg = Release|Any CPU
75+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x64.Build.0 = Release|Any CPU
76+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.ActiveCfg = Release|Any CPU
77+
{FD312677-2A62-4B8F-A965-879B059F1755}.Release|x86.Build.0 = Release|Any CPU
78+
EndGlobalSection
1979
GlobalSection(SolutionProperties) = preSolution
2080
HideSolutionNode = FALSE
2181
EndGlobalSection
22-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
25-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x64.ActiveCfg = Debug|Any CPU
26-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x64.Build.0 = Debug|Any CPU
27-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x86.ActiveCfg = Debug|Any CPU
28-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Debug|x86.Build.0 = Debug|Any CPU
29-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
30-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|Any CPU.Build.0 = Release|Any CPU
31-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x64.ActiveCfg = Release|Any CPU
32-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x64.Build.0 = Release|Any CPU
33-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x86.ActiveCfg = Release|Any CPU
34-
{E8C38068-3E3E-477D-A09A-D536D662FC1C}.Release|x86.Build.0 = Release|Any CPU
35-
EndGlobalSection
3682
GlobalSection(NestedProjects) = preSolution
37-
{E8C38068-3E3E-477D-A09A-D536D662FC1C} = {DDAB9F03-9137-4BA5-9932-96C006C88583}
83+
{AA148569-62FF-4E1A-8E16-0E529FA38040} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
84+
{11CC33C8-27D7-44D2-B402-76E3A33285A0} = {AA148569-62FF-4E1A-8E16-0E529FA38040}
85+
{24CE53FA-9C49-4E20-A060-4A43DFB8C8F1} = {19A533AA-E006-496D-A476-364DF2B637A1}
86+
{FD312677-2A62-4B8F-A965-879B059F1755} = {7E29AA10-F938-4CF8-9CAB-7ACD2D6DC784}
87+
EndGlobalSection
88+
GlobalSection(ExtensibilityGlobals) = postSolution
89+
SolutionGuid = {83EBEC34-0CE5-4D16-93CF-EF5B5CCBC538}
3890
EndGlobalSection
3991
EndGlobal

README.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ dotnet add package JsonApiDotNetCore.MongoDb
1414
### Models
1515

1616
```cs
17-
public sealed class Book : IIdentifiable<string>
17+
public sealed class Book : MongoDbIdentifiable
1818
{
19-
[BsonId]
20-
[BsonRepresentation(BsonType.ObjectId)]
21-
[Attr]
22-
public string Id { get; set; }
23-
2419
[Attr]
2520
public string Name { get; set; }
26-
27-
[BsonIgnore]
28-
public string StringId { get => Id; set => Id = value; }
2921
}
3022
```
3123

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

57-
services.AddResourceRepository<MongoDbRepository<Book>>();
58-
5949
services.AddJsonApi(resources: builder =>
6050
{
6151
builder.Add<Book, string>();
6252
});
53+
services.AddJsonApiMongoDb();
54+
55+
services.AddResourceRepository<MongoDbRepository<Book, string>>();
6356
}
6457

6558
public void Configure(IApplicationBuilder app)
@@ -70,6 +63,55 @@ public class Startup
7063
}
7164
}
7265
```
66+
Note: If your API project uses only MongoDB (not in combination with EF Core), then instead of
67+
registering all MongoDB resources and repositories individually, you can use:
68+
```cs
69+
public class Startup
70+
{
71+
public IServiceProvider ConfigureServices(IServiceCollection services)
72+
{
73+
// ...
74+
75+
services.AddJsonApi(facade => facade.AddCurrentAssembly());
76+
services.AddJsonApiMongoDb();
77+
78+
services.AddScoped(typeof(IResourceReadRepository<>), typeof(MongoDbRepository<>));
79+
services.AddScoped(typeof(IResourceReadRepository<,>), typeof(MongoDbRepository<,>));
80+
services.AddScoped(typeof(IResourceWriteRepository<>), typeof(MongoDbRepository<>));
81+
services.AddScoped(typeof(IResourceWriteRepository<,>), typeof(MongoDbRepository<,>));
82+
services.AddScoped(typeof(IResourceRepository<>), typeof(MongoDbRepository<>));
83+
services.AddScoped(typeof(IResourceRepository<,>), typeof(MongoDbRepository<,>));
84+
}
85+
}
86+
```
87+
88+
## Development
89+
90+
Restore all NuGet packages with:
91+
92+
```bash
93+
dotnet restore
94+
```
95+
96+
### Testing
97+
98+
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:
99+
100+
```bash
101+
dotnet test
102+
```
103+
104+
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:
105+
106+
```bash
107+
docker run -p 27017:27017 -d mongo:latest
108+
```
109+
110+
And then to run the API:
111+
112+
```bash
113+
dotnet run
114+
```
73115

74116
## Limitations
75117

run-docker-mongodb.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Requires -Version 7.0
2+
3+
# This script starts a docker container with MongoDB database, used for running tests.
4+
5+
docker container stop jsonapi-dotnet-core-mongodb-testing
6+
7+
docker run --rm --name jsonapi-dotnet-core-mongodb-testing `
8+
-p 27017:27017 `
9+
mongo:latest
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using GettingStarted.Models;
2+
using JsonApiDotNetCore.Configuration;
3+
using JsonApiDotNetCore.Controllers;
4+
using JsonApiDotNetCore.Services;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace GettingStarted.Controllers
8+
{
9+
public sealed class BooksController : JsonApiController<Book, string>
10+
{
11+
public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Book, string> resourceService)
12+
: base(options, loggerFactory, resourceService)
13+
{
14+
}
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="JsonApiDotNetCore" Version="$(JsonApiDotNetCoreVersion)" />
8+
<PackageReference Include="MongoDB.Driver" Version="$(MongoDBDriverVersion)" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using JsonApiDotNetCore.MongoDb.Resources;
2+
using JsonApiDotNetCore.Resources.Annotations;
3+
4+
namespace GettingStarted.Models
5+
{
6+
public sealed class Book : MongoDbIdentifiable
7+
{
8+
[Attr]
9+
public string Title { get; set; }
10+
11+
[Attr]
12+
public int PublishYear { get; set; }
13+
14+
[Attr]
15+
public string Author { get; set; }
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace GettingStarted
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
private static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:52498",
8+
"sslPort": 44343
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": false,
15+
"launchUrl": "api/books",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"Kestrel": {
21+
"commandName": "Project",
22+
"launchBrowser": false,
23+
"launchUrl": "api/books",
24+
"applicationUrl": "http://localhost:14141",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}
31+

src/Examples/GettingStarted/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Sample project
2+
3+
## Usage
4+
5+
`dotnet run` to run the project
6+
7+
You can verify the project is running by checking this endpoint:
8+
`localhost:14141/api/people`
9+
10+
For further documentation and implementation of a JsonApiDotnetCore Application see the documentation or GitHub page:
11+
12+
Repository: https://github.com/json-api-dotnet/JsonApiDotNetCore
13+
14+
Documentation: http://www.jsonapi.net

0 commit comments

Comments
 (0)