-
Notifications
You must be signed in to change notification settings - Fork 155
/
EntitySetAggregationController.cs
105 lines (94 loc) · 3.34 KB
/
EntitySetAggregationController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//-----------------------------------------------------------------------------
// <copyright file="EntitySetAggregationController.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 System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
namespace Microsoft.AspNetCore.OData.E2E.Tests.EntitySetAggregation
{
public class CustomersController : ODataController
{
private readonly EntitySetAggregationContext _context;
public CustomersController(EntitySetAggregationContext context)
{
context.Database.EnsureCreated();
_context = context;
if (!_context.Customers.Any())
{
Generate();
}
}
[EnableQuery]
public IQueryable<Customer> Get()
{
return _context.Customers;
}
[EnableQuery]
public SingleResult<Customer> Get(int key)
{
return SingleResult.Create(_context.Customers.Where(c => c.Id == key));
}
public void Generate()
{
for (int i = 1; i <= 3; i++)
{
var customer = new Customer
{
// Id = i,
Name = "Customer" + (i+1) % 2,
Orders =
new List<Order> {
new Order {
Name = "Order" + 2*i,
Price = i * 25,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 25 }
},
new Order {
Name = "Order" + 2*i+1,
Price = i * 75,
SaleInfo = new SaleInfo { Quantity = i, UnitPrice = 75 }
}
},
Address = new Address
{
Name = "City" + i % 2,
Street = "Street" + i % 2,
}
};
_context.Customers.Add(customer);
}
_context.SaveChanges();
}
}
public class EmployeesController : ODataController
{
private static readonly List<Employee> employees = new List<Employee>
{
new Employee
{
Id = 1,
NextOfKin = new NextOfKin { Name = "NoK 1", PhysicalAddress = new Location { City = "Redmond" } }
},
new Employee
{
Id = 2,
NextOfKin = new NextOfKin { Name = "NoK 2", PhysicalAddress = new Location { City = "Nairobi" } }
},
new Employee
{
Id = 3,
NextOfKin = new NextOfKin { Name = "NoK 3", PhysicalAddress = new Location { City = "Redmond" } }
}
};
[EnableQuery]
public IQueryable<Employee> Get()
{
return employees.AsQueryable();
}
}
}