Skip to content

Commit

Permalink
update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ElizabethOkerio committed Jul 18, 2023
1 parent d980ef8 commit 572f91c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;

Expand All @@ -28,11 +30,68 @@ public CustomersController(IAsyncEnumerableContext context)
}

[EnableQuery]
[HttpGet("v1/Customers")]
public IAsyncEnumerable<Customer> CustomersData()
{
IAsyncEnumerable<Customer> customers = CreateCollectionAsync<Customer>();

return customers;
}

[EnableQuery]
[HttpGet("odata/Customers")]
public IAsyncEnumerable<Customer> Get()
{
return _context.Customers.AsAsyncEnumerable();
}

public async IAsyncEnumerable<Customer> CreateCollectionAsync<T>()
{
await Task.Delay(100);
// Yield the items one by one asynchronously
yield return new Customer
{
Id = 1,
Name = "Customer1",
Orders = new List<Order> {
new Order {
Name = "Order1",
Price = 25
},
new Order {
Name = "Order2",
Price = 75
}
},
Address = new Address
{
Name = "City1",
Street = "Street1"
}
};
await Task.Delay(100);
yield return new Customer
{
Id = 2,
Name = "Customer2",
Orders = new List<Order> {
new Order {
Name = "Order1",
Price = 35
},
new Order {
Name = "Order2",
Price = 65
}
},
Address = new Address
{
Name = "City2",
Street = "Street2"
}
};
}

public void Generate()
{
for (int i = 1; i <= 3; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public override void ConfigureServices(IServiceCollection services)
IEdmModel edmModel = IAsyncEnumerableEdmModel.GetEdmModel();
services.AddControllers().AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null)
.AddRouteComponents("odata", edmModel));

services.AddControllers().AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null)
.AddRouteComponents("v1", edmModel));
}
}

Expand Down Expand Up @@ -60,5 +63,26 @@ public async Task UsingAsAsyncEnumerableWorks()
List<Customer> customers = JToken.Parse(await response.Content.ReadAsStringAsync())["value"].ToObject<List<Customer>>();
Assert.Equal(3, customers.Count);
}

[Fact]
public async Task UsingAsAsyncEnumerableWorksWithoutEFcORE()
{
// Arrange
string queryUrl = "v1/Customers";
var expectedResult = "{\"@odata.context\":\"http://localhost/v1/$metadata#Customers\",\"value\":[{\"Id\":1,\"Name\":\"Customer1\",\"Address\":{\"Name\":\"City1\",\"Street\":\"Street1\"}},{\"Id\":2,\"Name\":\"Customer2\",\"Address\":{\"Name\":\"City2\",\"Street\":\"Street2\"}}]}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));

// Act
HttpResponseMessage response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var resultObject = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResult, resultObject);

List<Customer> customers = JToken.Parse(await response.Content.ReadAsStringAsync())["value"].ToObject<List<Customer>>();
Assert.Equal(2, customers.Count);
}
}
}

0 comments on commit 572f91c

Please sign in to comment.