Skip to content

Commit cb3d526

Browse files
committed
fix caterories and customers endpoints
1 parent fb2c83f commit cb3d526

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ 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
}
@@ -88,10 +90,10 @@ public ActionResult<ProductDto[]> GetProductsByCategoryId(int id)
8890
{
8991
try
9092
{
91-
var category = this.categoryService.GetById(id);
92-
if (category != null)
93+
var products = this.productService.GetAllByCategoryId(id);
94+
if (products != null)
9395
{
94-
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(category.Products.ToArray()));
96+
return Ok(this.mapper.Map<ProductDb[], ProductDto[]>(products));
9597
}
9698

9799
return NotFound();

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
6969
var customer = this.customerService.GetById(id);
7070
if (customer != null)
7171
{
72-
return Ok(this.mapper.Map<OrderDb[], OrderDto>(customer.Orders.ToArray()));
72+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(customer.Orders.ToArray()));
7373
}
7474

7575
return NotFound();

NorthwindCRUD/Services/CustomerService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public CustomerDb GetById(string id)
2525
{
2626
return this.dataContext.Customers
2727
.Include(c => c.Address)
28+
.Include(c => c.Orders.Where(o => o.CustomerId == id))
2829
.FirstOrDefault(c => c.CustomerId == id);
2930
}
3031

NorthwindCRUD/Services/ProductService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public ProductDb[] GetAll()
2222
return this.dataContext.Products.ToArray();
2323
}
2424

25+
public ProductDb[] GetAllByCategoryId(int id)
26+
{
27+
return this.dataContext.Products.Where(p => p.CategoryId == id).ToArray();
28+
}
29+
2530
public ProductDb GetById(int id)
2631
{
2732
return this.dataContext.Products.FirstOrDefault(p => p.ProductId == id);

0 commit comments

Comments
 (0)