Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a sample with composite key
  • Loading branch information
xuzhg committed Nov 9, 2020
1 parent df3d601 commit f5d4b18
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
54 changes: 54 additions & 0 deletions sample/ODataRoutingSample/Controllers/PeopleController.cs
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using ODataRoutingSample.Models;

namespace ODataRoutingSample.Controllers
{
public class PeopleController : ControllerBase
{
private static IList<Person> _persons = new List<Person>
{
new Person
{
FirstName = "Goods",
LastName = "Zhangg",
},
new Person
{
FirstName = "Magazine",
LastName = "Jingchan",
},
new Person
{
FirstName = "Fiction",
LastName = "Hollewye"
},
};


[HttpGet]
[EnableQuery]
public IActionResult Get(CancellationToken token)
{
return Ok(_persons);
}

[EnableQuery]
public IActionResult Get(string keyFirstName, string keyLastName)
{
var person = _persons.FirstOrDefault(p => p.FirstName == keyFirstName && p.LastName == keyLastName);
if (person == null)
{
return NotFound($"Not found person with FirstName = {keyFirstName} and LastName = {keyLastName}");
}

return Ok(person);
}
}
}
2 changes: 0 additions & 2 deletions sample/ODataRoutingSample/Controllers/ProductsController.cs
Expand Up @@ -192,7 +192,5 @@ public string CalculateSalary(int min, int max, string name)
{
return $"Unbound function call on CalculateSalary: min={min}, max={max}, name={name}";
}


}
}
1 change: 1 addition & 0 deletions sample/ODataRoutingSample/Models/EdmModelBuilder.cs
Expand Up @@ -15,6 +15,7 @@ public static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
builder.EntitySet<Person>("People").EntityType.HasKey(c => new { c.FirstName, c.LastName });

// function with optional parameters
var functionWithOptional = builder.EntityType<Product>().Collection.Function("GetWholeSalary").ReturnsCollectionFromEntitySet<Order>("Orders");
Expand Down
12 changes: 12 additions & 0 deletions sample/ODataRoutingSample/Models/Person.cs
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace ODataRoutingSample.Models
{
public class Person
{
public string FirstName { get; set; }

public string LastName { get; set; }
}
}

0 comments on commit f5d4b18

Please sign in to comment.