Skip to content

Commit 27c9402

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Add singleton example
1 parent 2e2ad10 commit 27c9402

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

samples/aspnetcore/SwaggerODataSample/Configuration/SupplierConfiguration.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string rout
1717
return;
1818
}
1919

20-
var supplier = builder.EntitySet<Supplier>( "Suppliers" ).EntityType.HasKey( p => p.Id );
20+
builder.EntitySet<Supplier>( "Suppliers" ).EntityType.HasKey( p => p.Id );
21+
builder.Singleton<Supplier>( "Acme" );
2122
}
2223
}
2324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace Microsoft.Examples.V3
2+
{
3+
using Microsoft.AspNet.OData;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Examples.Models;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using static Microsoft.AspNetCore.Http.StatusCodes;
10+
11+
/// <summary>
12+
/// Represents a RESTful service for the ACME supplier.
13+
/// </summary>
14+
[ApiVersion( "3.0" )]
15+
public class AcmeController : ODataController
16+
{
17+
// <summary>
18+
/// Retrieves the ACME supplier.
19+
/// </summary>
20+
/// <returns>All available suppliers.</returns>
21+
/// <response code="200">The supplier successfully retrieved.</response>
22+
[EnableQuery]
23+
[Produces( "application/json" )]
24+
[ProducesResponseType( typeof( ODataValue<Supplier> ), Status200OK )]
25+
public IActionResult Get() => Ok( NewSupplier() );
26+
27+
/// <summary>
28+
/// Gets the products associated with the supplier.
29+
/// </summary>
30+
/// <returns>The associated supplier products.</returns>
31+
[EnableQuery]
32+
public IQueryable<Product> GetProducts() => NewSupplier().Products.AsQueryable();
33+
34+
/// <summary>
35+
/// Links a product to a supplier.
36+
/// </summary>
37+
/// <param name="navigationProperty">The product to link.</param>
38+
/// <param name="link">The product identifier.</param>
39+
/// <returns>None</returns>
40+
[HttpPost]
41+
[ProducesResponseType( Status204NoContent )]
42+
[ProducesResponseType( Status404NotFound )]
43+
public IActionResult CreateRef( [FromODataUri] string navigationProperty, [FromBody] Uri link ) => NoContent();
44+
45+
// TODO: OData doesn't seem to currently support this action in ASP.NET Core, but it works in Web API
46+
47+
/// <summary>
48+
/// Unlinks a product from a supplier.
49+
/// </summary>
50+
/// <param name="relatedKey">The related product identifier.</param>
51+
/// <param name="navigationProperty">The product to unlink.</param>
52+
/// <returns>None</returns>
53+
[ProducesResponseType( Status204NoContent )]
54+
[ProducesResponseType( Status404NotFound )]
55+
public IActionResult DeleteRef( [FromODataUri] string relatedKey, string navigationProperty ) => NoContent();
56+
57+
private static Supplier NewSupplier() =>
58+
new Supplier()
59+
{
60+
Id = 42,
61+
Name = "Acme",
62+
Products = new List<Product>()
63+
{
64+
new Product()
65+
{
66+
Id = 42,
67+
Name = "Product 42",
68+
Category = "Test",
69+
Price = 42,
70+
SupplierId = 42,
71+
}
72+
},
73+
};
74+
}
75+
}

samples/webapi/SwaggerODataWebApiSample/Configuration/SupplierConfiguration.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string rout
1717
return;
1818
}
1919

20-
var supplier = builder.EntitySet<Supplier>( "Suppliers" ).EntityType.HasKey( p => p.Id );
20+
builder.EntitySet<Supplier>( "Suppliers" ).EntityType.HasKey( p => p.Id );
21+
builder.Singleton<Supplier>( "Acme" );
2122
}
2223
}
2324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace Microsoft.Examples.V3
2+
{
3+
using Microsoft.AspNet.OData;
4+
using Microsoft.Examples.Models;
5+
using Microsoft.Web.Http;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Web.Http;
10+
using System.Web.Http.Description;
11+
using static System.Net.HttpStatusCode;
12+
13+
/// <summary>
14+
/// Represents a RESTful service for the ACME supplier.
15+
/// </summary>
16+
[ApiVersion( "3.0" )]
17+
public class AcmeController : ODataController
18+
{
19+
/// <summary>
20+
/// Retrieves the ACME supplier.
21+
/// </summary>
22+
/// <returns>The ACME supplier.</returns>
23+
/// <response code="200">The supplier was successfully retrieved.</response>
24+
[EnableQuery]
25+
[ResponseType( typeof( ODataValue<Supplier> ) )]
26+
public IHttpActionResult Get() => Ok( NewSupplier() );
27+
28+
/// <summary>
29+
/// Gets the products associated with the supplier.
30+
/// </summary>
31+
/// <returns>The associated supplier products.</returns>
32+
[EnableQuery]
33+
public IQueryable<Product> GetProducts() => NewSupplier().Products.AsQueryable();
34+
35+
/// <summary>
36+
/// Links a product to a supplier.
37+
/// </summary>
38+
/// <param name="navigationProperty">The product to link.</param>
39+
/// <param name="link">The product identifier.</param>
40+
/// <returns>None</returns>
41+
[HttpPost]
42+
public IHttpActionResult CreateRef( string navigationProperty, [FromBody] Uri link ) => StatusCode( NoContent );
43+
44+
/// <summary>
45+
/// Unlinks a product from a supplier.
46+
/// </summary>
47+
/// <param name="relatedKey">The related product identifier.</param>
48+
/// <param name="navigationProperty">The product to unlink.</param>
49+
/// <returns>None</returns>
50+
public IHttpActionResult DeleteRef( [FromODataUri] string relatedKey, string navigationProperty ) => StatusCode( NoContent );
51+
52+
private static Supplier NewSupplier() =>
53+
new Supplier()
54+
{
55+
Id = 42,
56+
Name = "Acme",
57+
Products = new List<Product>()
58+
{
59+
new Product()
60+
{
61+
Id = 42,
62+
Name = "Product 42",
63+
Category = "Test",
64+
Price = 42,
65+
SupplierId = 42,
66+
}
67+
},
68+
};
69+
}
70+
}

0 commit comments

Comments
 (0)