Skip to content

Commit

Permalink
Merge pull request #17 from Research-Institute/v0.1.4-fixes
Browse files Browse the repository at this point in the history
Dasherize name in RouteBuilder
  • Loading branch information
jaredcnance authored Sep 8, 2016
2 parents 1db131c + 6441a78 commit 8e0016c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using JsonApiDotNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper;
using JsonApiDotNetCore.Abstractions;
using JsonApiDotNetCore.Attributes;

namespace JsonApiDotNetCore.Configuration
Expand Down
7 changes: 4 additions & 3 deletions JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public async Task Invoke(HttpContext context)
}
else
{
_logger.LogWarning("Request not handled by JsonApiDotNetCore");

await _next.Invoke(context);

RespondUnsupportedMediaType(context);
Expand All @@ -51,14 +53,13 @@ private bool IsJsonApiRequest(HttpContext context)
if(context.Request.ContentType == "application/vnd.api+json") {
return true;
}
_logger.LogInformation("Content-Type invalid for JsonAPI");
_logger.LogWarning("Content-Type invalid for JsonAPI, must be application/vnd.api+json");
return false;
}
return true;
}

_logger.LogInformation("Accept header invalid for JsonAPI");

_logger.LogWarning("Accept header invalid for JsonAPI, must be application/vnd.api+json");
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/Routing/RouteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Type GetTypeOfRelatedResource(string relationshipName)
// TODO: Why is this here?
public static string BuildRoute(string nameSpace, string resourceCollectionName)
{
return $"/{nameSpace}/{resourceCollectionName}";
return $"/{nameSpace}/{resourceCollectionName.Dasherize()}";
}
}
}
23 changes: 23 additions & 0 deletions JsonApiDotNetCoreTests/Routing/UnitTests/RouteBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Xunit;
using JsonApiDotNetCore.Routing;

namespace JsonApiDotNetCoreTests.Routing.UnitTests
{
public class RoutBuilderTests
{
[Theory]
[InlineData("api/v1","People","/api/v1/people")]
[InlineData("api/v1","TodoItems","/api/v1/todo-items")]
[InlineData("api","todoItems","/api/todo-items")]
[InlineData("api","MoreModelsHere","/api/more-models-here")]
public void BuildRoute_Returns_CorrectRoute(string nameSpace, string collectionName, string expectOutput)
{
// arrange
// act
var result = RouteBuilder.BuildRoute(nameSpace, collectionName);

// assert
Assert.Equal(expectOutput, result);
}
}
}
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class PersonResource : IJsonApiResource
```

We use [AutoMapper](http://automapper.org/) to map from the model class to the resource class.
The below snippet shows how you can specify a custom mapping expression in your `Startup` class that will append `_1` to the resource name.
The below snippet is a trivial example of how you can specify a custom mapping expression in your `Startup` class.
In this example, the first and last name are concatenated and used for the value of the resource's DisplayName property.
Check out [AutoMapper's Wiki](https://github.com/AutoMapper/AutoMapper/wiki) for detailed mapping options.

```
Expand All @@ -76,7 +77,7 @@ services.AddJsonApi(config => {
config.AddResourceMapping<Person, PersonResource>(map =>
{
// resource.Name = model.Name + "_1"
map.ForMember("Name", opt => opt.MapFrom(src => $"{((Person)src).Name}_1"));
map.ForMember("DisplayName", opt => opt.MapFrom(src => $"{((Person)src).FirstName} {((Person)src).LastName}"));
});
...
});
Expand All @@ -94,9 +95,11 @@ services.AddJsonApi(config => {
});
```

The controller **MUST** implement `IJsonApiController`, and it **MAY** inherit from [JsonApiController](https://github.com/Research-Institute/json-api-dotnet-core/blob/master/JsonApiDotNetCore/Controllers/JsonApiController.cs).
- The controller **MUST** implement `IJsonApiController`
- Controllers **MAY** inherit from [JsonApiController](https://github.com/Research-Institute/json-api-dotnet-core/blob/master/JsonApiDotNetCore/Controllers/JsonApiController.cs).

Constructor dependency injection will work like normal.
Any services added in your `Startup.ConfigureServices()` method will be injected into the constructor parameters.
Any services in your `Startup.ConfigureServices()` method will be injected into the constructor parameters.

```
public class TodoItemsController : JsonApiController, IJsonApiController
Expand Down

0 comments on commit 8e0016c

Please sign in to comment.