Skip to content

Commit

Permalink
Merge pull request #44 from IgniteUI/vkombov/customers-with-orders
Browse files Browse the repository at this point in the history
Add customers with orders
  • Loading branch information
ddidgdimitrov-infragistics authored Jan 29, 2025
2 parents f6d1a80 + 239dc22 commit 0d72aa7
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 23 deletions.
30 changes: 30 additions & 0 deletions NorthwindCRUD/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public ActionResult<CountResultDto> GetCustomersCount()
}
}

[HttpGet("WithOrders")]
public ActionResult<CustomerWithOrdersDto[]> GetAllCustomersWithOrders()
{
try
{
var customers = this.customerService.GetAllCustomersWithOrders();
return Ok(this.mapper.Map<CustomerDb[], CustomerWithOrdersDto[]>(customers));
}
catch (Exception error)
{
logger.LogError(error.Message);
return StatusCode(500);
}
}

[HttpGet("{id}")]
public ActionResult<CustomerDto> GetById(string id)
{
Expand Down Expand Up @@ -156,6 +171,21 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
}
}

[HttpGet("{id}/Orders/WithDetails")]
public ActionResult<OrderWithDetailsDto[]> GetOrdersAndOrderDetailsByCustomerId(string id)
{
try
{
var orders = this.orderService.GetOrdersWithDetailsByCustomerId(id);
return Ok(this.mapper.Map<OrderDb[], OrderWithDetailsDto[]>(orders));
}
catch (Exception error)
{
logger.LogError(error.Message);
return StatusCode(500);
}
}

[HttpPost]
[Authorize]
public ActionResult<CustomerDto> Create(CustomerDto model)
Expand Down
2 changes: 1 addition & 1 deletion NorthwindCRUD/Controllers/RegionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public ActionResult<RegionDto> GetById(int id)
}

[HttpGet("{id}/Territories")]
public ActionResult<CustomerDto> GetTerritoryByRegionId(int id)
public ActionResult<TerritoryDto[]> GetTerritoryByRegionId(int id)
{
try
{
Expand Down
2 changes: 2 additions & 0 deletions NorthwindCRUD/Helpers/MappingProfiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public MappingProfiles()
CreateMap<TerritoryDto, TerritoryDb>().ReverseMap();
CreateMap<CustomerDto, CustomerDb>().ReverseMap();
CreateMap<OrderDto, OrderDb>().ReverseMap();
CreateMap<CustomerWithOrdersDto, CustomerDb>().ReverseMap();
CreateMap<OrderWithDetailsDto, OrderDb>().ReverseMap();
CreateMap<OrderDetailDto, OrderDetailDb>().ReverseMap();
CreateMap<AddressDto, AddressDb>().ReverseMap();
CreateMap<LoginDto, UserDb>().ReverseMap();
Expand Down
2 changes: 2 additions & 0 deletions NorthwindCRUD/Models/DbModels/OrderDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ public OrderDb()
public bool Completed { get; set; }

public AddressDb? ShipAddress { get; set; }

public ICollection<OrderDetailDb> OrderDetails { get; set; }
}
}
2 changes: 2 additions & 0 deletions NorthwindCRUD/Models/DbModels/ProductDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ProductDb : IProduct
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ProductId { get; set; }

public string Productname { get; set; }

public int? SupplierId { get; set; }

public SupplierDb? Supplier { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions NorthwindCRUD/Models/Dtos/CustomerWithOrdersDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NorthwindCRUD.Models.Contracts;

namespace NorthwindCRUD.Models.Dtos
{
public class CustomerWithOrdersDto : CustomerDto, ICustomer
{
public OrderWithDetailsDto[] Orders { get; set; }
}
}
9 changes: 9 additions & 0 deletions NorthwindCRUD/Models/Dtos/OrderWithDetailsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NorthwindCRUD.Models.Contracts;

namespace NorthwindCRUD.Models.Dtos
{
public class OrderWithDetailsDto : OrderDto, IOrder
{
public OrderDetailDto[] OrderDetails { get; set; }
}
}
2 changes: 2 additions & 0 deletions NorthwindCRUD/Models/Dtos/ProductDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class ProductDto : IProduct
{
public int ProductId { get; set; }

public string ProductName { get; set; }

public int? SupplierId { get; set; }

public int? CategoryId { get; set; }
Expand Down
16 changes: 10 additions & 6 deletions NorthwindCRUD/Providers/DbContextConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public void ConfigureOptions(DbContextOptionsBuilder options)
var connectionString = this.GetSqlLiteConnectionString(tenantId);
connection = new SqliteConnection(connectionString);
memoryCache.Set(cacheKey, connection, GetCacheConnectionEntryOptions());
}

// For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
// see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
connection.Open();
// For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
// see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
connection.Open();

options.UseSqlite(connection).EnableSensitiveDataLogging();
options.UseSqlite(connection).EnableSensitiveDataLogging();

SeedDb(options);
SeedDb(options);
}
else
{
options.UseSqlite(connection).EnableSensitiveDataLogging();
}
}
}

Expand Down
32 changes: 16 additions & 16 deletions NorthwindCRUD/Resources/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
},
{
"productId": 22,
"productName": "Gustaf's Kn�ckebr�d",
"productName": "Gustaf's Knäckebröd",
"supplierId": 9,
"categoryId": 5,
"quantityPerUnit": "24 - 500 g pkgs.",
Expand All @@ -265,7 +265,7 @@
},
{
"productId": 23,
"productName": "Tunnbr�d",
"productName": "Tunnbröd",
"supplierId": 9,
"categoryId": 5,
"quantityPerUnit": "12 - 250 g pkgs.",
Expand All @@ -277,7 +277,7 @@
},
{
"productId": 24,
"productName": "Guaran� Fant�stica",
"productName": "Guaraná Fantástica",
"supplierId": 10,
"categoryId": 1,
"quantityPerUnit": "12 - 355 ml cans",
Expand All @@ -289,7 +289,7 @@
},
{
"productId": 25,
"productName": "NuNuCa Nu�-Nougat-Creme",
"productName": "NuNuCa Nuß-Nougat-Creme",
"supplierId": 11,
"categoryId": 3,
"quantityPerUnit": "20 - 450 g glasses",
Expand All @@ -301,7 +301,7 @@
},
{
"productId": 26,
"productName": "Gumb�r Gummib�rchen",
"productName": "Gumbär Gummibärchen",
"supplierId": 11,
"categoryId": 3,
"quantityPerUnit": "100 - 250 g bags",
Expand All @@ -325,7 +325,7 @@
},
{
"productId": 28,
"productName": "R�ssle Sauerkraut",
"productName": "Rössle Sauerkraut",
"supplierId": 12,
"categoryId": 7,
"quantityPerUnit": "25 - 825 g cans",
Expand All @@ -337,7 +337,7 @@
},
{
"productId": 29,
"productName": "Th�ringer Rostbratwurst",
"productName": "Thüringer Rostbratwurst",
"supplierId": 12,
"categoryId": 6,
"quantityPerUnit": "50 bags x 30 sausgs.",
Expand Down Expand Up @@ -445,7 +445,7 @@
},
{
"productId": 38,
"productName": "C�te de Blaye",
"productName": "Côte de Blaye",
"supplierId": 18,
"categoryId": 1,
"quantityPerUnit": "12 - 75 cl bottles",
Expand Down Expand Up @@ -637,7 +637,7 @@
},
{
"productId": 54,
"productName": "Tourti�re",
"productName": "Tourtière",
"supplierId": 25,
"categoryId": 6,
"quantityPerUnit": "16 pies",
Expand All @@ -649,7 +649,7 @@
},
{
"productId": 55,
"productName": "P�t� chinois",
"productName": "Pâté chinois",
"supplierId": 25,
"categoryId": 6,
"quantityPerUnit": "24 boxes x 2 pies",
Expand Down Expand Up @@ -721,7 +721,7 @@
},
{
"productId": 61,
"productName": "Sirop d'�rable",
"productName": "Sirop d'érable",
"supplierId": 29,
"categoryId": 2,
"quantityPerUnit": "24 - 500 ml bottles",
Expand Down Expand Up @@ -757,7 +757,7 @@
},
{
"productId": 64,
"productName": "Wimmers gute Semmelkn�del",
"productName": "Wimmers gute Semmelknödel",
"supplierId": 12,
"categoryId": 5,
"quantityPerUnit": "20 bags x 4 pieces",
Expand Down Expand Up @@ -865,7 +865,7 @@
},
{
"productId": 73,
"productName": "R�d Kaviar",
"productName": "Röd Kaviar",
"supplierId": 17,
"categoryId": 8,
"quantityPerUnit": "24 - 150 g jars",
Expand All @@ -889,7 +889,7 @@
},
{
"productId": 75,
"productName": "Rh�nbr�u Klosterbier",
"productName": "Rhönbräu Klosterbier",
"supplierId": 12,
"categoryId": 1,
"quantityPerUnit": "24 - 0.5 l bottles",
Expand All @@ -901,7 +901,7 @@
},
{
"productId": 76,
"productName": "Lakkalik��ri",
"productName": "Lakkalikööri",
"supplierId": 23,
"categoryId": 1,
"quantityPerUnit": "500 ml",
Expand All @@ -913,7 +913,7 @@
},
{
"productId": 77,
"productName": "Original Frankfurter gr�ne So�e",
"productName": "Original Frankfurter grüne Soße",
"supplierId": 12,
"categoryId": 2,
"quantityPerUnit": "12 boxes",
Expand Down
10 changes: 10 additions & 0 deletions NorthwindCRUD/Services/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public IQueryable<CustomerDb> GetAllAsQueryable()
.FirstOrDefault(c => c.CustomerId == id);
}

public CustomerDb[] GetAllCustomersWithOrders()
{
return this.dataContext.Customers
.Include(c => c.Address)
.Include(c => c.Orders)
.ThenInclude(o => o.OrderDetails)
.AsNoTracking()
.ToArray();
}

public CustomerDb Create(CustomerDb model)
{
var id = IdGenerator.CreateLetterId(6);
Expand Down
15 changes: 15 additions & 0 deletions NorthwindCRUD/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public OrderDb[] GetOrdersByCustomerId(string id)
.ToArray();
}

public OrderDb[] GetOrdersWithDetailsByCustomerId(string id)
{
return GetOrdersWithDetailsQuery()
.Where(o => o.CustomerId == id)
.AsNoTracking()
.ToArray();
}

public OrderDb[] GetOrdersByEmployeeId(int id)
{
return GetOrdersQuery()
Expand Down Expand Up @@ -192,5 +200,12 @@ private IQueryable<OrderDb> GetOrdersQuery()
return this.dataContext.Orders
.Include(c => c.ShipAddress);
}

private IQueryable<OrderDb> GetOrdersWithDetailsQuery()
{
return this.dataContext.Orders
.Include(c => c.OrderDetails)
.Include(c => c.ShipAddress);
}
}
}

0 comments on commit 0d72aa7

Please sign in to comment.