Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/OData/AspNetCoreOData into …
Browse files Browse the repository at this point in the history
…ODataRoutingSample.Tests
  • Loading branch information
[email protected] committed Mar 26, 2022
2 parents 71294c7 + a10e8e5 commit af22290
Show file tree
Hide file tree
Showing 43 changed files with 1,426 additions and 37 deletions.
39 changes: 39 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''

---

**Assemblies affected**
Which assemblies and versions are known to be affected e.g. ASP.NET Core OData 8.x

**Describe the bug**
A clear and concise description of what the bug is.

**Reproduce steps**
The simplest set of steps to reproduce the issue. If possible, reference a commit that demonstrates the issue.

**Data Model**
Please share your Data model, for example, your C# class.

**EDM (CSDL) Model**
Please share your Edm model, for example, CSDL file.
You can send `$metadata` to get a CSDL XML content.

**Request/Response**
Please share your request Uri, head or the request body
Please share your response head, body.

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.


**Additional context**
Please share your call stack or any error message
Add any other context about the problem here.
6 changes: 6 additions & 0 deletions AspNetCoreOData.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataSampleCommon", "sample\ODataSampleCommon\ODataSampleCommon.csproj", "{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODataRoutingSample.Tests", "sample\ODataRoutingSample.Tests\ODataRoutingSample.Tests.csproj", "{2084B1F8-5122-41A1-8735-06F36E7C9903}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataAlternateKeySample", "sample\ODataAlternateKeySample\ODataAlternateKeySample.csproj", "{7B153669-A42F-4511-8BDB-587B3B27B2F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -69,6 +70,10 @@ Global
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.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 @@ -83,6 +88,7 @@ Global
{CE04E38B-547F-46C0-ABE4-F981E3A1874F} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{2084B1F8-5122-41A1-8735-06F36E7C9903} = {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
78 changes: 78 additions & 0 deletions sample/ODataAlternateKeySample/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//-----------------------------------------------------------------------------
// <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);
}

[HttpPost]
[EnableQuery]
public IActionResult Post([FromBody]Customer c)
{
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,66 @@
//-----------------------------------------------------------------------------
// <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),
Titles = new string[] { "abc", null, "efg" }
}).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;
}
}
23 changes: 23 additions & 0 deletions sample/ODataAlternateKeySample/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//-----------------------------------------------------------------------------
// <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; }

public string[] Titles { get; set; }
}
}
Loading

0 comments on commit af22290

Please sign in to comment.