forked from simplefx/Simple.OData
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathFunctionStartup.cs
More file actions
79 lines (65 loc) · 2.13 KB
/
FunctionStartup.cs
File metadata and controls
79 lines (65 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Web.Http;
using Microsoft.AspNetCore.OData.Batch;
using Microsoft.OData.Edm;
using WebApiOData.V4.Samples.Controllers;
using WebApiOData.V4.Samples.Models;
namespace WebApiOData.V4.Samples.Startups;
public class FunctionStartup : Startup
{
public FunctionStartup()
: base(typeof(ProductsController))
{
}
protected override void ConfigureController(HttpConfiguration config)
{
config.MapODataServiceRoute(
routeName: "OData functions",
routePrefix: "functions",
model: GetEdmModel(config),
batchHandler: new DefaultODataBatchHandler(new HttpServer(config)));
}
private static IEdmModel GetEdmModel(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder(config);
builder.EntitySet<Product>("Products");
var productType = builder.EntityType<Product>();
// Function bound to a collection
// Returns the most expensive product, a single entity
productType.Collection
.Function("MostExpensive")
.Returns<double>();
// Function bound to a collection
// Returns top 3 product, a collection
productType.Collection
.Function("MostExpensives")
.ReturnsCollectionFromEntitySet<Product>("Products");
// Function bound to a collection
// Returns the top 10 product, a collection
productType.Collection
.Function("Top10")
.ReturnsCollectionFromEntitySet<Product>("Products");
// Function bound to a single entity
// Returns the instance's price rank among all products
productType
.Function("GetPriceRank")
.Returns<int>();
// Function bound to a single entity
// Accept a string as parameter and return a double
// This function calculate the general sales tax base on the
// state
productType
.Function("CalculateGeneralSalesTax")
.Returns<double>()
.Parameter<string>("state");
// Function bound to a single entity
// Returns the list of movies with a product placement for this product
productType
.Function("Placements")
.ReturnsCollectionFromEntitySet<Movie>("Movies");
// Unbound Function
builder.Function("GetSalesTaxRate")
.Returns<double>()
.Parameter<string>("state");
return builder.GetEdmModel();
}
}