Skip to content

Commit 9ab464d

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update ASP.NET Core example with navigation properties
1 parent 510137e commit 9ab464d

File tree

12 files changed

+250
-2
lines changed

12 files changed

+250
-2
lines changed

samples/aspnetcore/SwaggerODataSample/Configuration/OrderModelConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public class OrderModelConfiguration : IModelConfiguration
1717
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
1818
{
1919
var order = builder.EntitySet<Order>( "Orders" ).EntityType.HasKey( o => o.Id );
20+
var lineItem = builder.EntityType<LineItem>().HasKey( li => li.Number );
2021

2122
if ( apiVersion < ApiVersions.V2 )
2223
{
2324
order.Ignore( o => o.EffectiveDate );
25+
lineItem.Ignore( li => li.Fulfilled );
2426
}
2527

2628
if ( apiVersion < ApiVersions.V3 )

samples/aspnetcore/SwaggerODataSample/Configuration/PersonModelConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class PersonModelConfiguration : IModelConfiguration
1818
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
1919
{
2020
var person = builder.EntitySet<Person>( "People" ).EntityType;
21+
var address = builder.EntityType<Address>().HasKey( a => a.Id );
2122

2223
person.HasKey( p => p.Id );
2324
person.Select().OrderBy( "firstName", "lastName" );
@@ -34,6 +35,9 @@ public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
3435

3536
if ( apiVersion > ApiVersions.V1 )
3637
{
38+
person.ContainsOptional( p => p.HomeAddress );
39+
person.Ignore( p => p.WorkAddress );
40+
3741
var function = person.Collection.Function( "NewHires" );
3842

3943
function.Parameter<DateTime>( "Since" );
@@ -42,6 +46,7 @@ public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
4246

4347
if ( apiVersion > ApiVersions.V2 )
4448
{
49+
person.ContainsOptional( p => p.WorkAddress );
4550
person.Action( "Promote" ).Parameter<string>( "title" );
4651
}
4752
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Microsoft.Examples.Models
2+
{
3+
using Microsoft.AspNet.OData.Query;
4+
using System;
5+
6+
/// <summary>
7+
/// Represents an address.
8+
/// </summary>
9+
public class Address
10+
{
11+
/// <summary>
12+
/// Gets or sets the address identifier.
13+
/// </summary>
14+
public int Id { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets the street address.
18+
/// </summary>
19+
/// <value>The street address.</value>
20+
public string Street { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the address city.
24+
/// </summary>
25+
/// <value>The address city.</value>
26+
public string City { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the address state.
30+
/// </summary>
31+
/// <value>The address state.</value>
32+
public string State { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the address zip code.
36+
/// </summary>
37+
/// <value>The address zip code.</value>
38+
public string ZipCode { get; set; }
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Microsoft.Examples.Models
2+
{
3+
using Microsoft.AspNet.OData.Query;
4+
using System;
5+
6+
/// <summary>
7+
/// Represents the line item on an order.
8+
/// </summary>
9+
[Select]
10+
public class LineItem
11+
{
12+
/// <summary>
13+
/// Gets or sets the line item number.
14+
/// </summary>
15+
/// <value>The line item number.</value>
16+
public int Number { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the line item description.
20+
/// </summary>
21+
/// <value>The line item description.</value>
22+
public string Description { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the line item quantity.
26+
/// </summary>
27+
/// <value>The line item quantity.</value>
28+
public int Quantity { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the line item unit price.
32+
/// </summary>
33+
/// <value>The line item unit price.</value>
34+
public decimal UnitPrice { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets a value indicating whether the line item is fulfilled.
38+
/// </summary>
39+
/// <value>True if the line item is fulfilled; otherwise, false.</value>
40+
public bool Fulfilled { get; set; }
41+
}
42+
}

samples/aspnetcore/SwaggerODataSample/Models/Order.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace Microsoft.Examples.Models
22
{
3+
using Microsoft.AspNet.OData.Builder;
34
using Microsoft.AspNet.OData.Query;
45
using System;
6+
using System.Collections.Generic;
57
using System.ComponentModel.DataAnnotations;
68

79
/// <summary>
@@ -41,5 +43,12 @@ public class Order
4143
/// </summary>
4244
/// <value>The description of the order.</value>
4345
public string Description { get; set; }
46+
47+
/// <summary>
48+
/// Gets a list of line items in the order.
49+
/// </summary>
50+
/// <value>The <see cref="IList{T}">list</see> of order <see cref="LineItem">line items</see>.</value>
51+
[Contained]
52+
public virtual IList<LineItem> LineItems { get; } = new List<LineItem>();
4453
}
4554
}

samples/aspnetcore/SwaggerODataSample/Models/Person.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,17 @@ public class Person
4141
/// </summary>
4242
/// <value>The person's telephone number.</value>
4343
public string Phone { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the person's home address.
47+
/// </summary>
48+
/// <value>The person's home address.</value>
49+
public Address HomeAddress { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets the person's work address.
53+
/// </summary>
54+
/// <value>The person's work address.</value>
55+
public Address WorkAddress { get; set; }
4456
}
4557
}

samples/aspnetcore/SwaggerODataSample/V1/OrdersController.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNet.OData.Routing;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Examples.Models;
7+
using System.Collections.Generic;
78
using System.Linq;
89
using static Microsoft.AspNet.OData.Query.AllowedQueryOptions;
910
using static Microsoft.AspNetCore.Http.StatusCodes;
@@ -68,5 +69,30 @@ public IActionResult Post( [FromBody] Order order )
6869
[ProducesResponseType( Status404NotFound )]
6970
[EnableQuery( AllowedQueryOptions = Select )]
7071
public SingleResult<Order> MostExpensive() => SingleResult.Create( new[] { new Order() { Id = 42, Customer = "Bill Mei" } }.AsQueryable() );
72+
73+
/// <summary>
74+
/// Gets the line items for the specified order.
75+
/// </summary>
76+
/// <param name="key">The order identifier.</param>
77+
/// <returns>The order line items.</returns>
78+
/// <response code="200">The line items were successfully retrieved.</response>
79+
/// <response code="404">The order does not exist.</response>
80+
[HttpGet]
81+
[ODataRoute( "({key})/LineItems" )]
82+
[Produces( "application/json" )]
83+
[ProducesResponseType( typeof( ODataValue<IEnumerable<LineItem>> ), Status200OK )]
84+
[ProducesResponseType( Status404NotFound )]
85+
[EnableQuery( AllowedQueryOptions = Select )]
86+
public IActionResult LineItems( int key )
87+
{
88+
var lineItems = new[]
89+
{
90+
new LineItem() { Number = 1, Quantity = 1, UnitPrice = 2m, Description = "Dry erase wipes" },
91+
new LineItem() { Number = 2, Quantity = 1, UnitPrice = 3.5m, Description = "Dry erase eraser" },
92+
new LineItem() { Number = 3, Quantity = 1, UnitPrice = 5m, Description = "Dry erase markers" },
93+
};
94+
95+
return Ok( lineItems );
96+
}
7197
}
7298
}

samples/aspnetcore/SwaggerODataSample/V2/OrdersController.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,30 @@ public IActionResult Rate( int key, ODataActionParameters parameters )
142142
var rating = (int) parameters["rating"];
143143
return NoContent();
144144
}
145+
146+
/// <summary>
147+
/// Gets the line items for the specified order.
148+
/// </summary>
149+
/// <param name="key">The order identifier.</param>
150+
/// <returns>The order line items.</returns>
151+
/// <response code="200">The line items were successfully retrieved.</response>
152+
/// <response code="404">The order does not exist.</response>
153+
[HttpGet]
154+
[ODataRoute( "({key})/LineItems" )]
155+
[Produces( "application/json" )]
156+
[ProducesResponseType( typeof( ODataValue<IEnumerable<LineItem>> ), Status200OK )]
157+
[ProducesResponseType( Status404NotFound )]
158+
[EnableQuery( AllowedQueryOptions = Select )]
159+
public IActionResult LineItems( int key )
160+
{
161+
var lineItems = new[]
162+
{
163+
new LineItem() { Number = 1, Quantity = 1, UnitPrice = 2m, Description = "Dry erase wipes" },
164+
new LineItem() { Number = 2, Quantity = 1, UnitPrice = 3.5m, Description = "Dry erase eraser" },
165+
new LineItem() { Number = 3, Quantity = 1, UnitPrice = 5m, Description = "Dry erase markers" },
166+
};
167+
168+
return Ok( lineItems );
169+
}
145170
}
146171
}

samples/aspnetcore/SwaggerODataSample/V2/PeopleController.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,26 @@ public IActionResult Get( int key, ODataQueryOptions<Person> options )
120120
[Produces( "application/json" )]
121121
[ProducesResponseType( typeof( ODataValue<IEnumerable<Person>> ), Status200OK )]
122122
public IActionResult NewHires( DateTime since, ODataQueryOptions<Person> options ) => Get( options );
123+
124+
/// <summary>
125+
/// Gets the home address of a person.
126+
/// </summary>
127+
/// <param name="key">The person identifier.</param>
128+
/// <returns>The person's home address.</returns>
129+
/// <response code="200">The home address was successfully retrieved.</response>
130+
/// <response code="404">The person does not exist.</response>
131+
[HttpGet]
132+
[Produces( "application/json" )]
133+
[ProducesResponseType( typeof( Address ), Status200OK )]
134+
[ProducesResponseType( Status404NotFound )]
135+
public IActionResult GetHomeAddress( int key ) =>
136+
Ok( new Address()
137+
{
138+
Id = 42,
139+
Street = "123 Some Place",
140+
City = "Seattle",
141+
State = "WA",
142+
ZipCode = "98101"
143+
} );
123144
}
124145
}

samples/aspnetcore/SwaggerODataSample/V3/OrdersController.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,30 @@ public IActionResult Rate( int key, ODataActionParameters parameters )
155155
var rating = (int) parameters["rating"];
156156
return NoContent();
157157
}
158+
159+
/// <summary>
160+
/// Gets the line items for the specified order.
161+
/// </summary>
162+
/// <param name="key">The order identifier.</param>
163+
/// <returns>The order line items.</returns>
164+
/// <response code="200">The line items were successfully retrieved.</response>
165+
/// <response code="404">The order does not exist.</response>
166+
[HttpGet]
167+
[ODataRoute( "({key})/LineItems" )]
168+
[Produces( "application/json" )]
169+
[ProducesResponseType( typeof( ODataValue<IEnumerable<LineItem>> ), Status200OK )]
170+
[ProducesResponseType( Status404NotFound )]
171+
[EnableQuery( AllowedQueryOptions = Select )]
172+
public IActionResult LineItems( int key )
173+
{
174+
var lineItems = new[]
175+
{
176+
new LineItem() { Number = 1, Quantity = 1, UnitPrice = 2m, Description = "Dry erase wipes" },
177+
new LineItem() { Number = 2, Quantity = 1, UnitPrice = 3.5m, Description = "Dry erase eraser" },
178+
new LineItem() { Number = 3, Quantity = 1, UnitPrice = 5m, Description = "Dry erase markers" },
179+
};
180+
181+
return Ok( lineItems );
182+
}
158183
}
159184
}

0 commit comments

Comments
 (0)