diff --git a/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs b/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs index 525a54b..7c3cb80 100644 --- a/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs +++ b/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs @@ -18,7 +18,8 @@ namespace JewelryStoreDatabaseImplement.Implements { using var context = new JewelryStoreDataBase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.Include(x => x.Jewel) + .Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { @@ -40,26 +41,47 @@ namespace JewelryStoreDatabaseImplement.Implements using var context = new JewelryStoreDataBase(); - return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders.Include(x => x.Jewel) + .Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (!model.DateFrom.HasValue || ! model.DateTo.HasValue) + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue) { return new(); } - using var context = new JewelryStoreDataBase(); - - return context.Orders.Include(x => x.Jewel).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); + if (model.DateFrom.HasValue) + { + return context.Orders + .Include(x => x.Jewel) + .Include(x => x.Client) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ClientId.HasValue) + return context.Orders + .Include(x => x.Jewel) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + return context.Orders + .Include(x => x.Jewel) + .Include(x => x.Client) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { using var context = new JewelryStoreDataBase(); - return context.Orders.Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Jewel) + .Include(x => x.Client).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -83,8 +105,8 @@ namespace JewelryStoreDatabaseImplement.Implements { using var context = new JewelryStoreDataBase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); - + var order = context.Orders.Include(x => x.Jewel) + .Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; diff --git a/JewelryStoreDatabaseImplement/Models/Order.cs b/JewelryStoreDatabaseImplement/Models/Order.cs index 0800ca7..2e480c0 100644 --- a/JewelryStoreDatabaseImplement/Models/Order.cs +++ b/JewelryStoreDatabaseImplement/Models/Order.cs @@ -18,6 +18,9 @@ namespace JewelryStoreDatabaseImplement.Models public int JewelId { get; private set; } + [Required] + public int ClientId { get; set; } + public string JewelName { get; private set; } = string.Empty; [Required] @@ -35,6 +38,7 @@ namespace JewelryStoreDatabaseImplement.Models public DateTime? DateImplement { get; private set; } public virtual Jewel Jewel { get; set; } + public Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -47,6 +51,7 @@ namespace JewelryStoreDatabaseImplement.Models { Id = model.Id, JewelId = model.JewelId, + ClientId = model.ClientId, JewelName = model.JewelName, Count = model.Count, Sum = model.Sum, @@ -76,6 +81,7 @@ namespace JewelryStoreDatabaseImplement.Models { Id = Id, JewelId = JewelId, + ClientId = ClientId, JewelName = JewelName, Count = Count, Sum = Sum,