Skip to content
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

Answer issue 1539 #54

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions src/Examples/GettingStarted/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GettingStarted.Models;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;

namespace GettingStarted.Controllers;

public sealed class BooksController : JsonApiController<Book, string?>
{
public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Book, string?> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
31 changes: 31 additions & 0 deletions src/Examples/GettingStarted/Definitions/BooksDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using GettingStarted.Models;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCore.Resources;

namespace GettingStarted.Definitions;

public sealed class BooksDefinition : JsonApiResourceDefinition<Book, string?>
{
private readonly IRequestQueryStringAccessor _queryStringAccessor;

public BooksDefinition(IResourceGraph resourceGraph, IRequestQueryStringAccessor queryStringAccessor)
: base(resourceGraph)
{
_queryStringAccessor = queryStringAccessor;
}

public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
{
if (!_queryStringAccessor.Query.ContainsKey("fields[books]"))
{
return existingSparseFieldSet
.Excluding<Book>(book => book.Refs1, ResourceGraph)
.Excluding<Book>(book => book.Refs2, ResourceGraph)
.Excluding<Book>(book => book.Refs3, ResourceGraph);
}

return existingSparseFieldSet;
}
}
4 changes: 2 additions & 2 deletions src/Examples/GettingStarted/GettingStarted.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<Import Project="..\..\..\package-versions.props" />

<ItemGroup>
<ProjectReference Include="..\..\JsonApiDotNetCore.MongoDb\JsonApiDotNetCore.MongoDb.csproj" />
<PackageReference Include="JsonApiDotNetCore.MongoDb" Version="4.2.0-alpha1" />
</ItemGroup>
</Project>
12 changes: 10 additions & 2 deletions src/Examples/GettingStarted/Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace GettingStarted.Models;

[UsedImplicitly(ImplicitUseTargetFlags.Members)]
[Resource]
public sealed class Book : HexStringMongoIdentifiable
public sealed class Book : MongoIdentifiable
{
[Attr]
public string Title { get; set; } = null!;
Expand All @@ -16,4 +15,13 @@ public sealed class Book : HexStringMongoIdentifiable

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

[Attr]
public List<string>? Refs1 { get; set; }

[Attr]
public List<string>? Refs2 { get; set; }

[Attr]
public List<string>? Refs3 { get; set; }
}
5 changes: 4 additions & 1 deletion src/Examples/GettingStarted/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using GettingStarted.Definitions;
using GettingStarted.Models;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.MongoDb.Configuration;
using JsonApiDotNetCore.MongoDb.Repositories;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MongoDB.Driver;
using Newtonsoft.Json;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

Expand All @@ -19,6 +21,7 @@
builder.Services.AddJsonApiMongoDb();

builder.Services.AddResourceRepository<MongoRepository<Book, string?>>();
builder.Services.AddResourceDefinition<BooksDefinition>();

WebApplication app = builder.Build();

Expand All @@ -38,7 +41,7 @@ static void ConfigureJsonApiOptions(JsonApiOptions options)
options.Namespace = "api";
options.UseRelativeLinks = true;
options.IncludeTotalResourceCount = true;
options.SerializerOptions.WriteIndented = true;
options.SerializerSettings.Formatting = Formatting.Indented;
}

static async Task CreateSampleDataAsync(IMongoDatabase database)
Expand Down
4 changes: 2 additions & 2 deletions src/Examples/GettingStarted/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/books",
"launchUrl": "api/books?fields[books]=title,refs1,refs2",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Kestrel": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/books",
"launchUrl": "api/books?fields[books]=title,refs1,refs2",
"applicationUrl": "http://localhost:24141",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
Loading