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

Fluent config csv support #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public IActionResult Get()
[Produces("text/csv")]
public IActionResult GetDataAsCsv()
{
return Ok( DummyDataList());
return Ok(DummyDataList());
}

[HttpGet]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Mvc;
using WebApiContrib.Core.Formatter.Csv;
using WebApiContrib.Core.Samples.Model;

namespace WebApiContrib.Core.Samples.Controllers
{
/// <summary>
///
/// Configuration is used for both CSV Output AND Input formatters
///
/// 1. Only primitive value type properties are allowed for UseProperty (no reference types and no method calls are allowed)
/// 2. Chain parameterless ForHeader() method when:
/// a) Not using headers (UseProperty is always chained after UseHeader)
/// b) Using headers but you want them generated automatically based on property name (or path)
/// 3. Chain method UseCsvDelimiter(string) when you want to override default delimiter (semilocolon)
/// 4. Chain method UseEncoding when you want to override default encoding (ISO-8859-1)
/// 5. Chain method UseFormatProvider when you want to provide custom formatting for your primitive types
///
/// </summary>
public class AuthorModelConfiguration : IFormattingConfiguration<AuthorModel>
{
public void Configure(IFormattingConfigurationBuilder<AuthorModel> builder)
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.DateSeparator = "-";
builder
.UseHeaders()
.UseFormatProvider(culture)
.ForHeader("Identifier")
.UseProperty(x => x.Id)
.ForHeader("First Name")
.UseProperty(x => x.FirstName)
.ForHeader("Last Name")
.UseProperty(x => x.LastName)
.ForHeader("Date of Birth")
.UseProperty(x => x.DateOfBirth)
.ForHeader("IQ")
.UseProperty(x => x.IQ)
.ForHeader("Street")
.UseProperty(x => x.Address.Street)
.ForHeader("City")
.UseProperty(x => x.Address.City)
// Header name will be inferred from property path 'Address.City'
.ForHeader()
.UseProperty(x => x.Address.Country)
// Header name will be inferred from property name 'Signature'
.ForHeader()
.UseProperty(x => x.Signature);
}
}

[Route("api/[controller]")]
public class FluentCsvTestController : Controller
{
// GET api/fluentcsvtest
[HttpGet]
public IActionResult Get()
{
return Ok(DummyDataList());
}

[HttpGet]
[Route("data.csv")]
[Produces("text/csv")]
public IActionResult GetDataAsCsv()
{
return Ok(DummyDataList());
}

[HttpGet]
[Route("dataarray.csv")]
[Produces("text/csv")]
public IActionResult GetArrayDataAsCsv()
{
return Ok(DummyDataArray());
}

private static IEnumerable<AuthorModel> DummyDataList()
{
return new List<AuthorModel>
{
new AuthorModel
{
Id = 1,
FirstName = "Joanne",
LastName = "Rowling",
DateOfBirth = DateTime.Now,
IQ = 70,
Signature = "signature",
Address = new AuthorAddress
{
Street = null,
City = "London",
Country = "UK"
}
},
new AuthorModel
{
Id = 1,
FirstName = "Hermann",
LastName = "Hesse",
DateOfBirth = DateTime.Now,
IQ = 180,
Signature = "signature"
}
};
}

private static AuthorModel[] DummyDataArray()
{
return new AuthorModel[]
{
new AuthorModel
{
Id = 1,
FirstName = "Joanne",
LastName = "Rowling",
DateOfBirth = DateTime.Now,
IQ = 70,
Signature = "signature",
Address = new AuthorAddress
{
Street = null,
City = "London",
Country = "UK"
}
},
new AuthorModel
{
Id = 1,
FirstName = "Hermann",
LastName = "Hesse",
DateOfBirth = DateTime.Now,
IQ = 180,
Signature = "signature",
Address = new AuthorAddress
{
Street = null,
City = "Berlin",
Country = "Germany"
}
}
};
}

// POST api/fluentcsvtest/import
[HttpPost]
[Route("import")]
public IActionResult Import([FromBody]List<AuthorModel> value)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
List<AuthorModel> data = value;
return Ok();
}
}

// POST api/fluentcsvtest/import
[HttpPost]
[Route("importarray")]
public IActionResult ImportArray([FromBody]AuthorModel[] value)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
var data = value;
return Ok();
}
}
}
}
23 changes: 23 additions & 0 deletions samples/WebApiContrib.Core.Samples/Model/AuthorModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace WebApiContrib.Core.Samples.Model
{
public class AuthorModel
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public int IQ { get; set; }
public object Signature { get; set; }

public AuthorAddress Address { get; set; }
}

public class AuthorAddress
{
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
}
9 changes: 1 addition & 8 deletions samples/WebApiContrib.Core.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApiContrib.Core.Samples
{
Expand Down
11 changes: 9 additions & 2 deletions samples/WebApiContrib.Core.Samples/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using WebApiContrib.Core.Versioning;
using WebApiContrib.Core.Samples.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace WebApiContrib.Core.Samples
{
Expand All @@ -37,7 +36,15 @@ public void ConfigureServices(IServiceCollection services)
{
o.AddJsonpOutputFormatter();
o.UseFromBodyBinding(controllerPredicate: c => c.ControllerType.AsType() == typeof(BindingController));
}).AddCsvSerializerFormatters()
})
// Register fluent csv formatters
.AddCsvSerializerFormatters(
builder =>
{
builder.RegisterConfiguration(new AuthorModelConfiguration());
})
// Register standard csv formatters
.AddCsvSerializerFormatters()
.AddPlainTextFormatters()
.AddVersionNegotiation(opt =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>WebApiContrib.Core.Samples</AssemblyName>
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>WebApiContrib.Core.Samples</AssemblyName>
</PropertyGroup>

<ItemGroup>
<Content Update="wwwroot\**\*;Views\**\*;appsettings.json;web.config">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<ItemGroup>
<Content Update="wwwroot\**\*;Views\**\*;appsettings.json;web.config">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.Csv\WebApiContrib.Core.Formatter.Csv.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.PlainText\WebApiContrib.Core.Formatter.PlainText.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.Jsonp\WebApiContrib.Core.Formatter.Jsonp.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.TagHelpers.Markdown\WebApiContrib.Core.TagHelpers.Markdown.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core\WebApiContrib.Core.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Versioning\WebApiContrib.Core.Versioning.csproj" />
<ItemGroup>
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.Csv\WebApiContrib.Core.Formatter.Csv.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.PlainText\WebApiContrib.Core.Formatter.PlainText.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Formatter.Jsonp\WebApiContrib.Core.Formatter.Jsonp.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.TagHelpers.Markdown\WebApiContrib.Core.TagHelpers.Markdown.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core\WebApiContrib.Core.csproj" />
<ProjectReference Include="..\..\src\WebApiContrib.Core.Versioning\WebApiContrib.Core.Versioning.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
</ItemGroup>

</Project>
Loading