Skip to content

Commit

Permalink
Update URL resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Aug 30, 2016
1 parent 718b7c5 commit 080cb28
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(
Expand All @@ -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<NorthwindApi2>(
"NorthwindApi", "api/Northwind",
new RestierBatchHandler(server));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<Compile Include="Models\Customer.cs" />
<Compile Include="Models\CustomerDemographic.cs" />
<Compile Include="Models\Employee.cs" />
<Compile Include="Models\NorthwindApi2.cs" />
<Compile Include="Models\NorthwindContext.cs" />
<Compile Include="Models\NorthwindApi.cs" />
<Compile Include="Models\Order.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This class is only used for unqualified operation call test
/// </summary>
public class NorthwindApi2 : EntityFrameworkApi<NorthwindContext>
{
public NorthwindContext ModelContext { get { return DbContext; } }

// Imperative views. Currently CUD operations not supported
[Resource]
public IQueryable<Product> ExpensiveProducts
{
get
{
return this.GetQueryableSource<Product>("Products")
.Where(c => c.UnitPrice > 50);
}
}

[Resource]
public IQueryable<Order> CurrentOrders
{
get
{
return this.GetQueryableSource<Order>("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<Product> bindingParameter)
{
return 0.0;
}

public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services)
{
return EntityFrameworkApi<NorthwindContext>.ConfigureApi(apiType, services)
.AddService<IModelBuilder, NorthwindModelExtender>()
.AddSingleton<ODataUriResolver>(new UnqualifiedODataUriResolver());
}

// Entity set filter
protected IQueryable<Customer> OnFilterCustomer(IQueryable<Customer> 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<IEdmModel> 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)
{
}
}
}

0 comments on commit 080cb28

Please sign in to comment.