diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index ba47328..2d8105c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -17,7 +17,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { using var context = new BlacksmithWorkshopDataBase(); return context.Orders - .Select(x => AccessManufactureStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -29,7 +29,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDataBase(); return context.Orders .Where(x => x.Id == model.Id) - .Select(x => AccessManufactureStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -39,7 +39,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return null; } using var context = new BlacksmithWorkshopDataBase(); - return AccessManufactureStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { @@ -51,7 +51,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return AccessManufactureStorage(newOrder.GetViewModel); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -64,7 +64,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessManufactureStorage(order.GetViewModel); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -75,25 +75,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return AccessManufactureStorage(element.GetViewModel); + return element.GetViewModel; } return null; } - - public static OrderViewModel AccessManufactureStorage(OrderViewModel model) - { - if (model == null) - return null; - using var context = new BlacksmithWorkshopDataBase(); - foreach (var manufacture in context.Manufactures) - { - if (manufacture.Id == model.ManufactureId) - { - model.ManufactureName = manufacture.ManufactureName; - break; - } - } - return model; - } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs index 0e51989..bfa8c1e 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs @@ -25,13 +25,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int ManufactureId { get; private set; } + public virtual Manufacture Manufacture { get; set; } = new(); - public static Order? Create(OrderBindingModel model) + public static Order? Create(BlacksmithWorkshopDataBase context, OrderBindingModel model) { - if (model == null) - { - return null; - } return new Order() { Id = model.Id, @@ -41,6 +38,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, ManufactureId = model.ManufactureId, + Manufacture = context.Manufactures.First(y => y.Id == model.ManufactureId) }; } public void Update(OrderBindingModel? model) @@ -55,6 +53,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models public OrderViewModel GetViewModel => new() { ManufactureId = ManufactureId, + ManufactureName = Manufacture.ManufactureName, Count = Count, Sum = Sum, Status = Status,