Skip to content

Commit ec45be6

Browse files
authored
Merge pull request #144 from JakeGinnivan/bugfix-input-formatter
Bugfix input formatter
2 parents faef0e6 + f8f35da commit ec45be6

7 files changed

Lines changed: 39 additions & 24 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ artifacts
111111

112112
WebApi.Hal*.nupkg
113113
*.received.txt
114+
115+
.vscode/
116+
117+
*~

WebApi.Hal.Build/WebApi.Hal.Commons.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<RepositoryUrl>$(PackageProjectUrl).git</RepositoryUrl>
2121
</PropertyGroup>
2222
<PropertyGroup>
23-
<Major>1</Major>
23+
<Major>3</Major>
2424
<Minor>0</Minor>
2525
<Revision>0</Revision>
2626

@@ -29,7 +29,7 @@
2929
<BuildNumber Condition=" '$(BuildNumber)' == '' ">0</BuildNumber>
3030

3131
<!-- Remove for release -->
32-
<PrereleaseLabel>-beta</PrereleaseLabel>
32+
<PrereleaseLabel>-pre</PrereleaseLabel>
3333

3434
<BuildSuffix Condition=" '$(PrereleaseLabel)' != '' ">$(PrereleaseLabel)-$(BuildNumber)</BuildSuffix>
3535
<BuildSuffix Condition=" '$(BuildSuffix)' == '' "></BuildSuffix>

WebApi.Hal.Tests/WebApi.Hal.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
<PropertyGroup>
66
<TargetFramework>netcoreapp2.0</TargetFramework>
7+
<Version>3.0.0-pre-1</Version>
8+
<Authors>Jake Ginnivan, DotNetMedia Organizaion</Authors>
9+
<Copyright>Copyright © Jake Ginnvan 2017</Copyright>
10+
<AssemblyVersion>3.0.0.0</AssemblyVersion>
711
</PropertyGroup>
812

913
<ItemGroup>

WebApi.Hal.Web/Api/BeerController.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.EntityFrameworkCore;
55
using WebApi.Hal.Web.Api.Resources;
66
using WebApi.Hal.Web.Data;
7+
using WebApi.Hal.Web.Models;
78

89
namespace WebApi.Hal.Web.Api
910
{
@@ -23,25 +24,26 @@ public BeerRepresentation Get(int id)
2324
{
2425
var beer = beerDbContext.Beers
2526
.Include("Brewery") // lazy loading isn't on for this query; force loading
26-
.Include("BeerStyle")
27+
.Include("Style")
2728
.Single(br => br.Id == id);
2829

2930
return new BeerRepresentation
30-
{
31-
Id = beer.Id,
32-
Name = beer.Name,
33-
BreweryId = beer.Brewery == null ? (int?) null : beer.Brewery.Id,
34-
BreweryName = beer.Brewery == null ? null : beer.Brewery.Name,
35-
StyleId = beer.Style == null ? (int?) null : beer.Style.Id,
36-
StyleName = beer.Style == null ? null : beer.Style.Name,
37-
ReviewIds = beerDbContext.Reviews.Where(r => r.Beer_Id == id).Select(r => r.Id).ToList()
38-
};
31+
{
32+
Id = beer.Id,
33+
Name = beer.Name,
34+
BreweryId = beer.Brewery == null ? (int?)null : beer.Brewery.Id,
35+
BreweryName = beer.Brewery == null ? null : beer.Brewery.Name,
36+
StyleId = beer.Style == null ? (int?)null : beer.Style.Id,
37+
StyleName = beer.Style == null ? null : beer.Style.Name,
38+
ReviewIds = beerDbContext.Reviews.Where(r => r.Beer_Id == id).Select(r => r.Id).ToList()
39+
};
3940
}
4041

41-
[HttpPut]
42-
// PUT beer?id=1&value=foo
43-
public void Put(int id, string value)
42+
[HttpPut("{id}")]
43+
// PUT beer/5 with a hal representation in the body as json. Be sure to set content-type: application/hal+json (and accept: application/hal+json for the response)
44+
public void Put(int id, [FromBody] BeerRepresentation value)
4445
{
46+
Console.WriteLine(string.Format("new beer would be updated if repostory supported it! {0}, {1}", value.Id, value.Name));
4547
}
4648

4749
[HttpDelete]

WebApi.Hal.Web/Api/BeersController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public BeerListRepresentation Search(string searchTerm, int page = 1)
5656

5757
[HttpPost]
5858
// POST beers
59-
public IActionResult Post(BeerRepresentation value)
59+
public IActionResult Post([FromBody] BeerRepresentation value)
6060
{
61-
var newBeer = new Beer(value.Name);
61+
var newBeer = new Beer(value.Name) { Style_Id = value.StyleId.Value, Brewery_Id = value.BreweryId.Value };
6262
repository.Add(newBeer);
6363

6464
return Created(LinkTemplates.Beers.Beer.CreateUri(new {id = newBeer.Id}), newBeer);

WebApi.Hal.Web/FormattersMvcOptionsSetup.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ public void Configure(MvcOptions options)
5656
// JsonInputFormatter would consume "application/json-patch+json" requests
5757
// before JsonPatchInputFormatter gets to see them.
5858

59-
//TODO Including this formatter causes all returns to be empty
60-
//var jsonInputPatchLogger = _loggerFactory.CreateLogger<JsonHalMediaTypeInputFormatter>();
61-
//options.InputFormatters.Add(new JsonHalMediaTypeInputFormatter(
62-
// jsonInputPatchLogger,
63-
// _jsonSerializerSettings,
64-
// _charPool,
65-
// _objectPoolProvider));
59+
var jsonInputPatchLogger = _loggerFactory.CreateLogger<JsonHalMediaTypeInputFormatter>();
60+
options.InputFormatters.Add(new JsonHalMediaTypeInputFormatter(
61+
jsonInputPatchLogger,
62+
new JsonSerializerSettings(),
63+
_charPool,
64+
_objectPoolProvider));
6665
}
6766
}
6867
}

WebApi.Hal/WebApi.Hal.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
<PropertyGroup>
66
<TargetFrameworks>netstandard2.0</TargetFrameworks>
7+
<Version>3.0.0</Version>
8+
<PackageVersion>3.0.0</PackageVersion>
9+
<Copyright>Copyright © Jake Ginnivan 2018</Copyright>
10+
<Description>Adds support for the Hal Media Type (and Hypermedia) to Asp.net</Description>
11+
<ProjectUrl>https://github.com/JakeGinnivan/WebApi.Hal</ProjectUrl>
12+
<IsPackable>true</IsPackable>
713
</PropertyGroup>
814

915
<ItemGroup>

0 commit comments

Comments
 (0)