Skip to content

Commit 58db5b2

Browse files
Merge pull request #12 from IgniteUI/resolve-endpoints-issues
Resolve endpoints issues
2 parents 367d26e + 1e6adef commit 58db5b2

26 files changed

+397
-122
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ namespace NorthwindCRUD.Controllers
1212
public class CategoriesController : ControllerBase
1313
{
1414
private readonly CategoryService categoryService;
15+
private readonly ProductService productService;
1516
private readonly IMapper mapper;
1617
private readonly ILogger logger;
1718

18-
public CategoriesController(CategoryService categoryService, IMapper mapper, ILogger logger)
19+
public CategoriesController(CategoryService categoryService, ProductService productService, IMapper mapper, ILogger logger)
1920
{
2021
this.categoryService = categoryService;
22+
this.productService = productService;
2123
this.mapper = mapper;
2224
this.logger = logger;
2325
}
2426

2527
[HttpGet]
2628
[Authorize]
27-
public ActionResult<Models.Dtos.CategoryDto[]> GetAll()
29+
public ActionResult<CategoryDto[]> GetAll()
2830
{
2931
try
3032
{
@@ -88,13 +90,8 @@ public ActionResult<ProductDto[]> GetProductsByCategoryId(int id)
8890
{
8991
try
9092
{
91-
var category = this.categoryService.GetById(id);
92-
if (category != null)
93-
{
94-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(category.Products.ToArray()));
95-
}
96-
97-
return NotFound();
93+
var products = this.productService.GetAllByCategoryId(id);
94+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
9895
}
9996
catch (Exception error)
10097
{

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
public class CustomersController : Controller
1313
{
1414
private readonly CustomerService customerService;
15+
private readonly OrderService orderService;
1516
private readonly IMapper mapper;
1617
private readonly ILogger logger;
1718

18-
public CustomersController(CustomerService customerService, IMapper mapper, ILogger logger)
19+
public CustomersController(CustomerService customerService, OrderService orderService, IMapper mapper, ILogger logger)
1920
{
2021
this.customerService = customerService;
22+
this.orderService = orderService;
2123
this.mapper = mapper;
2224
this.logger = logger;
2325
}
@@ -66,13 +68,8 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
6668
{
6769
try
6870
{
69-
var customer = this.customerService.GetById(id);
70-
if (customer != null)
71-
{
72-
return Ok(this.mapper.Map<OrderDb[], OrderDto>(customer.Orders.ToArray()));
73-
}
74-
75-
return NotFound();
71+
var orders = this.orderService.GetOrdersByCustomerId(id);
72+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7673
}
7774
catch (Exception error)
7875
{

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
public class EmployeesController : Controller
1313
{
1414
private readonly EmployeeService employeeService;
15+
private readonly EmployeeTerritoryService employeeTerritoryService;
16+
private readonly OrderService ordersService;
1517
private readonly IMapper mapper;
1618
private readonly ILogger logger;
1719

18-
public EmployeesController(EmployeeService employeeService, IMapper mapper, ILogger logger)
20+
public EmployeesController(EmployeeService employeeService, EmployeeTerritoryService employeeTerritoryService, OrderService ordersService, IMapper mapper, ILogger logger)
1921
{
2022
this.employeeService = employeeService;
23+
this.employeeTerritoryService = employeeTerritoryService;
24+
this.ordersService = ordersService;
2125
this.mapper = mapper;
2226
this.logger = logger;
2327
}
@@ -89,7 +93,7 @@ public ActionResult<EmployeeDto> GetSuperiorById(int id)
8993

9094
[HttpGet("{id}/Subordinates")]
9195
[Authorize]
92-
public ActionResult<ProductDto[]> GetSubordinatesById(int id)
96+
public ActionResult<EmployeeDto[]> GetSubordinatesById(int id)
9397
{
9498
try
9599
{
@@ -108,13 +112,8 @@ public ActionResult<OrderDto[]> GetOrdersByEmployeeId(int id)
108112
{
109113
try
110114
{
111-
var employee = this.employeeService.GetById(id);
112-
if (employee != null)
113-
{
114-
return Ok(this.mapper.Map<OrderDb[], OrderDto>(employee.Orders.ToArray()));
115-
}
116-
117-
return NotFound();
115+
var orders = this.ordersService.GetOrdersByEmployeeId(id);
116+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
118117
}
119118
catch (Exception error)
120119
{
@@ -129,12 +128,27 @@ public ActionResult<EmployeeDto[]> GetTeritoriesByEmployeeId(int id)
129128
{
130129
try
131130
{
132-
var employee = this.employeeService.GetById(id);
133-
if (employee != null)
134-
{
135-
var teritories = employee.EmployeesTerritories.Select(et => et.Territory).ToArray();
131+
var teritories = this.employeeTerritoryService.GetTeritoriesByEmployeeId(id);
132+
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
133+
}
134+
catch (Exception error)
135+
{
136+
logger.LogError(error.Message);
137+
return StatusCode(500);
138+
}
139+
}
136140

137-
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
141+
[HttpPost("Teritory")]
142+
[Authorize]
143+
public ActionResult<EmployeeTerritoryDto> AddTerritoryToEmployee(EmployeeTerritoryDto model)
144+
{
145+
try
146+
{
147+
if (ModelState.IsValid)
148+
{
149+
var mappedModel = this.mapper.Map<EmployeeTerritoryDto, EmployeeTerritoryDb>(model);
150+
var employeeTerrirtory = this.employeeTerritoryService.AddTerritoryToEmployee(mappedModel);
151+
return Ok(this.mapper.Map<EmployeeTerritoryDb, EmployeeTerritoryDto>(employeeTerrirtory));
138152
}
139153

140154
return NotFound();

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
public class OrdersController : ControllerBase
1414
{
1515
private readonly OrderService orderService;
16+
private readonly EmployeeService employeeService;
17+
private readonly CustomerService customerService;
18+
private readonly ShipperService shipperService;
19+
private readonly ProductService productService;
1620
private readonly IMapper mapper;
1721
private readonly ILogger logger;
1822

19-
public OrdersController(OrderService orderService, IMapper mapper, ILogger logger)
23+
public OrdersController(OrderService orderService, EmployeeService employeeService, CustomerService customerService, ShipperService shipperService, ProductService productService, IMapper mapper, ILogger logger)
2024
{
2125
this.orderService = orderService;
26+
this.employeeService = employeeService;
27+
this.customerService = customerService;
28+
this.shipperService = shipperService;
29+
this.productService = productService;
2230
this.mapper = mapper;
2331
this.logger = logger;
2432
}
@@ -91,11 +99,11 @@ public ActionResult<CustomerDto> GetCustomerByOrderId(int id)
9199
var order = this.orderService.GetById(id);
92100
if (order != null)
93101
{
94-
var customer = order.Customer;
102+
var customer = this.customerService.GetById(order.CustomerId);
95103

96104
if (customer != null)
97105
{
98-
return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));
106+
return this.mapper.Map<CustomerDb, CustomerDto>(customer);
99107
}
100108
}
101109

@@ -117,8 +125,7 @@ public ActionResult<CustomerDto> GetEmployeeByOrderId(int id)
117125
var order = this.orderService.GetById(id);
118126
if (order != null)
119127
{
120-
var employee = order.Employee;
121-
128+
var employee = this.employeeService.GetById(order.EmployeeId);
122129
if (employee != null)
123130
{
124131
return Ok(this.mapper.Map<EmployeeDb, EmployeeDto>(employee));
@@ -143,7 +150,7 @@ public ActionResult<CustomerDto> GetShipperByOrderId(int id)
143150
var order = this.orderService.GetById(id);
144151
if (order != null)
145152
{
146-
var shipper = order.Shipper;
153+
var shipper = this.shipperService.GetById(order.ShipVia);
147154

148155
if (shipper != null)
149156
{
@@ -170,9 +177,14 @@ public ActionResult<ProductDto[]> GetProductsByOrderId(int id)
170177
var order = this.orderService.GetById(id);
171178
if (order != null)
172179
{
173-
var products = order.Details.Select(o => o.Product).ToArray();
180+
var productIds = order.Details.Select(o => o.ProductId).ToArray();
181+
var products = this.productService.GetProductsByIds(productIds);
174182

175-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
183+
if (products != null)
184+
{
185+
var productDtos = this.mapper.Map<ProductDb[], ProductDto[]>(products);
186+
return Ok(productDtos);
187+
}
176188
}
177189

178190
return NotFound();

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ namespace NorthwindCRUD.Controllers
1313
public class ProductsController : ControllerBase
1414
{
1515
private readonly ProductService productService;
16+
private readonly CategoryService categoryService;
17+
private readonly OrderService orderService;
18+
private readonly SupplierService supplierService;
1619
private readonly IMapper mapper;
1720
private readonly ILogger logger;
1821

19-
public ProductsController(ProductService productService, IMapper mapper, ILogger logger)
22+
public ProductsController(ProductService productService, CategoryService categoryService, OrderService orderService, SupplierService supplierService, IMapper mapper, ILogger logger)
2023
{
2124
this.productService = productService;
25+
this.categoryService = categoryService;
26+
this.orderService = orderService;
27+
this.supplierService = supplierService;
2228
this.mapper = mapper;
2329
this.logger = logger;
2430
}
@@ -70,7 +76,7 @@ public ActionResult<CategoryDto> GetCategoryByProductId(int id)
7076
var product = this.productService.GetById(id);
7177
if (product != null)
7278
{
73-
var category = product.Category;
79+
var category = this.categoryService.GetById(product.CategoryId);
7480

7581
if (category != null)
7682
{
@@ -96,7 +102,8 @@ public ActionResult<OrderDetailDto[]> GetOrderDetailsByProductId(int id)
96102
var product = this.productService.GetById(id);
97103
if (product != null)
98104
{
99-
return Ok(this.mapper.Map<OrderDetailDb[], OrderDetailDto>(product.Details.ToArray()));
105+
var orderDetails = this.orderService.GetOrderDetailsByProductId(id);
106+
return Ok(this.mapper.Map<OrderDetailDb[], OrderDetailDto[]>(orderDetails));
100107
}
101108

102109
return NotFound();
@@ -117,7 +124,7 @@ public ActionResult<SupplierDto> GetSupplierByProductId(int id)
117124
var product = this.productService.GetById(id);
118125
if (product != null)
119126
{
120-
var supplier = product.Supplier;
127+
var supplier = this.supplierService.GetById(product.SupplierId);
121128

122129
if (supplier != null)
123130
{

NorthwindCRUD/Controllers/RegionsController.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
public class RegionsController : ControllerBase
1414
{
1515
private readonly RegionService regionService;
16+
private readonly TerritoryService territoryService;
1617
private readonly IMapper mapper;
1718
private readonly ILogger logger;
1819

19-
public RegionsController(RegionService regionService, IMapper mapper, ILogger logger)
20+
public RegionsController(RegionService regionService, TerritoryService territoryService, IMapper mapper, ILogger logger)
2021
{
2122
this.regionService = regionService;
23+
this.territoryService = territoryService;
2224
this.mapper = mapper;
2325
this.logger = logger;
2426
}
@@ -61,7 +63,7 @@ public ActionResult<RegionDto> GetById(int id)
6163
}
6264
}
6365

64-
[HttpGet("{id}/Territory")]
66+
[HttpGet("{id}/Territories")]
6567
[Authorize]
6668
public ActionResult<CustomerDto> GetTerritoryByRegionId(int id)
6769
{
@@ -70,7 +72,8 @@ public ActionResult<CustomerDto> GetTerritoryByRegionId(int id)
7072
var region = this.regionService.GetById(id);
7173
if (region != null)
7274
{
73-
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(region.Territories.ToArray()));
75+
var territories = this.territoryService.GetTerritoriesByRegionId(id);
76+
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(territories));
7477
}
7578

7679
return NotFound();

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
[Route("[controller]")]
1313
public class ShippersController : ControllerBase
1414
{
15-
private readonly ShipperService ShipperService;
15+
private readonly ShipperService shipperService;
16+
private readonly OrderService orderService;
1617
private readonly IMapper mapper;
1718
private readonly ILogger logger;
1819

19-
public ShippersController(ShipperService ShipperService, IMapper mapper, ILogger logger)
20+
public ShippersController(ShipperService shipperService, OrderService orderService, IMapper mapper, ILogger logger)
2021
{
21-
this.ShipperService = ShipperService;
22+
this.shipperService = shipperService;
23+
this.orderService = orderService;
2224
this.mapper = mapper;
2325
this.logger = logger;
2426
}
@@ -29,8 +31,8 @@ public ActionResult<ShipperDto[]> GetAll()
2931
{
3032
try
3133
{
32-
var Shippers = this.ShipperService.GetAll();
33-
return Ok(this.mapper.Map<ShipperDb[], ShipperDto[]>(Shippers));
34+
var shippers = this.shipperService.GetAll();
35+
return Ok(this.mapper.Map<ShipperDb[], ShipperDto[]>(shippers));
3436
}
3537
catch (Exception error)
3638
{
@@ -46,7 +48,7 @@ public ActionResult<ShipperDto> GetById(int id)
4648
{
4749
try
4850
{
49-
var category = this.ShipperService.GetById(id);
51+
var category = this.shipperService.GetById(id);
5052
if (category != null)
5153
{
5254
return Ok(this.mapper.Map<ShipperDb, ShipperDto>(category));
@@ -67,13 +69,8 @@ public ActionResult<OrderDto[]> GetOrdersByShipperId(int id)
6769
{
6870
try
6971
{
70-
var shipper = this.ShipperService.GetById(id);
71-
if (shipper != null)
72-
{
73-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(shipper.Orders.ToArray()));
74-
}
75-
76-
return NotFound();
72+
var orders = this.orderService.GetOrdersByShipperId(id);
73+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7774
}
7875
catch (Exception error)
7976
{
@@ -91,7 +88,7 @@ public ActionResult<ShipperDto> Create(ShipperDto model)
9188
if (ModelState.IsValid)
9289
{
9390
var mappedModel = this.mapper.Map<ShipperDto, ShipperDb>(model);
94-
var shipper = this.ShipperService.Create(mappedModel);
91+
var shipper = this.shipperService.Create(mappedModel);
9592
return Ok(this.mapper.Map<ShipperDb, ShipperDto>(shipper));
9693
}
9794

@@ -113,7 +110,7 @@ public ActionResult<ShipperDto> Update(ShipperDto model)
113110
if (ModelState.IsValid)
114111
{
115112
var mappedModel = this.mapper.Map<ShipperDto, ShipperDb>(model);
116-
var shipper = this.ShipperService.Update(mappedModel);
113+
var shipper = this.shipperService.Update(mappedModel);
117114

118115
if (shipper != null)
119116
{
@@ -138,7 +135,7 @@ public ActionResult<ShipperDto> Delete(int id)
138135
{
139136
try
140137
{
141-
var shipper = this.ShipperService.Delete(id);
138+
var shipper = this.shipperService.Delete(id);
142139
if (shipper != null)
143140
{
144141
return Ok(this.mapper.Map<ShipperDb, ShipperDto>(shipper));

0 commit comments

Comments
 (0)