-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an alternate key sample and its related docs
- Loading branch information
Showing
18 changed files
with
812 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sample/ODataAlternateKeySample/Controllers/CustomersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
sample/ODataAlternateKeySample/Controllers/OrdersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
sample/ODataAlternateKeySample/Controllers/PeopleController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
sample/ODataAlternateKeySample/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
sample/ODataAlternateKeySample/Models/AlternateKeyRepositoryInMemory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
Oops, something went wrong.