diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index 336e07d..69ced0d 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,8 @@ using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Migrations; using System; using System.Collections.Generic; using System.Linq; @@ -38,13 +40,17 @@ namespace ElectronicsShopDataBaseImplement.Implements public List GetFilteredList(OrderSearchModel model) { - if (!model.DateFrom.HasValue) + if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null)) { return new(); } using var context = new Database(); - //Придумать решение - return context.Orders.Where(x => x.Status.Contains(model.ID).Select(x => x.GetViewModel)).ToList(); + return context.Orders + .Include(x => x.Products) + .ThenInclude(x => x._product) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() @@ -55,12 +61,12 @@ namespace ElectronicsShopDataBaseImplement.Implements public OrderViewModel? Insert(OrderBindingModel model) { - var newComponent = Order.Create(model); + using var context = new Database(); + var newComponent = Order.Create(context,model); if (newComponent == null) { return null; } - using var context = new Database(); context.Orders.Add(newComponent); context.SaveChanges(); return newComponent.GetViewModel; diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs index edd78f0..f0e7733 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs @@ -5,6 +5,7 @@ using ElectronicsShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; using System.Linq; using System.Text; @@ -24,11 +25,24 @@ namespace ElectronicsShopDataBaseImplement.Models [Required] public DateTime DateCreate { get; set; }= DateTime.Now; [Required] - public DateTime? DateImplemet { get; set; } = null; + public DateTime? DateImplement { get; set; } [Required] public Dictionary ProductList { get; set; } = new(); - public static Order? Create(OrderBindingModel? model) + [NotMapped] + public Dictionary _productList + { + get { + if (ProductList == null) { + ProductList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count)); + } + return _productList; + } + } + + public virtual List Products { get; set; } = new(); + + public static Order? Create(Database context ,OrderBindingModel? model) { if (model == null) { @@ -41,19 +55,11 @@ namespace ElectronicsShopDataBaseImplement.Models Status = model.Status, PaymeantOption = model.PaymeantOption, DateCreate = model.DateCreate, - //todo ProductList - }; - } - public static Order? Create(OrderViewModel model) - { - return new Order - { - ID = model.ID, - Sum = model.Sum, - Status = model.Status, - PaymeantOption = model.PaymeantOption, - DateCreate = model.DateCreate, - //todo ProductList + Products = model.ProductList.Select(x => new OrderProducts + { + _product = context.Products.First(y => y.ID == x.Key), + Count = x.Value.Item2 + }).ToList() }; } public void Update(OrderBindingModel model) @@ -67,9 +73,9 @@ namespace ElectronicsShopDataBaseImplement.Models PaymeantOption = model.PaymeantOption; Status = model.Status; DateCreate = model.DateCreate; - if (model.DateImplemet != null) + if (model.DateImplement != null) { - DateImplemet = model.DateImplemet; + DateImplement = model.DateImplement; } //todo ProductList } @@ -81,7 +87,7 @@ namespace ElectronicsShopDataBaseImplement.Models PaymeantOption = PaymeantOption, Status = Status, DateCreate = DateCreate, - DateImplemet=DateImplemet, + DateImplement=DateImplement, ProductList = ProductList }; } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProducts.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProducts.cs new file mode 100644 index 0000000..11a056e --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProducts.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Models +{ + public class OrderProducts + { + public int ID { get; set; } + + [Required] + public int OrdersID { get; set; } + + [Required] + public int ProductID { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Order _order { get; set; } = new(); + public virtual Product _product { get; set; } = new(); + } +}