Skip to content

Commit 510137e

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update Web API example with navigation properties
1 parent e0cfb9e commit 510137e

File tree

11 files changed

+238
-0
lines changed

11 files changed

+238
-0
lines changed

samples/webapi/SwaggerODataWebApiSample/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/webapi/SwaggerODataWebApiSample/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/webapi/SwaggerODataWebApiSample/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/webapi/SwaggerODataWebApiSample/Models/Person.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Microsoft.Examples.Models
22
{
3+
using Microsoft.AspNet.OData.Builder;
34
using System;
45
using System.ComponentModel.DataAnnotations;
56

@@ -41,5 +42,17 @@ public class Person
4142
/// </summary>
4243
/// <value>The person's telephone number.</value>
4344
public string Phone { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets the person's home address.
48+
/// </summary>
49+
/// <value>The person's home address.</value>
50+
public Address HomeAddress { get; set; }
51+
52+
/// <summary>
53+
/// Gets or sets the person's work address.
54+
/// </summary>
55+
/// <value>The person's work address.</value>
56+
public Address WorkAddress { get; set; }
4457
}
4558
}

samples/webapi/SwaggerODataWebApiSample/V1/OrdersController.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNet.OData.Routing;
55
using Microsoft.Examples.Models;
66
using Microsoft.Web.Http;
7+
using System.Collections.Generic;
78
using System.Linq;
89
using System.Web.Http;
910
using System.Web.Http.Description;
@@ -65,5 +66,28 @@ public IHttpActionResult Post( [FromBody] Order order )
6566
[ResponseType( typeof( Order ) )]
6667
[EnableQuery( AllowedQueryOptions = Select )]
6768
public SingleResult<Order> MostExpensive() => SingleResult.Create( new[] { new Order() { Id = 42, Customer = "Bill Mei" } }.AsQueryable() );
69+
70+
/// <summary>
71+
/// Gets the line items for the specified order.
72+
/// </summary>
73+
/// <param name="key">The order identifier.</param>
74+
/// <returns>The order line items.</returns>
75+
/// <response code="200">The line items were successfully retrieved.</response>
76+
/// <response code="404">The order does not exist.</response>
77+
[HttpGet]
78+
[ODataRoute( "({key})/LineItems" )]
79+
[ResponseType( typeof( ODataValue<IEnumerable<LineItem>> ) )]
80+
[EnableQuery( AllowedQueryOptions = Select )]
81+
public IHttpActionResult LineItems( int key )
82+
{
83+
var lineItems = new[]
84+
{
85+
new LineItem() { Number = 1, Quantity = 1, UnitPrice = 2m, Description = "Dry erase wipes" },
86+
new LineItem() { Number = 2, Quantity = 1, UnitPrice = 3.5m, Description = "Dry erase eraser" },
87+
new LineItem() { Number = 3, Quantity = 1, UnitPrice = 5m, Description = "Dry erase markers" },
88+
};
89+
90+
return Ok( lineItems );
91+
}
6892
}
6993
}

samples/webapi/SwaggerODataWebApiSample/V2/OrdersController.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,28 @@ public IHttpActionResult Rate( int key, ODataActionParameters parameters )
131131
var rating = (int) parameters["rating"];
132132
return StatusCode( NoContent );
133133
}
134+
135+
/// <summary>
136+
/// Gets the line items for the specified order.
137+
/// </summary>
138+
/// <param name="key">The order identifier.</param>
139+
/// <returns>The order line items.</returns>
140+
/// <response code="200">The line items were successfully retrieved.</response>
141+
/// <response code="404">The order does not exist.</response>
142+
[HttpGet]
143+
[ODataRoute( "({key})/LineItems" )]
144+
[ResponseType( typeof( ODataValue<IEnumerable<LineItem>> ) )]
145+
[EnableQuery( AllowedQueryOptions = Select )]
146+
public IHttpActionResult LineItems( int key )
147+
{
148+
var lineItems = new[]
149+
{
150+
new LineItem() { Number = 1, Quantity = 1, UnitPrice = 2m, Description = "Dry erase wipes" },
151+
new LineItem() { Number = 2, Quantity = 1, UnitPrice = 3.5m, Description = "Dry erase eraser" },
152+
new LineItem() { Number = 3, Quantity = 1, UnitPrice = 5m, Description = "Dry erase markers" },
153+
};
154+
155+
return Ok( lineItems );
156+
}
134157
}
135158
}

samples/webapi/SwaggerODataWebApiSample/V2/PeopleController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,24 @@ public IHttpActionResult Get( int key, ODataQueryOptions<Person> options )
112112
[HttpGet]
113113
[ResponseType( typeof( ODataValue<IEnumerable<Person>> ) )]
114114
public IHttpActionResult NewHires( DateTime since, ODataQueryOptions<Person> options ) => Get( options );
115+
116+
/// <summary>
117+
/// Gets the home address of a person.
118+
/// </summary>
119+
/// <param name="key">The person identifier.</param>
120+
/// <returns>The person's home address.</returns>
121+
/// <response code="200">The home address was successfully retrieved.</response>
122+
/// <response code="404">The person does not exist.</response>
123+
[HttpGet]
124+
[ResponseType( typeof( Address ) )]
125+
public IHttpActionResult GetHomeAddress( int key ) =>
126+
Ok( new Address()
127+
{
128+
Id = 42,
129+
Street = "123 Some Place",
130+
City = "Seattle",
131+
State = "WA",
132+
ZipCode = "98101"
133+
} );
115134
}
116135
}

samples/webapi/SwaggerODataWebApiSample/V3/OrdersController.cs

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

0 commit comments

Comments
 (0)