diff --git a/MotorPlant/MotorPlant.sln b/MotorPlant/MotorPlant.sln index 50ee9ea..b12bd0e 100644 --- a/MotorPlant/MotorPlant.sln +++ b/MotorPlant/MotorPlant.sln @@ -5,6 +5,14 @@ VisualStudioVersion = 17.7.34031.279 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantView", "MotorPlantView\MotorPlantView.csproj", "{254164C1-CDF8-4F3C-8564-8C8987B7BFA1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantDataModels", "MotorPlantDataModels\MotorPlantDataModels.csproj", "{F9D3BBA7-D038-4F69-911E-0A25407E4716}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantContracts", "MotorPlantContracts\MotorPlantContracts.csproj", "{B2784391-936D-471E-BB99-215CB345B1CD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantListImplement", "MotorPlantListImplement\MotorPlantListImplement.csproj", "{0694ECF1-AEF8-4033-A0BF-EAA22A733F31}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantBusinessLogic", "MotorPlantBusinessLogic\MotorPlantBusinessLogic.csproj", "{75003AA5-9D46-45B6-9EF0-BAF16E2790EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +23,22 @@ Global {254164C1-CDF8-4F3C-8564-8C8987B7BFA1}.Debug|Any CPU.Build.0 = Debug|Any CPU {254164C1-CDF8-4F3C-8564-8C8987B7BFA1}.Release|Any CPU.ActiveCfg = Release|Any CPU {254164C1-CDF8-4F3C-8564-8C8987B7BFA1}.Release|Any CPU.Build.0 = Release|Any CPU + {F9D3BBA7-D038-4F69-911E-0A25407E4716}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9D3BBA7-D038-4F69-911E-0A25407E4716}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9D3BBA7-D038-4F69-911E-0A25407E4716}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9D3BBA7-D038-4F69-911E-0A25407E4716}.Release|Any CPU.Build.0 = Release|Any CPU + {B2784391-936D-471E-BB99-215CB345B1CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B2784391-936D-471E-BB99-215CB345B1CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B2784391-936D-471E-BB99-215CB345B1CD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B2784391-936D-471E-BB99-215CB345B1CD}.Release|Any CPU.Build.0 = Release|Any CPU + {0694ECF1-AEF8-4033-A0BF-EAA22A733F31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0694ECF1-AEF8-4033-A0BF-EAA22A733F31}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0694ECF1-AEF8-4033-A0BF-EAA22A733F31}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0694ECF1-AEF8-4033-A0BF-EAA22A733F31}.Release|Any CPU.Build.0 = Release|Any CPU + {75003AA5-9D46-45B6-9EF0-BAF16E2790EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {75003AA5-9D46-45B6-9EF0-BAF16E2790EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {75003AA5-9D46-45B6-9EF0-BAF16E2790EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {75003AA5-9D46-45B6-9EF0-BAF16E2790EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/ComponentLogic.cs b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..ffef92d --- /dev/null +++ b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/ComponentLogic.cs @@ -0,0 +1,109 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace MotorPlantBusinessLogic.BusinessLogics +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", + nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}.Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/EngineLogic.cs b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/EngineLogic.cs new file mode 100644 index 0000000..b1335bd --- /dev/null +++ b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/EngineLogic.cs @@ -0,0 +1,112 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace MotorPlantBusinessLogic.BusinessLogics +{ + public class EngineLogic : IEngineLogic + { + private readonly ILogger _logger; + private readonly IEngineStorage _EngineStorage; + public EngineLogic(ILogger logger, IEngineStorage EngineStorage) + { + _logger = logger; + _EngineStorage = EngineStorage; + } + public List? ReadList(EngineSearchModel? model) + { + _logger.LogInformation("ReadList. EngineName:{EngineName}.Id:{Id}", model?.EngineName, model?.Id); + var list = model == null ? _EngineStorage.GetFullList() : _EngineStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public EngineViewModel? ReadElement(EngineSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. EngineName:{EngineName}.Id:{Id}", model.EngineName, model.Id); + var element = _EngineStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(EngineBindingModel model) + { + CheckModel(model); + if (_EngineStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(EngineBindingModel model) + { + CheckModel(model); + if (_EngineStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(EngineBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_EngineStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(EngineBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.EngineName)) + { + throw new ArgumentNullException("Нет названия изделия", nameof(model.EngineName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Engine. EngineName:{EngineName}.Price:{Cost}. Id: {Id}", model.EngineName, model.Price, model.Id); + var element = _EngineStorage.GetElement(new EngineSearchModel + { + EngineName = model.EngineName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Изделие с таким названием уже есть"); + } + } + } +} diff --git a/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/OrderLogic.cs b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..7b27063 --- /dev/null +++ b/MotorPlant/MotorPlantBusinessLogic/BusinessLogic/OrderLogic.cs @@ -0,0 +1,132 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using Microsoft.Extensions.Logging; +using MotorPlantDataModels.Enums; + +namespace MotorPlantBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + + if (model.Status != OrderStatus.Неизвестен) + return false; + model.Status = OrderStatus.Принят; + + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return ToNextStatus(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return ToNextStatus(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return ToNextStatus(model, OrderStatus.Выдан); + } + + public bool ToNextStatus(OrderBindingModel model, OrderStatus orderStatus) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel() + { + Id = model.Id + }); + if (element == null) + { + throw new ArgumentNullException(nameof(element)); + } + + model.EngineId = element.EngineId; + model.DateCreate = element.DateCreate; + model.DateImplement = element.DateImplement; + model.Status = element.Status; + model.Count = element.Count; + model.Sum = element.Sum; + + if (model.Status != orderStatus - 1) + { + _logger.LogWarning("Status update to " + orderStatus + " operation failed"); + return false; + } + model.Status = orderStatus; + + if (model.Status == OrderStatus.Выдан) + { + model.DateImplement = DateTime.Now; + } + + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Changing status operation faled"); + return false; + } + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); + } + if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) + { + throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} должна быть позже даты его создания {model.DateCreate}"); + } + _logger.LogInformation("Engine. EngineId:{EngineId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.EngineId, model.Count, model.Sum, model.Id); + } + } +} diff --git a/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj new file mode 100644 index 0000000..46d0222 --- /dev/null +++ b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/MotorPlant/MotorPlantContracts/BindingModels/ComponentBindingModel.cs b/MotorPlant/MotorPlantContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..547f803 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,13 @@ +using MotorPlantDataModels.Models; + +namespace MotorPlantContracts.BindingModels +{ + public class ComponentBindingModel : IComponentModel + { + public int Id { get; set; } + + public string ComponentName { get; set; } = string.Empty; + + public double Cost { get; set; } + } +} diff --git a/MotorPlant/MotorPlantContracts/BindingModels/EngineBindingModel.cs b/MotorPlant/MotorPlantContracts/BindingModels/EngineBindingModel.cs new file mode 100644 index 0000000..8a48fc8 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BindingModels/EngineBindingModel.cs @@ -0,0 +1,16 @@ +using MotorPlantDataModels.Models; + +namespace MotorPlantContracts.BindingModels +{ + public class EngineBindingModel : IEngineModel + { + public int Id { get; set; } + + public string EngineName { get; set; } = string.Empty; + + public double Price { get; set; } + + public Dictionary EngineComponents { get; set; } = new(); + + } +} diff --git a/MotorPlant/MotorPlantContracts/BindingModels/OrderBindingModel.cs b/MotorPlant/MotorPlantContracts/BindingModels/OrderBindingModel.cs new file mode 100644 index 0000000..2a825c8 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BindingModels/OrderBindingModel.cs @@ -0,0 +1,27 @@ +using MotorPlantDataModels.Enums; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantContracts.BindingModels +{ + public class OrderBindingModel : IOrderModel + { + public int Id {get; set;} + + public int EngineId { get; set; } + + public int Count { get; set; } + + public double Sum { get; set; } + + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; set; } = DateTime.Now; + + public DateTime? DateImplement { get; set;} +} +} diff --git a/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IComponentLogic.cs b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IComponentLogic.cs new file mode 100644 index 0000000..3069ed2 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IComponentLogic.cs @@ -0,0 +1,17 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantContracts.BusinessLogicsContracts +{ + public interface IComponentLogic + { + List? ReadList(ComponentSearchModel? model); + + ComponentViewModel? ReadElement(ComponentSearchModel model); + + bool Create(ComponentBindingModel model); + bool Update(ComponentBindingModel model); + bool Delete(ComponentBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IEngineLogic.cs b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IEngineLogic.cs new file mode 100644 index 0000000..a7e6650 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IEngineLogic.cs @@ -0,0 +1,16 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantContracts.BusinessLogicsContracts +{ + public interface IEngineLogic + { + List? ReadList(EngineSearchModel? model); + EngineViewModel? ReadElement(EngineSearchModel model); + + bool Create(EngineBindingModel model); + bool Update(EngineBindingModel model); + bool Delete(EngineBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IOrderLogic.cs b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IOrderLogic.cs new file mode 100644 index 0000000..eb9cf4f --- /dev/null +++ b/MotorPlant/MotorPlantContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -0,0 +1,16 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantContracts.BusinessLogicsContracts +{ + public interface IOrderLogic + { + List? ReadList(OrderSearchModel? model); + + bool CreateOrder(OrderBindingModel model); + bool TakeOrderInWork(OrderBindingModel model); + bool FinishOrder(OrderBindingModel model); + bool DeliveryOrder(OrderBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj b/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj new file mode 100644 index 0000000..3fdb632 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/MotorPlant/MotorPlantContracts/SearchModels/ComponentSearchModel.cs b/MotorPlant/MotorPlantContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..19a8983 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,8 @@ +namespace MotorPlantContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id {get; set; } + public string? ComponentName { get; set; } + } +} diff --git a/MotorPlant/MotorPlantContracts/SearchModels/EngineSearchModel.cs b/MotorPlant/MotorPlantContracts/SearchModels/EngineSearchModel.cs new file mode 100644 index 0000000..f6497fa --- /dev/null +++ b/MotorPlant/MotorPlantContracts/SearchModels/EngineSearchModel.cs @@ -0,0 +1,8 @@ +namespace MotorPlantContracts.SearchModels +{ + public class EngineSearchModel + { + public int? Id { get; set; } + public string? EngineName { get; set; } + } +} diff --git a/MotorPlant/MotorPlantContracts/SearchModels/OrderSearchModel.cs b/MotorPlant/MotorPlantContracts/SearchModels/OrderSearchModel.cs new file mode 100644 index 0000000..2255ebd --- /dev/null +++ b/MotorPlant/MotorPlantContracts/SearchModels/OrderSearchModel.cs @@ -0,0 +1,7 @@ +namespace MotorPlantContracts.SearchModels +{ + public class OrderSearchModel + { + public int? Id { get; set; } + } +} diff --git a/MotorPlant/MotorPlantContracts/StoragesContracts/IComponentStorage.cs b/MotorPlant/MotorPlantContracts/StoragesContracts/IComponentStorage.cs new file mode 100644 index 0000000..5f653db --- /dev/null +++ b/MotorPlant/MotorPlantContracts/StoragesContracts/IComponentStorage.cs @@ -0,0 +1,24 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantContracts.StoragesContracts +{ + public interface IComponentStorage + { + List GetFullList(); + + List GetFilteredList(ComponentSearchModel model); + + ComponentViewModel? GetElement(ComponentSearchModel model); + + ComponentViewModel? Insert(ComponentBindingModel model); + ComponentViewModel? Update(ComponentBindingModel model); + ComponentViewModel? Delete(ComponentBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/StoragesContracts/IEngineStorage.cs b/MotorPlant/MotorPlantContracts/StoragesContracts/IEngineStorage.cs new file mode 100644 index 0000000..08c1e2e --- /dev/null +++ b/MotorPlant/MotorPlantContracts/StoragesContracts/IEngineStorage.cs @@ -0,0 +1,16 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantContracts.StoragesContracts +{ + public interface IEngineStorage + { + List GetFullList(); + List GetFilteredList(EngineSearchModel model); + EngineViewModel? GetElement(EngineSearchModel model); + EngineViewModel? Insert(EngineBindingModel model); + EngineViewModel? Update(EngineBindingModel model); + EngineViewModel? Delete(EngineBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/StoragesContracts/IOrderStorage.cs b/MotorPlant/MotorPlantContracts/StoragesContracts/IOrderStorage.cs new file mode 100644 index 0000000..4815307 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/StoragesContracts/IOrderStorage.cs @@ -0,0 +1,16 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantContracts.StoragesContracts +{ + public interface IOrderStorage + { + List GetFullList(); + List GetFilteredList(OrderSearchModel model); + OrderViewModel? GetElement(OrderSearchModel model); + OrderViewModel? Insert(OrderBindingModel model); + OrderViewModel? Update(OrderBindingModel model); + OrderViewModel? Delete(OrderBindingModel model); + } +} diff --git a/MotorPlant/MotorPlantContracts/ViewModels/ComponentViewModel.cs b/MotorPlant/MotorPlantContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..ab561a6 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,18 @@ +using MotorPlantDataModels.Models; +using System.ComponentModel; + +namespace MotorPlantContracts.ViewModels +{ + public class ComponentViewModel : IComponentModel + { + public int Id { get; set; } + + [DisplayName("Название компонента")] + + public string ComponentName { get; set; } = string.Empty; + + [DisplayName("Цена")] + + public double Cost { get; set; } + } +} diff --git a/MotorPlant/MotorPlantContracts/ViewModels/EngineViewModel.cs b/MotorPlant/MotorPlantContracts/ViewModels/EngineViewModel.cs new file mode 100644 index 0000000..db139d9 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/ViewModels/EngineViewModel.cs @@ -0,0 +1,26 @@ +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantContracts.ViewModels +{ + public class EngineViewModel : IEngineModel + { + public int Id { get; set; } + + [DisplayName("Название изделия")] + + public string EngineName { get; set; } = string.Empty; + + [DisplayName("Цена")] + + public double Price { get; set; } + + public Dictionary EngineComponents { get; set; } = new(); + + } +} diff --git a/MotorPlant/MotorPlantContracts/ViewModels/OrderViewModel.cs b/MotorPlant/MotorPlantContracts/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..550f3c5 --- /dev/null +++ b/MotorPlant/MotorPlantContracts/ViewModels/OrderViewModel.cs @@ -0,0 +1,44 @@ +using MotorPlantDataModels.Enums; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantContracts.ViewModels +{ + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + + public int Id { get; set; } + + public int EngineId { get; set; } + + [DisplayName("Изделие")] + + public string EngineName { get; set; } = string.Empty; + + [DisplayName("Количество")] + + public int Count { get; set; } + + [DisplayName("Сумма")] + + public double Sum { get; set; } + + [DisplayName("Статус")] + + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + + [DisplayName("Дата создания")] + + public DateTime DateCreate { get; set; } = DateTime.Now; + + [DisplayName("Дата выполнения")] + + public DateTime? DateImplement { get; set; } + } +} diff --git a/MotorPlant/MotorPlantDataModels/Enums/OrderStatus.cs b/MotorPlant/MotorPlantDataModels/Enums/OrderStatus.cs new file mode 100644 index 0000000..157c4bf --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/Enums/OrderStatus.cs @@ -0,0 +1,15 @@ +namespace MotorPlantDataModels.Enums +{ + public enum OrderStatus + { + Неизвестен = -1, + + Принят = 0, + + Выполняется = 1, + + Готов = 2, + + Выдан = 3 + } +} diff --git a/MotorPlant/MotorPlantDataModels/IId.cs b/MotorPlant/MotorPlantDataModels/IId.cs new file mode 100644 index 0000000..23788c5 --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/IId.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDataModels +{ + public interface IId + { + int Id { get; } + } +} diff --git a/MotorPlant/MotorPlantDataModels/Models/IComponentModel.cs b/MotorPlant/MotorPlantDataModels/Models/IComponentModel.cs new file mode 100644 index 0000000..7239b56 --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/Models/IComponentModel.cs @@ -0,0 +1,8 @@ +namespace MotorPlantDataModels.Models +{ + public interface IComponentModel : IId + { + string ComponentName { get; } + double Cost { get; } + } +} diff --git a/MotorPlant/MotorPlantDataModels/Models/IEngineModel.cs b/MotorPlant/MotorPlantDataModels/Models/IEngineModel.cs new file mode 100644 index 0000000..bf275b8 --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/Models/IEngineModel.cs @@ -0,0 +1,9 @@ +namespace MotorPlantDataModels.Models +{ + public interface IEngineModel : IId + { + string EngineName { get; } + double Price { get; } + Dictionary EngineComponents { get; } + } +} diff --git a/MotorPlant/MotorPlantDataModels/Models/IOrderModel.cs b/MotorPlant/MotorPlantDataModels/Models/IOrderModel.cs new file mode 100644 index 0000000..7981ecc --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/Models/IOrderModel.cs @@ -0,0 +1,14 @@ +using MotorPlantDataModels.Enums; + +namespace MotorPlantDataModels.Models +{ + public interface IOrderModel : IId + { + int EngineId { get; } + int Count { get; } + double Sum { get; } + OrderStatus Status { get; } + DateTime DateCreate { get; } + DateTime? DateImplement { get; } + } +} diff --git a/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj b/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/MotorPlant/MotorPlantListImplement/DataListSingleton.cs b/MotorPlant/MotorPlantListImplement/DataListSingleton.cs new file mode 100644 index 0000000..4bd61f6 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/DataListSingleton.cs @@ -0,0 +1,31 @@ +using MotorPlantListImplement.Models; + +namespace MotorPlantListImplement +{ + internal class DataListSingleton + { + private static DataListSingleton? _instance; + + public List Components { get; set; } + + public List Orders { get; set; } + + public List Engines { get; set; } + + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Engines = new List(); + } + + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/MotorPlant/MotorPlantListImplement/Implements/ComponentStorage.cs b/MotorPlant/MotorPlantListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..7cd2ce4 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,108 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantListImplement.Models; + +namespace MotorPlantListImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + + public List GetFilteredList(ComponentSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if ((!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/MotorPlant/MotorPlantListImplement/Implements/EngineStorage.cs b/MotorPlant/MotorPlantListImplement/Implements/EngineStorage.cs new file mode 100644 index 0000000..730d3a5 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Implements/EngineStorage.cs @@ -0,0 +1,108 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantListImplement.Models; + +namespace MotorPlantListImplement.Implements +{ + public class EngineStorage : IEngineStorage + { + private readonly DataListSingleton _source; + + public EngineStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var engine in _source.Engines) + { + result.Add(engine.GetViewModel); + } + return result; + } + + public List GetFilteredList(EngineSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.EngineName)) + { + return result; + } + foreach (var Engine in _source.Engines) + { + if (Engine.EngineName.Contains(model.EngineName)) + { + result.Add(Engine.GetViewModel); + } + } + return result; + } + + public EngineViewModel? GetElement(EngineSearchModel model) + { + if (string.IsNullOrEmpty(model.EngineName) && !model.Id.HasValue) + { + return null; + } + foreach (var Engine in _source.Engines) + { + if ((!string.IsNullOrEmpty(model.EngineName) && Engine.EngineName == model.EngineName) || + (model.Id.HasValue && Engine.Id == model.Id)) + { + return Engine.GetViewModel; + } + } + return null; + } + + public EngineViewModel? Insert(EngineBindingModel model) + { + model.Id = 1; + foreach (var Engine in _source.Engines) + { + if (model.Id <= Engine.Id) + { + model.Id = Engine.Id + 1; + } + } + var newEngine = Engine.Create(model); + if (newEngine == null) + { + return null; + } + _source.Engines.Add(newEngine); + return newEngine.GetViewModel; + } + + public EngineViewModel? Update(EngineBindingModel model) + { + foreach (var Engine in _source.Engines) + { + if (Engine.Id == model.Id) + { + Engine.Update(model); + return Engine.GetViewModel; + } + } + return null; + } + + public EngineViewModel? Delete(EngineBindingModel model) + { + for (int i = 0; i < _source.Engines.Count; ++i) + { + if (_source.Engines[i].Id == model.Id) + { + var element = _source.Engines[i]; + _source.Engines.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/MotorPlant/MotorPlantListImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..5e75a80 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Implements/OrderStorage.cs @@ -0,0 +1,118 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using MotorPlantListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(AttachEngineName(order.GetViewModel)); + } + return result; + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (model == null || !model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(AttachEngineName(order.GetViewModel)); + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return AttachEngineName(order.GetViewModel); + } + } + return null; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return AttachEngineName(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return AttachEngineName(order.GetViewModel); + } + } + return null; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return AttachEngineName(element.GetViewModel); + } + } + return null; + } + private OrderViewModel AttachEngineName(OrderViewModel model) + { + foreach (var Engine in _source.Engines) + { + if (Engine.Id == model.EngineId) + { + model.EngineName = Engine.EngineName; + return model; + } + } + return model; + } + } +} diff --git a/MotorPlant/MotorPlantListImplement/Models/Component.cs b/MotorPlant/MotorPlantListImplement/Models/Component.cs new file mode 100644 index 0000000..95a2a66 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Models/Component.cs @@ -0,0 +1,52 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Models +{ + internal class Component : IComponentModel + { + public int Id { get; private set; } + + public string ComponentName { get; private set; } = string.Empty; + + public double Cost { get; set; } + + public static Component? Create(ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost, + }; + } + + public void Update(ComponentBindingModel? model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost, + }; + } +} diff --git a/MotorPlant/MotorPlantListImplement/Models/Engine.cs b/MotorPlant/MotorPlantListImplement/Models/Engine.cs new file mode 100644 index 0000000..c7ac342 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Models/Engine.cs @@ -0,0 +1,56 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Models +{ + internal class Engine : IEngineModel + { + public int Id { get; private set; } + + public string EngineName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary EngineComponents { get; private set; } = new Dictionary(); + + public static Engine? Create(EngineBindingModel? model) + { + if (model == null) + { + return null; + } + return new Engine() + { + Id = model.Id, + EngineName = model.EngineName, + Price = model.Price, + EngineComponents = model.EngineComponents, + }; + } + + public void Update(EngineBindingModel? model) + { + if(model == null) + { + return; + } + EngineName = model.EngineName; + Price = model.Price; + EngineComponents = model.EngineComponents; + } + + public EngineViewModel GetViewModel => new() + { + Id = Id, + EngineName = EngineName, + Price = Price, + EngineComponents = EngineComponents, + }; + } +} diff --git a/MotorPlant/MotorPlantListImplement/Models/Order.cs b/MotorPlant/MotorPlantListImplement/Models/Order.cs new file mode 100644 index 0000000..56053d1 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/Models/Order.cs @@ -0,0 +1,71 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Enums; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Models +{ + internal class Order : IOrderModel + { + public int Id { get; private set; } + + public int EngineId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } + + public DateTime DateCreate { get; private set; } + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + Id = model.Id, + EngineId = model.EngineId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public void Update(OrderBindingModel? model) + { + if(model == null) + { + return; + } + Id = model.Id; + EngineId = model.EngineId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + EngineId = EngineId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj b/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj new file mode 100644 index 0000000..4da00b3 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/MotorPlant/MotorPlantView/Form1.Designer.cs b/MotorPlant/MotorPlantView/Form1.Designer.cs deleted file mode 100644 index 3955c75..0000000 --- a/MotorPlant/MotorPlantView/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace MotorPlantView -{ - partial class Form1 - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; - } - - #endregion - } -} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/Form1.cs b/MotorPlant/MotorPlantView/Form1.cs deleted file mode 100644 index 9ef675b..0000000 --- a/MotorPlant/MotorPlantView/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace MotorPlantView -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormComponent.Designer.cs b/MotorPlant/MotorPlantView/FormComponent.Designer.cs new file mode 100644 index 0000000..0eed36d --- /dev/null +++ b/MotorPlant/MotorPlantView/FormComponent.Designer.cs @@ -0,0 +1,117 @@ +namespace MotorPlantView.Forms +{ + partial class FormComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + buttonCancel = new Button(); + buttonSave = new Button(); + textBoxCost = new TextBox(); + label2 = new Label(); + label1 = new Label(); + textBoxName = new TextBox(); + SuspendLayout(); + // + // buttonCancel + // + buttonCancel.Location = new Point(284, 89); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(86, 26); + buttonCancel.TabIndex = 1; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // buttonSave + // + buttonSave.Location = new Point(190, 89); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(86, 26); + buttonSave.TabIndex = 0; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // textBoxCost + // + textBoxCost.Location = new Point(103, 49); + textBoxCost.Name = "textBoxCost"; + textBoxCost.Size = new Size(163, 23); + textBoxCost.TabIndex = 5; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(21, 52); + label2.Name = "label2"; + label2.Size = new Size(38, 15); + label2.TabIndex = 3; + label2.Text = "Цена:"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(21, 22); + label1.Name = "label1"; + label1.Size = new Size(62, 15); + label1.TabIndex = 2; + label1.Text = "Название:"; + // + // textBoxName + // + textBoxName.Location = new Point(103, 20); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(267, 23); + textBoxName.TabIndex = 4; + // + // FormComponent + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(404, 130); + Controls.Add(textBoxCost); + Controls.Add(textBoxName); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Name = "FormComponent"; + Text = "Компонент"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonCancel; + private Button buttonSave; + private TextBox textBoxCost; + private Label label2; + private Label label1; + private TextBox textBoxName; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormComponent.cs b/MotorPlant/MotorPlantView/FormComponent.cs new file mode 100644 index 0000000..2977cab --- /dev/null +++ b/MotorPlant/MotorPlantView/FormComponent.cs @@ -0,0 +1,85 @@ +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.BindingModels; +using Microsoft.Extensions.Logging; + +namespace MotorPlantView.Forms +{ + public partial class FormComponent : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormComponent(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation(" "); + var view = _logic.ReadElement(new ComponentSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ComponentName; + textBoxCost.Text = view.Cost.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, " "); + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation(" "); + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = textBoxName.Text, + Cost = Convert.ToDouble(textBoxCost.Text) + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception(" . ."); + } + MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, " "); + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/Form1.resx b/MotorPlant/MotorPlantView/FormComponent.resx similarity index 93% rename from MotorPlant/MotorPlantView/Form1.resx rename to MotorPlant/MotorPlantView/FormComponent.resx index 1af7de1..af32865 100644 --- a/MotorPlant/MotorPlantView/Form1.resx +++ b/MotorPlant/MotorPlantView/FormComponent.resx @@ -1,17 +1,17 @@  - diff --git a/MotorPlant/MotorPlantView/FormComponents.Designer.cs b/MotorPlant/MotorPlantView/FormComponents.Designer.cs new file mode 100644 index 0000000..fdb57f1 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormComponents.Designer.cs @@ -0,0 +1,129 @@ +namespace MotorPlantView.Forms +{ + partial class FormComponents + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.Window; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.Margin = new Padding(3, 4, 3, 4); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 24; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(585, 531); + dataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(604, 22); + buttonAdd.Margin = new Padding(3, 4, 3, 4); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(125, 35); + buttonAdd.TabIndex = 1; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonUpd + // + buttonUpd.Location = new Point(604, 75); + buttonUpd.Margin = new Padding(3, 4, 3, 4); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(125, 35); + buttonUpd.TabIndex = 2; + buttonUpd.Text = "Изменить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; + // + // buttonDel + // + buttonDel.Location = new Point(604, 129); + buttonDel.Margin = new Padding(3, 4, 3, 4); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(125, 35); + buttonDel.TabIndex = 3; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; + // + // buttonRef + // + buttonRef.Location = new Point(604, 187); + buttonRef.Margin = new Padding(3, 4, 3, 4); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(125, 35); + buttonRef.TabIndex = 4; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += buttonRef_Click; + // + // FormComponents + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + BackColor = SystemColors.ButtonFace; + ClientSize = new Size(755, 531); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(buttonUpd); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Margin = new Padding(3, 4, 3, 4); + Name = "FormComponents"; + Text = "Компоненты"; + Load += FormComponents_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpd; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormComponents.cs b/MotorPlant/MotorPlantView/FormComponents.cs new file mode 100644 index 0000000..6c22176 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormComponents.cs @@ -0,0 +1,104 @@ +using Microsoft.Extensions.Logging; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; + +namespace MotorPlantView.Forms +{ + public partial class FormComponents : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + public FormComponents(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormComponents_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new ComponentBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/MotorPlant/MotorPlantView/FormComponents.resx b/MotorPlant/MotorPlantView/FormComponents.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormComponents.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormCreateOrder.Designer.cs b/MotorPlant/MotorPlantView/FormCreateOrder.Designer.cs new file mode 100644 index 0000000..275d9a5 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormCreateOrder.Designer.cs @@ -0,0 +1,151 @@ +namespace MotorPlantView.Forms +{ + partial class FormCreateOrder + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label1 = new Label(); + label2 = new Label(); + label3 = new Label(); + textBoxCount = new TextBox(); + comboBoxEngine = new ComboBox(); + textBoxSum = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(22, 16); + label1.Name = "label1"; + label1.Size = new Size(71, 20); + label1.TabIndex = 0; + label1.Text = "Изделие:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(22, 59); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 1; + label2.Text = "Количество:"; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(22, 93); + label3.Name = "label3"; + label3.Size = new Size(58, 20); + label3.TabIndex = 2; + label3.Text = "Сумма:"; + // + // textBoxCount + // + textBoxCount.Location = new Point(118, 55); + textBoxCount.Margin = new Padding(3, 4, 3, 4); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(281, 27); + textBoxCount.TabIndex = 3; + textBoxCount.TextChanged += textBoxCount_TextChanged; + // + // comboBoxEngine + // + comboBoxEngine.FormattingEnabled = true; + comboBoxEngine.Location = new Point(118, 13); + comboBoxEngine.Margin = new Padding(3, 4, 3, 4); + comboBoxEngine.Name = "comboBoxEngine"; + comboBoxEngine.Size = new Size(281, 28); + comboBoxEngine.TabIndex = 4; + comboBoxEngine.SelectedIndexChanged += ComboBoxEngine_SelectedIndexChanged; + // + // textBoxSum + // + textBoxSum.BackColor = SystemColors.ControlLightLight; + textBoxSum.Location = new Point(118, 93); + textBoxSum.Margin = new Padding(3, 4, 3, 4); + textBoxSum.Name = "textBoxSum"; + textBoxSum.ReadOnly = true; + textBoxSum.Size = new Size(281, 27); + textBoxSum.TabIndex = 5; + // + // buttonSave + // + buttonSave.Location = new Point(171, 135); + buttonSave.Margin = new Padding(3, 4, 3, 4); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(107, 35); + buttonSave.TabIndex = 6; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(285, 135); + buttonCancel.Margin = new Padding(3, 4, 3, 4); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(107, 35); + buttonCancel.TabIndex = 7; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // FormCreateOrder + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(427, 181); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxSum); + Controls.Add(comboBoxEngine); + Controls.Add(textBoxCount); + Controls.Add(label3); + Controls.Add(label2); + Controls.Add(label1); + Margin = new Padding(3, 4, 3, 4); + Name = "FormCreateOrder"; + Text = "Заказ"; + Load += FormCreateOrder_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label1; + private Label label2; + private Label label3; + private TextBox textBoxCount; + private ComboBox comboBoxEngine; + private TextBox textBoxSum; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormCreateOrder.cs b/MotorPlant/MotorPlantView/FormCreateOrder.cs new file mode 100644 index 0000000..d4fd6e3 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormCreateOrder.cs @@ -0,0 +1,111 @@ +using Microsoft.Extensions.Logging; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.BusinessLogicsContracts; + +namespace MotorPlantView.Forms +{ + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IEngineLogic _logicP; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, IEngineLogic logicP, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicO = logicO; + } + + private void FormCreateOrder_Load(object sender, EventArgs e) + { + try + { + var list = _logicP.ReadList(null); + if (list != null) + { + comboBoxEngine.DisplayMember = "EngineName"; + comboBoxEngine.ValueMember = "Id"; + comboBoxEngine.DataSource = list; + comboBoxEngine.SelectedItem = null; + _logger.LogInformation("Загрузка изделий для заказа"); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка изделий"); + } + } + private void CalcSum() + { + if (comboBoxEngine.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxEngine.SelectedValue); + var Engine = _logicP.ReadElement(new EngineSearchModel { Id = id }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (Engine?.Price ?? 0), 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void textBoxCount_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void ComboBoxEngine_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxEngine.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + EngineId = Convert.ToInt32(comboBoxEngine.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text), + Sum = Convert.ToDouble(textBoxSum.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + } +} diff --git a/MotorPlant/MotorPlantView/FormCreateOrder.resx b/MotorPlant/MotorPlantView/FormCreateOrder.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormCreateOrder.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngine.Designer.cs b/MotorPlant/MotorPlantView/FormEngine.Designer.cs new file mode 100644 index 0000000..35f5bd3 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngine.Designer.cs @@ -0,0 +1,256 @@ +namespace MotorPlantView.Forms +{ + partial class FormEngine + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label1 = new Label(); + label2 = new Label(); + buttonAdd = new Button(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonRef = new Button(); + dataGridView = new DataGridView(); + textBoxName = new TextBox(); + textBoxPrice = new TextBox(); + groupBox1 = new GroupBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + Number = new DataGridViewTextBoxColumn(); + Component = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(18, 20); + label1.Name = "label1"; + label1.Size = new Size(80, 20); + label1.TabIndex = 0; + label1.Text = "Название:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(18, 55); + label2.Name = "label2"; + label2.Size = new Size(86, 20); + label2.TabIndex = 1; + label2.Text = "Стоимость:"; + // + // buttonAdd + // + buttonAdd.BackColor = SystemColors.ButtonFace; + buttonAdd.Location = new Point(656, 59); + buttonAdd.Margin = new Padding(3, 4, 3, 4); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(111, 37); + buttonAdd.TabIndex = 0; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = false; + buttonAdd.Click += buttonAdd_Click; + // + // buttonUpd + // + buttonUpd.BackColor = SystemColors.ButtonFace; + buttonUpd.Location = new Point(656, 120); + buttonUpd.Margin = new Padding(3, 4, 3, 4); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(111, 37); + buttonUpd.TabIndex = 1; + buttonUpd.Text = "Изменить"; + buttonUpd.UseVisualStyleBackColor = false; + buttonUpd.Click += buttonUpd_Click; + // + // buttonDel + // + buttonDel.BackColor = SystemColors.ButtonFace; + buttonDel.Location = new Point(656, 179); + buttonDel.Margin = new Padding(3, 4, 3, 4); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(111, 37); + buttonDel.TabIndex = 2; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = false; + buttonDel.Click += buttonDel_Click; + // + // buttonRef + // + buttonRef.BackColor = SystemColors.ButtonFace; + buttonRef.Location = new Point(656, 231); + buttonRef.Margin = new Padding(3, 4, 3, 4); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(111, 37); + buttonRef.TabIndex = 3; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = false; + buttonRef.Click += buttonRef_Click; + // + // dataGridView + // + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { Number, Component, Count }); + dataGridView.GridColor = SystemColors.ButtonHighlight; + dataGridView.Location = new Point(0, 27); + dataGridView.Margin = new Padding(3, 4, 3, 4); + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 24; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.ShowEditingIcon = false; + dataGridView.Size = new Size(627, 443); + dataGridView.TabIndex = 4; + // + // textBoxName + // + textBoxName.Location = new Point(107, 19); + textBoxName.Margin = new Padding(3, 4, 3, 4); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(281, 27); + textBoxName.TabIndex = 3; + // + // textBoxPrice + // + textBoxPrice.BackColor = SystemColors.ButtonHighlight; + textBoxPrice.Enabled = false; + textBoxPrice.Location = new Point(107, 52); + textBoxPrice.Margin = new Padding(3, 4, 3, 4); + textBoxPrice.Name = "textBoxPrice"; + textBoxPrice.ReadOnly = true; + textBoxPrice.Size = new Size(97, 27); + textBoxPrice.TabIndex = 4; + textBoxPrice.TabStop = false; + // + // groupBox1 + // + groupBox1.Controls.Add(buttonRef); + groupBox1.Controls.Add(buttonDel); + groupBox1.Controls.Add(dataGridView); + groupBox1.Controls.Add(buttonUpd); + groupBox1.Controls.Add(buttonAdd); + groupBox1.Location = new Point(16, 95); + groupBox1.Margin = new Padding(3, 4, 3, 4); + groupBox1.Name = "groupBox1"; + groupBox1.Padding = new Padding(3, 4, 3, 4); + groupBox1.Size = new Size(787, 489); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Компоненты"; + // + // buttonSave + // + buttonSave.Location = new Point(454, 605); + buttonSave.Margin = new Padding(3, 4, 3, 4); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(130, 37); + buttonSave.TabIndex = 0; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(609, 605); + buttonCancel.Margin = new Padding(3, 4, 3, 4); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(130, 37); + buttonCancel.TabIndex = 0; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // Number + // + Number.HeaderText = "Id"; + Number.MinimumWidth = 6; + Number.Name = "Number"; + Number.ReadOnly = true; + Number.Visible = false; + Number.Width = 60; + // + // Component + // + Component.HeaderText = "Компонент"; + Component.MinimumWidth = 6; + Component.Name = "Component"; + Component.ReadOnly = true; + Component.Width = 125; + // + // Count + // + Count.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + Count.HeaderText = "Количество"; + Count.MinimumWidth = 6; + Count.Name = "Count"; + Count.ReadOnly = true; + // + // FormEngine + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(813, 663); + Controls.Add(groupBox1); + Controls.Add(textBoxPrice); + Controls.Add(textBoxName); + Controls.Add(label2); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(label1); + Margin = new Padding(3, 4, 3, 4); + Name = "FormEngine"; + Text = "Двигатель"; + Load += FormEngine_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + groupBox1.ResumeLayout(false); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label label1; + private Label label2; + private Button buttonRef; + private Button buttonDel; + private Button buttonUpd; + private Button buttonAdd; + private DataGridView dataGridView; + private TextBox textBoxName; + private TextBox textBoxPrice; + private GroupBox groupBox1; + private Button buttonSave; + private Button buttonCancel; + private DataGridViewTextBoxColumn Number; + private DataGridViewTextBoxColumn Component; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngine.cs b/MotorPlant/MotorPlantView/FormEngine.cs new file mode 100644 index 0000000..f292451 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngine.cs @@ -0,0 +1,208 @@ +using Microsoft.Extensions.Logging; +using MotorPlantDataModels.Models; +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.BindingModels; + +namespace MotorPlantView.Forms +{ + public partial class FormEngine : Form + { + private readonly ILogger _logger; + + private readonly IEngineLogic _logic; + + private int? _id; + + private Dictionary _productComponents; + + public int Id { set { _id = value; } } + + public FormEngine(ILogger logger, IEngineLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _productComponents = new Dictionary(); + } + + private void FormEngine_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка изделия"); + try + { + var view = _logic.ReadElement(new EngineSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.EngineName; + textBoxPrice.Text = view.Price.ToString(); + _productComponents = view.EngineComponents ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделия"); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка компонент изделия"); + try + { + if (_productComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _productComponents) + { + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент изделия"); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEngineComponent)); + if (service is FormEngineComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента: { ComponentName} - { Count} ", form.ComponentModel.ComponentName, form.Count); + if (_productComponents.ContainsKey(form.Id)) + { + _productComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _productComponents.Add(form.Id, (form.ComponentModel, form.Count)); + } + LoadData(); + } + } + } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEngineComponent)); + if (service is FormEngineComponent form) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _productComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + _productComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + } + + private void buttonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + _logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); + _productComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPrice.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (_productComponents == null || _productComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + var model = new EngineBindingModel + { + Id = _id ?? 0, + EngineName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + EngineComponents = _productComponents + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private double CalcPrice() + { + double price = 0; + foreach (var elem in _productComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} diff --git a/MotorPlant/MotorPlantView/FormEngine.resx b/MotorPlant/MotorPlantView/FormEngine.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngine.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngineComponent.Designer.cs b/MotorPlant/MotorPlantView/FormEngineComponent.Designer.cs new file mode 100644 index 0000000..f4d12c8 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngineComponent.Designer.cs @@ -0,0 +1,124 @@ +namespace MotorPlantView.Forms +{ + partial class FormEngineComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + buttonSave = new Button(); + buttonCancel = new Button(); + label1 = new Label(); + label2 = new Label(); + comboBoxComponent = new ComboBox(); + textBoxCount = new TextBox(); + SuspendLayout(); + // + // buttonSave + // + buttonSave.Location = new Point(199, 100); + buttonSave.Margin = new Padding(3, 4, 3, 4); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(98, 36); + buttonSave.TabIndex = 0; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(309, 100); + buttonCancel.Margin = new Padding(3, 4, 3, 4); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(98, 36); + buttonCancel.TabIndex = 1; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(14, 19); + label1.Name = "label1"; + label1.Size = new Size(91, 20); + label1.TabIndex = 2; + label1.Text = "Компонент:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(14, 59); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 3; + label2.Text = "Количество:"; + // + // comboBoxComponent + // + comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxComponent.FormattingEnabled = true; + comboBoxComponent.Location = new Point(121, 12); + comboBoxComponent.Margin = new Padding(3, 4, 3, 4); + comboBoxComponent.Name = "comboBoxComponent"; + comboBoxComponent.Size = new Size(285, 28); + comboBoxComponent.TabIndex = 4; + // + // textBoxCount + // + textBoxCount.Location = new Point(121, 59); + textBoxCount.Margin = new Padding(3, 4, 3, 4); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(285, 27); + textBoxCount.TabIndex = 5; + // + // FormEngineComponent + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(426, 149); + Controls.Add(textBoxCount); + Controls.Add(comboBoxComponent); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Margin = new Padding(3, 4, 3, 4); + Name = "FormEngineComponent"; + Text = "Компонент двигателя"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label label1; + private Label label2; + private ComboBox comboBoxComponent; + private TextBox textBoxCount; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngineComponent.cs b/MotorPlant/MotorPlantView/FormEngineComponent.cs new file mode 100644 index 0000000..529d8f3 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngineComponent.cs @@ -0,0 +1,68 @@ +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; + +namespace MotorPlantView.Forms +{ + public partial class FormEngineComponent : Form + { + private readonly List? _list; + + public int Id { get { return Convert.ToInt32(comboBoxComponent.SelectedValue); } set { comboBoxComponent.SelectedValue = value; } } + + public IComponentModel? ComponentModel + { + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public int Count { get { return Convert.ToInt32(textBoxCount.Text); } set { textBoxCount.Text = value.ToString(); } } + + public FormEngineComponent(IComponentLogic logic) + { + InitializeComponent(); + + _list = logic.ReadList(null); + if (_list != null) + { + comboBoxComponent.DisplayMember = "ComponentName"; + comboBoxComponent.ValueMember = "Id"; + comboBoxComponent.DataSource = _list; + comboBoxComponent.SelectedItem = null; + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComponent.SelectedValue == null) + { + MessageBox.Show("Выберите компонент", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/MotorPlant/MotorPlantView/FormEngineComponent.resx b/MotorPlant/MotorPlantView/FormEngineComponent.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngineComponent.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngines.Designer.cs b/MotorPlant/MotorPlantView/FormEngines.Designer.cs new file mode 100644 index 0000000..e3b9a47 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngines.Designer.cs @@ -0,0 +1,121 @@ +namespace MotorPlantView.Forms +{ + partial class FormEngines + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + buttonRef = new Button(); + buttonDel = new Button(); + buttonUpd = new Button(); + buttonAdd = new Button(); + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // buttonRef + // + buttonRef.Location = new Point(741, 180); + buttonRef.Margin = new Padding(3, 4, 3, 4); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(128, 35); + buttonRef.TabIndex = 9; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += buttonRef_Click; + // + // buttonDel + // + buttonDel.Location = new Point(741, 129); + buttonDel.Margin = new Padding(3, 4, 3, 4); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(128, 35); + buttonDel.TabIndex = 8; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; + // + // buttonUpd + // + buttonUpd.Location = new Point(741, 77); + buttonUpd.Margin = new Padding(3, 4, 3, 4); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(128, 35); + buttonUpd.TabIndex = 7; + buttonUpd.Text = "Изменить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(741, 27); + buttonAdd.Margin = new Padding(3, 4, 3, 4); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(128, 35); + buttonAdd.TabIndex = 6; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // dataGridView + // + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(-1, 0); + dataGridView.Margin = new Padding(3, 4, 3, 4); + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 24; + dataGridView.Size = new Size(709, 568); + dataGridView.TabIndex = 5; + // + // FormEngines + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(895, 571); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(buttonUpd); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormEngines"; + Text = "Список двигателей"; + Load += FormEngines_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Button buttonRef; + private Button buttonDel; + private Button buttonUpd; + private Button buttonAdd; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormEngines.cs b/MotorPlant/MotorPlantView/FormEngines.cs new file mode 100644 index 0000000..7e129dc --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngines.cs @@ -0,0 +1,105 @@ +using Microsoft.Extensions.Logging; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; + +namespace MotorPlantView.Forms +{ + public partial class FormEngines : Form + { + private readonly ILogger _logger; + private readonly IEngineLogic _logic; + public FormEngines(ILogger logger, IEngineLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormEngines_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["EngineComponents"].Visible = false; + dataGridView.Columns["EngineName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEngine)); + if (service is FormEngine form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEngine)); + if (service is FormEngine form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление записи"); + try + { + if (!_logic.Delete(new EngineBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления записи"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/MotorPlant/MotorPlantView/FormEngines.resx b/MotorPlant/MotorPlantView/FormEngines.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormEngines.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormMain.Designer.cs b/MotorPlant/MotorPlantView/FormMain.Designer.cs new file mode 100644 index 0000000..1673d78 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormMain.Designer.cs @@ -0,0 +1,194 @@ +namespace MotorPlantView.Forms +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + toolStrip1 = new ToolStrip(); + toolStripDropDownButton1 = new ToolStripDropDownButton(); + КомпонентыToolStripMenuItem = new ToolStripMenuItem(); + ДвигателиToolStripMenuItem = new ToolStripMenuItem(); + buttonCreateOrder = new Button(); + buttonTakeOrderInWork = new Button(); + buttonOrderReady = new Button(); + buttonIssuedOrder = new Button(); + buttonRef = new Button(); + dataGridView = new DataGridView(); + toolStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // toolStrip1 + // + toolStrip1.ImageScalingSize = new Size(20, 20); + toolStrip1.Items.AddRange(new ToolStripItem[] { toolStripDropDownButton1 }); + toolStrip1.Location = new Point(0, 0); + toolStrip1.Name = "toolStrip1"; + toolStrip1.Size = new Size(1126, 27); + toolStrip1.TabIndex = 0; + toolStrip1.Text = "toolStrip1"; + // + // toolStripDropDownButton1 + // + toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыToolStripMenuItem, ДвигателиToolStripMenuItem }); + toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; + toolStripDropDownButton1.Name = "toolStripDropDownButton1"; + toolStripDropDownButton1.Size = new Size(108, 24); + toolStripDropDownButton1.Text = "Справочник"; + // + // КомпонентыToolStripMenuItem + // + КомпонентыToolStripMenuItem.Name = "КомпонентыToolStripMenuItem"; + КомпонентыToolStripMenuItem.Size = new Size(224, 26); + КомпонентыToolStripMenuItem.Text = "Компоненты"; + КомпонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; + // + // ДвигателиToolStripMenuItem + // + ДвигателиToolStripMenuItem.Name = "ДвигателиToolStripMenuItem"; + ДвигателиToolStripMenuItem.Size = new Size(224, 26); + ДвигателиToolStripMenuItem.Text = "Двигатели"; + ДвигателиToolStripMenuItem.Click += ИзделияToolStripMenuItem_Click; + // + // buttonCreateOrder + // + buttonCreateOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonCreateOrder.BackColor = SystemColors.ControlLight; + buttonCreateOrder.Location = new Point(911, 195); + buttonCreateOrder.Margin = new Padding(3, 4, 3, 4); + buttonCreateOrder.Name = "buttonCreateOrder"; + buttonCreateOrder.Size = new Size(203, 40); + buttonCreateOrder.TabIndex = 1; + buttonCreateOrder.Text = "Создать заказ"; + buttonCreateOrder.UseVisualStyleBackColor = false; + buttonCreateOrder.Click += buttonCreateOrder_Click; + // + // buttonTakeOrderInWork + // + buttonTakeOrderInWork.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonTakeOrderInWork.BackColor = SystemColors.ControlLight; + buttonTakeOrderInWork.Location = new Point(911, 258); + buttonTakeOrderInWork.Margin = new Padding(3, 4, 3, 4); + buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; + buttonTakeOrderInWork.Size = new Size(203, 40); + buttonTakeOrderInWork.TabIndex = 2; + buttonTakeOrderInWork.Text = "Отдать на выполнение"; + buttonTakeOrderInWork.UseVisualStyleBackColor = false; + buttonTakeOrderInWork.Click += buttonTakeOrderInWork_Click; + // + // buttonOrderReady + // + buttonOrderReady.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonOrderReady.BackColor = SystemColors.ControlLight; + buttonOrderReady.Location = new Point(911, 323); + buttonOrderReady.Margin = new Padding(3, 4, 3, 4); + buttonOrderReady.Name = "buttonOrderReady"; + buttonOrderReady.Size = new Size(203, 40); + buttonOrderReady.TabIndex = 3; + buttonOrderReady.Text = "Заказ готов"; + buttonOrderReady.UseVisualStyleBackColor = false; + buttonOrderReady.Click += buttonOrderReady_Click; + // + // buttonIssuedOrder + // + buttonIssuedOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonIssuedOrder.BackColor = SystemColors.ControlLight; + buttonIssuedOrder.Location = new Point(911, 383); + buttonIssuedOrder.Margin = new Padding(3, 4, 3, 4); + buttonIssuedOrder.Name = "buttonIssuedOrder"; + buttonIssuedOrder.Size = new Size(203, 40); + buttonIssuedOrder.TabIndex = 4; + buttonIssuedOrder.Text = "Заказ выдан"; + buttonIssuedOrder.UseVisualStyleBackColor = false; + buttonIssuedOrder.Click += buttonIssuedOrder_Click; + // + // buttonRef + // + buttonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRef.BackColor = SystemColors.ControlLight; + buttonRef.Location = new Point(911, 133); + buttonRef.Margin = new Padding(3, 4, 3, 4); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(203, 40); + buttonRef.TabIndex = 5; + buttonRef.Text = "Обновить список"; + buttonRef.UseVisualStyleBackColor = false; + buttonRef.Click += buttonRef_Click; + // + // dataGridView + // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(0, 35); + dataGridView.Margin = new Padding(3, 4, 3, 4); + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 24; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(891, 588); + dataGridView.TabIndex = 6; + // + // FormMain + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1126, 623); + Controls.Add(dataGridView); + Controls.Add(buttonRef); + Controls.Add(buttonIssuedOrder); + Controls.Add(buttonOrderReady); + Controls.Add(buttonTakeOrderInWork); + Controls.Add(buttonCreateOrder); + Controls.Add(toolStrip1); + Margin = new Padding(3, 4, 3, 4); + Name = "FormMain"; + Text = "Моторный завод"; + Load += FormMain_Load; + toolStrip1.ResumeLayout(false); + toolStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ToolStrip toolStrip1; + private Button buttonCreateOrder; + private Button buttonTakeOrderInWork; + private Button buttonOrderReady; + private Button buttonIssuedOrder; + private Button buttonRef; + private DataGridView dataGridView; + private ToolStripDropDownButton toolStripDropDownButton1; + private ToolStripMenuItem КомпонентыToolStripMenuItem; + private ToolStripMenuItem ДвигателиToolStripMenuItem; + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/FormMain.cs b/MotorPlant/MotorPlantView/FormMain.cs new file mode 100644 index 0000000..06f7132 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormMain.cs @@ -0,0 +1,147 @@ +using Microsoft.Extensions.Logging; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.BusinessLogicsContracts; + +namespace MotorPlantView.Forms +{ + public partial class FormMain : Form + { + private readonly ILogger _logger; + private readonly IOrderLogic _orderLogic; + public FormMain(ILogger logger, IOrderLogic orderLogic) + { + InitializeComponent(); + _logger = logger; + _orderLogic = orderLogic; + } + private void FormMain_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + _logger.LogInformation("Загрузка заказов"); + try + { + var list = _orderLogic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["EngineId"].Visible = false; + dataGridView.Columns["EngineName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + + _logger.LogInformation("Загрузка заказов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки заказов"); + } + } + private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + + if (service is FormComponents form) + { + form.ShowDialog(); + } + } + private void ИзделияToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEngines)); + + if (service is FormEngines form) + { + form.ShowDialog(); + } + } + private void buttonCreateOrder_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + if (service is FormCreateOrder form) + { + form.ShowDialog(); + LoadData(); + } + } + private void buttonTakeOrderInWork_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); + try + { + var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = id, + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void buttonOrderReady_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); + try + { + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void buttonIssuedOrder_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); + try + { + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = id + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + _logger.LogInformation("Заказ №{id} выдан", id); + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/MotorPlant/MotorPlantView/FormMain.resx b/MotorPlant/MotorPlantView/FormMain.resx new file mode 100644 index 0000000..ae2d9c5 --- /dev/null +++ b/MotorPlant/MotorPlantView/FormMain.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 68 + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/MotorPlantView.csproj b/MotorPlant/MotorPlantView/MotorPlantView.csproj index b57c89e..1199a44 100644 --- a/MotorPlant/MotorPlantView/MotorPlantView.csproj +++ b/MotorPlant/MotorPlantView/MotorPlantView.csproj @@ -8,4 +8,27 @@ enable + + + + + + + Always + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/Program.cs b/MotorPlant/MotorPlantView/Program.cs index 2e234f5..21b1eff 100644 --- a/MotorPlant/MotorPlantView/Program.cs +++ b/MotorPlant/MotorPlantView/Program.cs @@ -1,7 +1,19 @@ +using MotorPlantView.Forms; +using MotorPlantContracts.BusinessLogicsContracts; +using MotorPlantContracts.StoragesContracts; +using MotorPlantListImplement.Implements; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using MotorPlantBusinessLogic.BusinessLogics; +using System; + namespace MotorPlantView { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +23,32 @@ namespace MotorPlantView // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + + Application.Run(_serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/MotorPlant/MotorPlantView/nlog.config b/MotorPlant/MotorPlantView/nlog.config new file mode 100644 index 0000000..af70d20 --- /dev/null +++ b/MotorPlant/MotorPlantView/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file