Skip to content

Commit

Permalink
modify the northwind sample
Browse files Browse the repository at this point in the history
modify according to code review
  • Loading branch information
liqian19891011 authored and congysu committed Oct 9, 2014
1 parent 4085ac1 commit 2d59d27
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Domain;
using System.Web.OData.Routing;
Expand All @@ -37,10 +38,47 @@ private NorthwindContext DbContext
}
}

// OData Attibute Routing
[ODataRoute("Customers({key})/CompanyName")]
[ODataRoute("Customers({key})/CompanyName/$value")]
public string GetCustomerCompanyName([FromODataUri]string key)
{
return DbContext.Customers.Where(c => c.CustomerID == key).Select(c => c.CompanyName).FirstOrDefault();
}

[ODataRoute("Products/$count")]
public IHttpActionResult GetProductsCount()
{
return Ok(DbContext.Products.Count());
}

[HttpPut]
[ODataRoute("Products({key})/UnitPrice")]
public IHttpActionResult UpdateProductUnitPrice(int key, [FromBody]decimal price)
{
var entity = DbContext.Products.Find(key);
if (entity == null)
{
return NotFound();
}
entity.UnitPrice = price;

try
{
DbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!DbContext.Products.Any(p => p.ProductID == key))
{
return NotFound();
}
else
{
throw;
}
}
return Ok(price);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,37 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Microsoft.Data.Domain.EntityFramework;
using Microsoft.Data.Domain.Security;

namespace Microsoft.Data.Domain.Samples.Northwind.Models
{
[EnableConventions]
[EnableRoleBasedSecurity]
[Grant(DomainPermissionType.Inspect)]
[Grant(DomainPermissionType.All, On = "Customers")]
[Grant(DomainPermissionType.All, On = "Employees")]
[Grant(DomainPermissionType.All, On = "CurrentOrders")]
[Grant(DomainPermissionType.All, On = "Orders")] //, To = "Manager")]
[Grant(DomainPermissionType.All, On = "Products")]
[Grant(DomainPermissionType.All, On = "CurrentOrders")]
[Grant(DomainPermissionType.All, On = "ExpensiveProducts")]
[Grant(DomainPermissionType.All, On = "Orders")]
[Grant(DomainPermissionType.All, On = "Employees")]
[Grant(DomainPermissionType.Inspect, On = "Suppliers")]
[Grant(DomainPermissionType.Read, On = "Suppliers")]

public class NorthwindDomain : DbDomain<NorthwindContext>
{
public NorthwindContext Context { get { return DbContext; } }

// [Assert("Manager")]
// Imperative views. Currenly CUD operations not supported
protected IQueryable<Product> ExpensiveProducts
{
get
{
return this.Source<Product>("Products")
.Where(c => c.UnitPrice > 50);
}
}

protected IQueryable<Order> CurrentOrders
{
get
Expand All @@ -50,9 +59,26 @@ protected IQueryable<Order> CurrentOrders
}
}

// Entity set filter
private IQueryable<Customer> OnFilterCustomers(IQueryable<Customer> customers)
{
return customers.Where(c => c.CountryRegion == "France");
}

// Submit logic
private void OnUpdatingProducts(Product product)
{
WriteLog(DateTime.Now.ToString() + product.ProductID + " is being updated");
}

private 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,11 @@
<Property Name="definition" Type="Edm.Binary" MaxLength="max" />
</EntityType>
<EntityContainer Name="NorthwindContext">
<EntitySet Name="Alphabetical_list_of_products" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Alphabetical_list_of_product" />
<EntitySet Name="Categories" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Category">
<NavigationPropertyBinding Path="Products" Target="Products" />
</EntitySet>
<EntitySet Name="Products" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Product">
<NavigationPropertyBinding Path="Category" Target="Categories" />
<NavigationPropertyBinding Path="Order_Details" Target="Order_Details" />
<NavigationPropertyBinding Path="Supplier" Target="Suppliers" />
</EntitySet>
<EntitySet Name="Order_Details" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Order_Detail">
<NavigationPropertyBinding Path="Order" Target="Orders" />
<NavigationPropertyBinding Path="Product" Target="Products" />
</EntitySet>
<EntitySet Name="Orders" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Order">
<NavigationPropertyBinding Path="Customer" Target="Customers" />
<NavigationPropertyBinding Path="Employee" Target="Employees" />
Expand All @@ -421,44 +413,16 @@
<NavigationPropertyBinding Path="CustomerDemographics" Target="CustomerDemographics" />
<NavigationPropertyBinding Path="Orders" Target="Orders" />
</EntitySet>
<EntitySet Name="CustomerDemographics" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.CustomerDemographic">
<NavigationPropertyBinding Path="Customers" Target="Customers" />
</EntitySet>
<EntitySet Name="Employees" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Employee">
<NavigationPropertyBinding Path="Employees1" Target="Employees" />
<NavigationPropertyBinding Path="Employee1" Target="Employees" />
<NavigationPropertyBinding Path="Orders" Target="Orders" />
<NavigationPropertyBinding Path="Territories" Target="Territories" />
</EntitySet>
<EntitySet Name="Territories" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Territory">
<NavigationPropertyBinding Path="Region" Target="Regions" />
<NavigationPropertyBinding Path="Employees" Target="Employees" />
</EntitySet>
<EntitySet Name="Regions" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Region">
<NavigationPropertyBinding Path="Territories" Target="Territories" />
</EntitySet>
<EntitySet Name="Shippers" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Shipper">
<NavigationPropertyBinding Path="Orders" Target="Orders" />
</EntitySet>
<EntitySet Name="Suppliers" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Supplier">
<NavigationPropertyBinding Path="Products" Target="Products" />
</EntitySet>
<EntitySet Name="Category_Sales_for_1997" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Category_Sales_for_1997" />
<EntitySet Name="Contacts" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Contact" />
<EntitySet Name="Current_Product_Lists" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Current_Product_List" />
<EntitySet Name="Customer_and_Suppliers_by_Cities" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Customer_and_Suppliers_by_City" />
<EntitySet Name="Invoices" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Invoice" />
<EntitySet Name="Order_Details_Extendeds" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Order_Details_Extended" />
<EntitySet Name="Order_Subtotals" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Order_Subtotal" />
<EntitySet Name="Orders_Qries" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Orders_Qry" />
<EntitySet Name="Product_Sales_for_1997" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Product_Sales_for_1997" />
<EntitySet Name="Products_Above_Average_Prices" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Products_Above_Average_Price" />
<EntitySet Name="Products_by_Categories" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Products_by_Category" />
<EntitySet Name="Sales_by_Categories" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Sales_by_Category" />
<EntitySet Name="Sales_Totals_by_Amounts" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Sales_Totals_by_Amount" />
<EntitySet Name="Summary_of_Sales_by_Quarters" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Summary_of_Sales_by_Quarter" />
<EntitySet Name="Summary_of_Sales_by_Years" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Summary_of_Sales_by_Year" />
<EntitySet Name="sysdiagrams" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.sysdiagram" />
<EntitySet Name="ExpensiveProducts" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Product" />
<EntitySet Name="CurrentOrders" EntityType="Microsoft.Data.Domain.Samples.Northwind.Models.Order" />
</EntityContainer>
</Schema>
Expand Down

0 comments on commit 2d59d27

Please sign in to comment.