Skip to content

Commit 8a1fc49

Browse files
committed
replace multiple includes
1 parent d4231bc commit 8a1fc49

25 files changed

+296
-79
lines changed

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 6 additions & 4 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,10 +68,10 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
6668
{
6769
try
6870
{
69-
var customer = this.customerService.GetById(id);
70-
if (customer != null)
71+
var orders = this.orderService.GetOrdersByCustomerId(id);
72+
if (orders != null)
7173
{
72-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(customer.Orders.ToArray()));
74+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7375
}
7476

7577
return NotFound();

NorthwindCRUD/Controllers/EmployeesController.cs

Lines changed: 32 additions & 8 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
}
@@ -108,10 +112,10 @@ public ActionResult<OrderDto[]> GetOrdersByEmployeeId(int id)
108112
{
109113
try
110114
{
111-
var employee = this.employeeService.GetById(id);
112-
if (employee != null)
115+
var orders = this.ordersService.GetOrdersByEmployeeId(id);
116+
if (orders != null)
113117
{
114-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(employee.Orders.ToArray()));
118+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
115119
}
116120

117121
return NotFound();
@@ -129,11 +133,9 @@ public ActionResult<EmployeeDto[]> GetTeritoriesByEmployeeId(int id)
129133
{
130134
try
131135
{
132-
var employee = this.employeeService.GetById(id);
133-
if (employee != null)
136+
var teritories = this.employeeTerritoryService.GetTeritoriesByEmployeeId(id);
137+
if (teritories != null)
134138
{
135-
var teritories = employee.EmployeesTerritories.Select(et => et.Territory).ToArray();
136-
137139
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
138140
}
139141

@@ -146,6 +148,28 @@ public ActionResult<EmployeeDto[]> GetTeritoriesByEmployeeId(int id)
146148
}
147149
}
148150

151+
[HttpPost("Teritory")]
152+
[Authorize]
153+
public ActionResult<EmployeeTerritoryDto> AddTerritoryToEmployee(EmployeeTerritoryDto model)
154+
{
155+
try
156+
{
157+
if (ModelState.IsValid)
158+
{
159+
var mappedModel = this.mapper.Map<EmployeeTerritoryDto, EmployeeTerritoryDb>(model);
160+
var employeeTerrirtory = this.employeeTerritoryService.AddTerritoryToEmployee(mappedModel);
161+
return Ok(this.mapper.Map<EmployeeTerritoryDb, EmployeeTerritoryDto>(employeeTerrirtory));
162+
}
163+
164+
return NotFound();
165+
}
166+
catch (Exception error)
167+
{
168+
logger.LogError(error.Message);
169+
return StatusCode(500);
170+
}
171+
}
172+
149173
[HttpPost]
150174
[Authorize]
151175
public ActionResult<EmployeeDto> Create(EmployeeDto model)

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: 8 additions & 2 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
}
@@ -70,7 +72,11 @@ 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+
if (territories != null)
77+
{
78+
return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(territories));
79+
}
7480
}
7581

7682
return NotFound();

NorthwindCRUD/Controllers/ShippersController.cs

Lines changed: 14 additions & 12 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,10 +69,10 @@ public ActionResult<OrderDto[]> GetOrdersByShipperId(int id)
6769
{
6870
try
6971
{
70-
var shipper = this.ShipperService.GetById(id);
71-
if (shipper != null)
72+
var orders = this.orderService.GetOrdersByShipperId(id);
73+
if (orders != null)
7274
{
73-
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(shipper.Orders.ToArray()));
75+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
7476
}
7577

7678
return NotFound();
@@ -91,7 +93,7 @@ public ActionResult<ShipperDto> Create(ShipperDto model)
9193
if (ModelState.IsValid)
9294
{
9395
var mappedModel = this.mapper.Map<ShipperDto, ShipperDb>(model);
94-
var shipper = this.ShipperService.Create(mappedModel);
96+
var shipper = this.shipperService.Create(mappedModel);
9597
return Ok(this.mapper.Map<ShipperDb, ShipperDto>(shipper));
9698
}
9799

@@ -113,7 +115,7 @@ public ActionResult<ShipperDto> Update(ShipperDto model)
113115
if (ModelState.IsValid)
114116
{
115117
var mappedModel = this.mapper.Map<ShipperDto, ShipperDb>(model);
116-
var shipper = this.ShipperService.Update(mappedModel);
118+
var shipper = this.shipperService.Update(mappedModel);
117119

118120
if (shipper != null)
119121
{
@@ -138,7 +140,7 @@ public ActionResult<ShipperDto> Delete(int id)
138140
{
139141
try
140142
{
141-
var shipper = this.ShipperService.Delete(id);
143+
var shipper = this.shipperService.Delete(id);
142144
if (shipper != null)
143145
{
144146
return Ok(this.mapper.Map<ShipperDb, ShipperDto>(shipper));

NorthwindCRUD/Controllers/SuppliersController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
public class SuppliersController : ControllerBase
1414
{
1515
private readonly SupplierService supplierService;
16+
private readonly ProductService productService;
1617
private readonly IMapper mapper;
1718
private readonly ILogger logger;
1819

19-
public SuppliersController(SupplierService SupplierService, IMapper mapper, ILogger logger)
20+
public SuppliersController(SupplierService supplierService, ProductService productService, IMapper mapper, ILogger logger)
2021
{
21-
this.supplierService = SupplierService;
22+
this.supplierService = supplierService;
23+
this.productService = productService;
2224
this.mapper = mapper;
2325
this.logger = logger;
2426
}
@@ -67,10 +69,10 @@ public ActionResult<ProductDto[]> GetProductsBySupplierId(int id)
6769
{
6870
try
6971
{
70-
var supplier = this.supplierService.GetById(id);
71-
if (supplier != null)
72+
var products = this.productService.GetAllBySupplierId(id);
73+
if (products != null)
7274
{
73-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(supplier.Products.ToArray()));
75+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
7476
}
7577

7678
return NotFound();

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public class TerritoriesController : ControllerBase
1414
{
1515
private readonly TerritoryService territoryService;
1616
private readonly RegionService regionService;
17+
private readonly EmployeeTerritoryService employeeTerritoryService;
1718
private readonly IMapper mapper;
1819
private readonly ILogger logger;
1920

20-
public TerritoriesController(TerritoryService territoryService, RegionService regionService, IMapper mapper, ILogger logger)
21+
public TerritoriesController(TerritoryService territoryService, EmployeeTerritoryService employeeTerritoryService, RegionService regionService, IMapper mapper, ILogger logger)
2122
{
2223
this.territoryService = territoryService;
2324
this.regionService = regionService;
25+
this.employeeTerritoryService = employeeTerritoryService;
2426
this.mapper = mapper;
2527
this.logger = logger;
2628
}
@@ -69,12 +71,10 @@ public ActionResult<EmployeeDto[]> GetEmployeesByTerritory(string id)
6971
{
7072
try
7173
{
72-
var territory = this.territoryService.GetById(id);
74+
var employees = this.employeeTerritoryService.GetEmployeesByTerritoryId(id);
7375

74-
if (territory != null)
76+
if (employees != null)
7577
{
76-
var employees = territory.EmployeesTerritories.Select(et => et.Employee).ToArray();
77-
7878
return Ok(this.mapper.Map<EmployeeDb[], EmployeeDto[]>(employees));
7979
}
8080

0 commit comments

Comments
 (0)