Skip to content

Commit 20d98d3

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update OData examples
1 parent d3323d4 commit 20d98d3

File tree

73 files changed

+930
-277
lines changed

Some content is hidden

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

73 files changed

+930
-277
lines changed

ApiVersioning.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Common.OData.ApiExplorer",
141141
EndProject
142142
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests\Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests.csproj", "{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}"
143143
EndProject
144+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionsODataSample", "samples\aspnetcore\ConventionsODataSample\ConventionsODataSample.csproj", "{992B6D9F-F007-441A-9ED9-6A0669993A70}"
145+
EndProject
146+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdvancedODataSample", "samples\aspnetcore\AdvancedODataSample\AdvancedODataSample.csproj", "{DDC53D03-C461-4477-84E2-4C31DD3C6B13}"
147+
EndProject
144148
Global
145149
GlobalSection(SharedMSBuildProjectFiles) = preSolution
146150
src\Common.OData.ApiExplorer\Common.OData.ApiExplorer.projitems*{0d6519ae-20d2-4c98-97aa-ed3622043936}*SharedItemsImports = 5
@@ -306,6 +310,14 @@ Global
306310
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
307311
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
308312
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Release|Any CPU.Build.0 = Release|Any CPU
313+
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
314+
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Debug|Any CPU.Build.0 = Debug|Any CPU
315+
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Release|Any CPU.ActiveCfg = Release|Any CPU
316+
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Release|Any CPU.Build.0 = Release|Any CPU
317+
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
318+
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Debug|Any CPU.Build.0 = Debug|Any CPU
319+
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Release|Any CPU.ActiveCfg = Release|Any CPU
320+
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Release|Any CPU.Build.0 = Release|Any CPU
309321
EndGlobalSection
310322
GlobalSection(SolutionProperties) = preSolution
311323
HideSolutionNode = FALSE
@@ -353,6 +365,8 @@ Global
353365
{0D6519AE-20D2-4C98-97AA-ED3622043936} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}
354366
{C0C766F3-A2D6-461E-ADFF-27496600EA9C} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}
355367
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
368+
{992B6D9F-F007-441A-9ED9-6A0669993A70} = {900DD210-8500-4D89-A05D-C9526935A719}
369+
{DDC53D03-C461-4477-84E2-4C31DD3C6B13} = {900DD210-8500-4D89-A05D-C9526935A719}
356370
EndGlobalSection
357371
GlobalSection(ExtensibilityGlobals) = postSolution
358372
SolutionGuid = {5A38B7FA-17BC-4D3C-977F-7379653DC67C}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<RootNamespace>Microsoft.Examples</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.OData.Versioning\Microsoft.AspNetCore.OData.Versioning.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Microsoft.Examples.Configuration
2+
{
3+
using Microsoft.AspNet.OData.Builder;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Examples.Models;
6+
7+
public class OrderModelConfiguration : IModelConfiguration
8+
{
9+
private static readonly ApiVersion V2 = new ApiVersion( 2, 0 );
10+
11+
private EntityTypeConfiguration<Order> ConfigureCurrent( ODataModelBuilder builder )
12+
{
13+
var order = builder.EntitySet<Order>( "Orders" ).EntityType;
14+
15+
order.HasKey( p => p.Id );
16+
17+
return order;
18+
}
19+
20+
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix )
21+
{
22+
// note: the EDM for orders is only available in version 2.0
23+
if ( apiVersion == V2 )
24+
{
25+
ConfigureCurrent( builder );
26+
}
27+
}
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace Microsoft.Examples.Configuration
2+
{
3+
using Microsoft.AspNet.OData.Builder;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Examples.Models;
6+
7+
public class PersonModelConfiguration : IModelConfiguration
8+
{
9+
private void ConfigureV1( ODataModelBuilder builder )
10+
{
11+
var person = ConfigureCurrent( builder );
12+
person.Ignore( p => p.Email );
13+
person.Ignore( p => p.Phone );
14+
}
15+
16+
private void ConfigureV2( ODataModelBuilder builder ) => ConfigureCurrent( builder ).Ignore( p => p.Phone );
17+
18+
private EntityTypeConfiguration<Person> ConfigureCurrent( ODataModelBuilder builder )
19+
{
20+
var person = builder.EntitySet<Person>( "People" ).EntityType;
21+
22+
person.HasKey( p => p.Id );
23+
24+
return person;
25+
}
26+
27+
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix )
28+
{
29+
switch ( apiVersion.MajorVersion )
30+
{
31+
case 1:
32+
ConfigureV1( builder );
33+
break;
34+
case 2:
35+
ConfigureV2( builder );
36+
break;
37+
default:
38+
ConfigureCurrent( builder );
39+
break;
40+
}
41+
}
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNet.OData;
4+
using Microsoft.AspNet.OData.Query;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Examples.Models;
7+
8+
[ApiVersion( "2.0" )]
9+
[ControllerName( "Orders" )]
10+
public class Orders2Controller : ODataController
11+
{
12+
// GET ~/api/orders?api-version=2.0
13+
[HttpGet]
14+
public IActionResult Get( ODataQueryOptions<Order> options, ApiVersion version ) =>
15+
Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );
16+
17+
// GET ~/api/orders/{id}?api-version=2.0
18+
[HttpGet( "{id}" )]
19+
public IActionResult Get( int id, ODataQueryOptions<Order> options, ApiVersion version ) =>
20+
Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Examples.Models;
5+
6+
[ApiController]
7+
[ApiVersion( "3.0" )]
8+
[ControllerName( "Orders" )]
9+
[Route( "api/orders" )]
10+
public class Orders3Controller : ControllerBase
11+
{
12+
// GET ~/api/orders?api-version=3.0
13+
[HttpGet]
14+
public IActionResult Get( ApiVersion version ) => Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );
15+
16+
// GET ~/api/orders/{id}?api-version=3.0
17+
[HttpGet( "{id}" )]
18+
public IActionResult Get( int id, ApiVersion version ) => Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Examples.Models;
5+
6+
// note: since the application is configured with AssumeDefaultVersionWhenUnspecified, this controller
7+
// is implicitly versioned to the DefaultApiVersion, which has the default value 1.0.
8+
[ApiController]
9+
[Route( "api/orders" )]
10+
public class OrdersController : ControllerBase
11+
{
12+
// GET ~/api/orders
13+
// GET ~/api/orders?api-version=1.0
14+
[HttpGet]
15+
public IActionResult Get( ApiVersion version ) => Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );
16+
17+
// GET ~/api/orders/{id}
18+
// GET ~/api/orders/{id}?api-version=1.0
19+
[HttpGet( "{id}" )]
20+
public IActionResult Get( int id, ApiVersion version ) => Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNet.OData;
4+
using Microsoft.AspNet.OData.Query;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Examples.Models;
7+
8+
[ApiVersion( "3.0" )]
9+
[ControllerName( "People" )]
10+
public class People2Controller : ODataController
11+
{
12+
// GET ~/api/people?api-version=3.0
13+
[HttpGet]
14+
public IActionResult Get( ODataQueryOptions<Person> options, ApiVersion version ) =>
15+
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "bill.mei@somewhere.com", Phone = "555-555-5555" } } );
16+
17+
// GET ~/api/people/{id}?api-version=3.0
18+
[HttpGet( "{id}" )]
19+
public IActionResult Get( int id, ODataQueryOptions<Person> options, ApiVersion version ) =>
20+
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "bill.mei@somewhere.com", Phone = "555-555-5555" } );
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNet.OData;
4+
using Microsoft.AspNet.OData.Query;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Examples.Models;
7+
8+
// note: since the application is configured with AssumeDefaultVersionWhenUnspecified, this controller
9+
// is resolved without or without an API version, even though it is explicitly versioned
10+
[ApiVersion( "1.0" )]
11+
[ApiVersion( "2.0" )]
12+
public class PeopleController : ODataController
13+
{
14+
// GET ~/api/people
15+
// GET ~/api/people?api-version=[1.0|2.0]
16+
[HttpGet]
17+
public IActionResult Get( ODataQueryOptions<Person> options, ApiVersion version ) =>
18+
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "bill.mei@somewhere.com", Phone = "555-555-5555" } } );
19+
20+
// GET ~/api/people/{id}
21+
// GET ~/api/people/{id}?api-version=[1.0|2.0]
22+
[HttpGet( "{id}" )]
23+
public IActionResult Get( int id, ODataQueryOptions<Person> options, ApiVersion version ) =>
24+
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "bill.mei@somewhere.com", Phone = "555-555-5555" } );
25+
26+
// PATCH ~/api/people/{id}?api-version=2.0
27+
[HttpPatch( "{id}" )]
28+
[MapToApiVersion( "2.0" )]
29+
public IActionResult Patch( int id, Delta<Person> delta, ODataQueryOptions<Person> options, ApiVersion version )
30+
{
31+
if ( !ModelState.IsValid )
32+
return BadRequest( ModelState );
33+
34+
var person = new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "bill.mei@somewhere.com", Phone = "555-555-5555" };
35+
36+
delta.Patch( person );
37+
38+
return Updated( person );
39+
}
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.Models
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.Linq;
7+
using System.Web;
8+
9+
public class Order
10+
{
11+
public int Id { get; set; }
12+
13+
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now;
14+
15+
public DateTimeOffset EffectiveDate { get; set; } = DateTimeOffset.Now;
16+
17+
[Required]
18+
public string Customer { get; set; }
19+
}
20+
}

0 commit comments

Comments
 (0)