Skip to content

Commit

Permalink
Add an alternate key sample and its related docs
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Feb 24, 2022
1 parent 8c40be1 commit 38338c0
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AspNetCoreOData.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataDynamicModel", "sample
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataSampleCommon", "sample\ODataSampleCommon\ODataSampleCommon.csproj", "{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataAlternateKeySample", "sample\ODataAlternateKeySample\ODataAlternateKeySample.csproj", "{7B153669-A42F-4511-8BDB-587B3B27B2F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +65,10 @@ Global
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Release|Any CPU.Build.0 = Release|Any CPU
{7B153669-A42F-4511-8BDB-587B3B27B2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B153669-A42F-4511-8BDB-587B3B27B2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B153669-A42F-4511-8BDB-587B3B27B2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B153669-A42F-4511-8BDB-587B3B27B2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -76,6 +82,7 @@ Global
{BDC5474B-9511-4CDF-83FE-376C7130F7F0} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{CE04E38B-547F-46C0-ABE4-F981E3A1874F} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{7B153669-A42F-4511-8BDB-587B3B27B2F3} = {B1F86961-6958-4617-ACA4-C231F95AE099}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {540C9752-AAC0-49EA-BA60-78490C90FF86}
Expand Down
71 changes: 71 additions & 0 deletions sample/ODataAlternateKeySample/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------------
// <copyright file="CustomersController.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataAlternateKeySample.Models;

namespace ODataAlternateKeySample.Controllers
{
public class CustomersController : ODataController
{
private readonly IAlternateKeyRepository _repository;

public CustomersController(IAlternateKeyRepository repository)
{
_repository = repository;
}

[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_repository.GetCustomers());
}

[HttpGet]
[EnableQuery]
public IActionResult Get(int key)
{
var c = _repository.GetCustomers().FirstOrDefault(c => c.Id == key);
if (c == null)
{
return NotFound();
}

return Ok(c);
}

// Alternate key: SSN
[HttpGet("odata/Customers(SSN={ssn})")]
public IActionResult GetCustomerBySSN(string ssn)
{
var c = _repository.GetCustomers().FirstOrDefault(c => c.SSN == ssn);
if (c == null)
{
return NotFound();
}

return Ok(c);
}

[HttpPatch("odata/Customers(SSN={ssnKey})")]
public IActionResult PatchCustomerBySSN(string ssnKey, Delta<Customer> delta)
{
var originalCustomer = _repository.GetCustomers().FirstOrDefault(c => c.SSN == ssnKey);
if (originalCustomer == null)
{
return NotFound();
}

delta.Patch(originalCustomer);
return Updated(originalCustomer);
}
}
}
70 changes: 70 additions & 0 deletions sample/ODataAlternateKeySample/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//-----------------------------------------------------------------------------
// <copyright file="OrdersController.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataAlternateKeySample.Models;

namespace ODataAlternateKeySample.Controllers
{
public class OrdersController : ODataController
{
private readonly IAlternateKeyRepository _repository;

public OrdersController(IAlternateKeyRepository repository)
{
_repository = repository;
}

[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_repository.GetOrders());
}

[HttpGet]
[EnableQuery]
public IActionResult Get(int key)
{
var c = _repository.GetOrders().FirstOrDefault(c => c.Id == key);
if (c == null)
{
return NotFound();
}

return Ok(c);
}

// alternate key: Name
[HttpGet("odata/Orders(Name={orderName})")]
public IActionResult GetOrderByName(string orderName)
{
var c = _repository.GetOrders().FirstOrDefault(c => c.Name == orderName);
if (c == null)
{
return NotFound();
}

return Ok(c);
}

// alternate key: Token
[HttpGet("odata/Orders(Token={token})")]
public IActionResult GetOrderByToken(Guid token)
{
var c = _repository.GetOrders().FirstOrDefault(c => c.Token == token);
if (c == null)
{
return NotFound();
}

return Ok(c);
}
}
}
56 changes: 56 additions & 0 deletions sample/ODataAlternateKeySample/Controllers/PeopleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//-----------------------------------------------------------------------------
// <copyright file="PeopleController.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataAlternateKeySample.Models;

namespace ODataAlternateKeySample.Controllers
{
public class PeopleController : ODataController
{
private readonly IAlternateKeyRepository _repository;

public PeopleController(IAlternateKeyRepository repository)
{
_repository = repository;
}

[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_repository.GetPeople());
}

[HttpGet]
[EnableQuery]
public IActionResult Get(int key)
{
var c = _repository.GetPeople().FirstOrDefault(c => c.Id == key);
if (c == null)
{
return NotFound();
}

return Ok(c);
}

[HttpGet("odata/People(c_or_r={cr},passport={passport})")]
public IActionResult FindPeopleByCountryAndPassport(string cr, string passport)
{
var c = _repository.GetPeople().FirstOrDefault(c => c.CountryOrRegion == cr && c.Passport == passport);
if (c == null)
{
return NotFound();
}

return Ok(c);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;

namespace ODataAlternateKeySample.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//-----------------------------------------------------------------------------
// <copyright file="IAlternateKeyRepository.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

namespace ODataAlternateKeySample.Models
{
public class AlternateKeyRepositoryInMemory : IAlternateKeyRepository
{
private static IList<Customer> _customers;
private static IList<Order> _orders;
private static IList<Person> _people;

static AlternateKeyRepositoryInMemory()
{
// Customers
var names = new[] { "Tom", "Jerry", "Mike", "Ben", "Sam", "Peter" };
_customers = Enumerable.Range(1, 5).Select(e => new Customer
{
Id = e,
Name = names[e - 1],
SSN = "SSN-" + e + "-" + (100 + e)
}).ToList();

// Orders
Guid[] tokes =
{
new Guid("196B3584-EF3D-41FD-90B4-76D59F9B929C"),
new Guid("6CED5600-28BA-40EE-A2DF-E80AFADBE6C7"),
new Guid("75036B94-C836-4946-8CC8-054CF54060EC"),
new Guid("B3FF5460-6E77-4678-B959-DCC1C4937FA7"),
new Guid("ED773C85-4E3C-4FC4-A3E9-9F1DA0A626DA"),
new Guid("E9CC3D9F-BC80-4D43-8C3E-ED38E8C9A8B6")
};

_orders = Enumerable.Range(1, 6).Select(e => new Order
{
Id = e,
Name = string.Format("Order-{0}", e),
Token = tokes[e - 1],
Amount = 10 * (e + 1) - e,
Price = 8 * e
}).ToList();

// People
var cs = new[] { "EN", "CN", "USA", "RU", "JP", "KO" };
var ps = new[] { "1001", "2010", "9999", "3199992", "00001", "8110" };
_people = Enumerable.Range(1, 6).Select(e => new Person
{
Id = e,
Name = names[e - 1],
CountryOrRegion = cs[e - 1],
Passport = ps[e - 1]
}).ToList();
}

public IEnumerable<Customer> GetCustomers() => _customers;

public IEnumerable<Order> GetOrders() => _orders;

public IEnumerable<Person> GetPeople() => _people;
}
}
21 changes: 21 additions & 0 deletions sample/ODataAlternateKeySample/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//-----------------------------------------------------------------------------
// <copyright file="Customer.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

namespace ODataAlternateKeySample.Models
{
/// <summary>
/// Entity type with one alternate key
/// </summary>
public class Customer
{
public int Id { get; set; }

public string Name { get; set; }

public string SSN { get; set; }
}
}
Loading

0 comments on commit 38338c0

Please sign in to comment.