Skip to content

Commit b7e2468

Browse files
committed
Add customers with orders
1 parent c22519b commit b7e2468

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

NorthwindCRUD/Controllers/CustomersController.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ 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+
public ActionResult<CustomerDto[]> GetAllCustomersWithOrders()
52+
{
53+
try
54+
{
55+
var customers = this.customerService.GetAllCustomerOrders();
56+
return Ok(this.mapper.Map<CustomerDb[], CustomerDto[]>(customers));
57+
}
58+
catch (Exception error)
59+
{
60+
logger.LogError(error.Message);
61+
return StatusCode(500);
62+
}
63+
}
64+
4565
/// <summary>
4666
/// Fetches all customers or a page of customers based on the provided parameters.
4767
/// </summary>

NorthwindCRUD/Helpers/MappingProfiles.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ public MappingProfiles()
1616
CreateMap<ShipperDto, ShipperDb>().ReverseMap();
1717
CreateMap<SupplierDto, SupplierDb>().ReverseMap();
1818
CreateMap<TerritoryDto, TerritoryDb>().ReverseMap();
19-
CreateMap<CustomerDto, CustomerDb>().ReverseMap();
20-
CreateMap<OrderDto, OrderDb>().ReverseMap();
19+
CreateMap<CustomerDto, CustomerDb>().ReverseMap()
20+
.ForMember(dest => dest.Orders, opt => opt.MapFrom(src => src.Orders.ToArray()));
21+
CreateMap<OrderDto, OrderDb>().ReverseMap()
22+
.ForMember(dest => dest.OrderDetails, opt => opt.MapFrom(src => src.OrderDetails.ToArray()));
2123
CreateMap<OrderDetailDto, OrderDetailDb>().ReverseMap();
2224
CreateMap<AddressDto, AddressDb>().ReverseMap();
2325
CreateMap<LoginDto, UserDb>().ReverseMap();

NorthwindCRUD/Models/DbModels/OrderDb.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,7 @@ public OrderDb()
4646
public bool Completed { get; set; }
4747

4848
public AddressDb? ShipAddress { get; set; }
49+
50+
public ICollection<OrderDetailDb> OrderDetails { get; set; }
4951
}
5052
}

NorthwindCRUD/Models/Dtos/CustomerDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ public class CustomerDto : ICustomer
1818
public string ContactTitle { get; set; }
1919

2020
public AddressDto Address { get; set; }
21+
22+
public OrderDto[] Orders { get; set; }
2123
}
2224
}

NorthwindCRUD/Models/Dtos/OrderDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ public class OrderDto : IOrder
3636
public bool Completed { get; set; }
3737

3838
public AddressDto ShipAddress { get; set; }
39+
40+
public OrderDetailDto[] OrderDetails { get; set; }
3941
}
4042
}

NorthwindCRUD/Services/CustomerService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public IQueryable<CustomerDb> GetAllAsQueryable()
3232
.FirstOrDefault(c => c.CustomerId == id);
3333
}
3434

35+
public CustomerDb[] GetAllCustomerOrders()
36+
{
37+
return this.dataContext.Customers
38+
.Include(c => c.Orders)
39+
.ThenInclude(o => o.OrderDetails)
40+
.ToArray();
41+
}
42+
3543
public CustomerDb Create(CustomerDb model)
3644
{
3745
var id = IdGenerator.CreateLetterId(6);

0 commit comments

Comments
 (0)