Skip to content

Commit 7b90ac1

Browse files
committed
Implement retrieval of orders with details by customer ID
1 parent 10744b7 commit 7b90ac1

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,6 @@ public ActionResult<CustomerDto[]> GetAll()
4242
}
4343
}
4444

45-
/// <summary>
46-
/// Retrieves all customers along with their associated orders.
47-
/// </summary>
48-
/// <returns>
49-
/// An <see cref="ActionResult{T}"/> containing an array of <see cref="CustomerDto"/> objects.
50-
/// </returns>
51-
[HttpGet("WithOrders")]
52-
public ActionResult<CustomerDto[]> GetAllCustomersWithOrders()
53-
{
54-
try
55-
{
56-
var customers = this.customerService.GetAllCustomersWithOrders();
57-
return Ok(this.mapper.Map<CustomerDb[], CustomerDto[]>(customers));
58-
}
59-
catch (Exception error)
60-
{
61-
logger.LogError(error.Message);
62-
return StatusCode(500);
63-
}
64-
}
65-
6645
/// <summary>
6746
/// Fetches all customers or a page of customers based on the provided parameters.
6847
/// </summary>
@@ -142,6 +121,21 @@ public ActionResult<CountResultDto> GetCustomersCount()
142121
}
143122
}
144123

124+
[HttpGet("WithOrders")]
125+
public ActionResult<CustomerDto[]> GetAllCustomersWithOrders()
126+
{
127+
try
128+
{
129+
var customers = this.customerService.GetAllCustomersWithOrders();
130+
return Ok(this.mapper.Map<CustomerDb[], CustomerDto[]>(customers));
131+
}
132+
catch (Exception error)
133+
{
134+
logger.LogError(error.Message);
135+
return StatusCode(500);
136+
}
137+
}
138+
145139
[HttpGet("{id}")]
146140
public ActionResult<CustomerDto> GetById(string id)
147141
{
@@ -178,6 +172,21 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
178172
}
179173
}
180174

175+
[HttpGet("{id}/Orders/WithDetails")]
176+
public ActionResult<OrderDto[]> GetOrdersAndOrderDetailsByCustomerId(string id)
177+
{
178+
try
179+
{
180+
var orders = this.orderService.GetOrdersWithDetailsByCustomerId(id);
181+
return Ok(this.mapper.Map<OrderDb[], OrderDto[]>(orders));
182+
}
183+
catch (Exception error)
184+
{
185+
logger.LogError(error.Message);
186+
return StatusCode(500);
187+
}
188+
}
189+
181190
[HttpPost]
182191
[Authorize]
183192
public ActionResult<CustomerDto> Create(CustomerDto model)

NorthwindCRUD/Services/OrderService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public OrderDb[] GetOrdersByCustomerId(string id)
5454
.ToArray();
5555
}
5656

57+
public OrderDb[] GetOrdersWithDetailsByCustomerId(string id)
58+
{
59+
return GetOrdersWithDetailsQuery()
60+
.Where(o => o.CustomerId == id)
61+
.ToArray();
62+
}
63+
5764
public OrderDb[] GetOrdersByEmployeeId(int id)
5865
{
5966
return GetOrdersQuery()
@@ -192,5 +199,12 @@ private IQueryable<OrderDb> GetOrdersQuery()
192199
return this.dataContext.Orders
193200
.Include(c => c.ShipAddress);
194201
}
202+
203+
private IQueryable<OrderDb> GetOrdersWithDetailsQuery()
204+
{
205+
return this.dataContext.Orders
206+
.Include(c => c.OrderDetails)
207+
.Include(c => c.ShipAddress);
208+
}
195209
}
196210
}

0 commit comments

Comments
 (0)