From 080cb287d2f0d537b6fe40a1cb3c56ed9a3432eb Mon Sep 17 00:00:00 2001 From: Vincent He Date: Tue, 30 Aug 2016 08:07:30 +0800 Subject: [PATCH] Update URL resolver --- .../OperationTests.cs | 6 +- .../App_Start/WebApiConfig.cs | 13 +- .../Controllers/NorthwindController.cs | 37 ------ ...soft.OData.Service.Sample.Northwind.csproj | 1 + .../Models/NorthwindApi.cs | 2 +- .../Models/NorthwindApi2.cs | 116 ++++++++++++++++++ 6 files changed, 130 insertions(+), 45 deletions(-) create mode 100644 test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi2.cs diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind.Tests/OperationTests.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind.Tests/OperationTests.cs index 86429595..d3ae88d8 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind.Tests/OperationTests.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind.Tests/OperationTests.cs @@ -26,8 +26,7 @@ public async Task FunctionCallWithUnqualifiedName() { await FunctionCall(false, (config, server) => { - config.SetUriResolver(new UnqualifiedODataUriResolver()); - WebApiConfig.RegisterNorthwind(config, server); + WebApiConfig.RegisterNorthwind2(config, server); }); } @@ -55,8 +54,7 @@ public async Task ActionCallWithUnqualifiedName() { await ActionCall(true, (config, server) => { - config.SetUriResolver(new UnqualifiedODataUriResolver()); - WebApiConfig.RegisterNorthwind(config, server); + WebApiConfig.RegisterNorthwind2(config, server); }); } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/App_Start/WebApiConfig.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/App_Start/WebApiConfig.cs index 775683b0..6a3249c3 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/App_Start/WebApiConfig.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/App_Start/WebApiConfig.cs @@ -4,7 +4,6 @@ using System.Web.Http; using System.Web.OData.Extensions; using Microsoft.OData.Service.Sample.Northwind.Models; -using Microsoft.OData.UriParser; using Microsoft.Restier.Publishers.OData; using Microsoft.Restier.Publishers.OData.Batch; @@ -18,8 +17,7 @@ public static void Register(HttpConfiguration config) // Web API routes config.MapHttpAttributeRoutes(); - - config.SetUriResolver(new UnqualifiedODataUriResolver()); + RegisterNorthwind(config, GlobalConfiguration.DefaultServer); config.Routes.MapHttpRoute( @@ -38,5 +36,14 @@ public static void RegisterNorthwind( "NorthwindApi", "api/Northwind", new RestierBatchHandler(server)); } + + public static void RegisterNorthwind2( + HttpConfiguration config, HttpServer server) + { + config.SetUseVerboseErrors(true); + config.MapRestierRoute( + "NorthwindApi", "api/Northwind", + new RestierBatchHandler(server)); + } } } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Controllers/NorthwindController.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Controllers/NorthwindController.cs index 8331ada0..a46fb7d3 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Controllers/NorthwindController.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Controllers/NorthwindController.cs @@ -61,43 +61,6 @@ public IHttpActionResult UpdateProductUnitPrice(int key, [FromBody]decimal price return Ok(price); } - [HttpGet] - [ODataRoute("Products/Microsoft.OData.Service.Sample.Northwind.Models.MostExpensive")] - public IHttpActionResult MostExpensive() - { - var product = DbContext.Products.Max(p => p.UnitPrice); - return Ok(product); - } - - [HttpPost] - [ODataRoute("Products({key})/Microsoft.OData.Service.Sample.Northwind.Models.IncreasePrice")] - public IHttpActionResult IncreasePrice([FromODataUri] int key, ODataActionParameters parameters) - { - var entity = DbContext.Products.FirstOrDefault(e => e.ProductID == key); - if (entity == null) - { - return NotFound(); - } - entity.UnitPrice = entity.UnitPrice + (int)parameters["diff"]; - - try - { - DbContext.SaveChanges(); - } - catch (DbUpdateConcurrencyException) - { - if (!DbContext.Products.Any(p => p.ProductID == key)) - { - return NotFound(); - } - else - { - throw; - } - } - return StatusCode(HttpStatusCode.NoContent); - } - [HttpPost] [ODataRoute("ResetDataSource")] public IHttpActionResult ResetDataSource() diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Microsoft.OData.Service.Sample.Northwind.csproj b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Microsoft.OData.Service.Sample.Northwind.csproj index eaa03096..b31a72f5 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Microsoft.OData.Service.Sample.Northwind.csproj +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Microsoft.OData.Service.Sample.Northwind.csproj @@ -125,6 +125,7 @@ + diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi.cs index e6a0b2b3..7e870c8d 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi.cs @@ -4,12 +4,12 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Web.OData; using Microsoft.Extensions.DependencyInjection; using Microsoft.OData.Edm; +using Microsoft.OData.UriParser; using Microsoft.Restier.Core; using Microsoft.Restier.Core.Model; using Microsoft.Restier.Providers.EntityFramework; diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi2.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi2.cs new file mode 100644 index 00000000..698c5de4 --- /dev/null +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Sample.Northwind/Models/NorthwindApi2.cs @@ -0,0 +1,116 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Web.OData; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OData.Edm; +using Microsoft.OData.UriParser; +using Microsoft.Restier.Core; +using Microsoft.Restier.Core.Model; +using Microsoft.Restier.Providers.EntityFramework; +using Microsoft.Restier.Publishers.OData.Model; + +namespace Microsoft.OData.Service.Sample.Northwind.Models +{ + /// + /// This class is only used for unqualified operation call test + /// + public class NorthwindApi2 : EntityFrameworkApi + { + public NorthwindContext ModelContext { get { return DbContext; } } + + // Imperative views. Currently CUD operations not supported + [Resource] + public IQueryable ExpensiveProducts + { + get + { + return this.GetQueryableSource("Products") + .Where(c => c.UnitPrice > 50); + } + } + + [Resource] + public IQueryable CurrentOrders + { + get + { + return this.GetQueryableSource("Orders") + .Where(o => o.ShippedDate == null); + } + } + + [Operation(IsBound = true, HasSideEffects = true)] + public void IncreasePrice(Product bindingParameter, int diff) + { + } + + [Operation(HasSideEffects = true)] + public void ResetDataSource() + { + } + + [Operation(IsBound = true)] + public double MostExpensive(IEnumerable bindingParameter) + { + return 0.0; + } + + public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services) + { + return EntityFrameworkApi.ConfigureApi(apiType, services) + .AddService() + .AddSingleton(new UnqualifiedODataUriResolver()); + } + + // Entity set filter + protected IQueryable OnFilterCustomer(IQueryable customers) + { + return customers.Where(c => c.CountryRegion == "France"); + } + + // Submit logic + protected void OnUpdatingProducts(Product product) + { + WriteLog(DateTime.Now.ToString() + product.ProductID + " is being updated"); + } + + protected void OnInsertedProducts(Product product) + { + WriteLog(DateTime.Now.ToString() + product.ProductID + " has been inserted"); + } + + private void WriteLog(string text) + { + // Fake writing log method for submit logic demo + } + + private class NorthwindModelExtender : IModelBuilder + { + public IModelBuilder InnerHandler { get; set; } + + public async Task GetModelAsync(ModelContext context, CancellationToken cancellationToken) + { + var model = await InnerHandler.GetModelAsync(context, cancellationToken); + + // enable auto-expand through model annotation. + var orderType = (EdmEntityType)model.SchemaElements.Single(e => e.Name == "Order"); + var orderDetailsProperty = (EdmNavigationProperty)orderType.DeclaredProperties + .Single(prop => prop.Name == "Order_Details"); + model.SetAnnotationValue(orderDetailsProperty, + new QueryableRestrictionsAnnotation(new QueryableRestrictions { AutoExpand = true })); + + return model; + } + } + + public NorthwindApi2(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + } +} \ No newline at end of file