From 49325d73b2d293ded9e42ee4f2cb90b015c4a707 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Sun, 9 Apr 2023 17:23:33 +0400 Subject: [PATCH] Psql implement done + models for many-to-many relationships + Buisness logic and forms for materials (testing purpouses) --- ConstructionCompany/ConstructionCompany.sln | 6 + .../BusinessLogics/MaterialLogic.cs | 110 +++++++++ .../ConstructionCompanyBusinessLogic.csproj | 17 ++ .../EmployeeOrderBindingModel.cs | 15 ++ .../MaterialOrderBindingModel.cs | 16 ++ .../IEmployeeOrderLogic.cs | 20 ++ .../IMaterialOrderLogic.cs | 20 ++ .../SearchModels/EmployeeOrderSearchModel.cs | 14 ++ .../SearchModels/MaterialOrderSearchModel.cs | 14 ++ .../StorageContracts/IEmployeeOrderStorage.cs | 20 ++ .../StorageContracts/IMaterialOrderStorage.cs | 20 ++ .../ViewModels/EmployeeOrderViewModel.cs | 20 ++ .../ViewModels/MaterialOrderViewModel.cs | 22 ++ .../Models/IEmployeeOrderModel.cs | 14 ++ .../Models/IMaterialOrderModel.cs | 15 ++ .../ConstructionCompanyDatabase.cs | 208 ++++++++++++++++++ .../ConstructionCompanyPsqlImplement.csproj | 26 +++ .../Implements/EmployeeOrderStorage.cs | 78 +++++++ .../Implements/EmployeeStorage.cs | 101 +++++++++ .../Implements/MaterialOrderStorage.cs | 79 +++++++ .../Implements/MaterialStorage.cs | 101 +++++++++ .../Implements/OrderStorage.cs | 90 ++++++++ .../Implements/PositionStorage.cs | 101 +++++++++ .../Models/Employee.cs | 76 +++++++ .../Models/EmployeeOrder.cs | 56 +++++ .../Models/Material.cs | 73 ++++++ .../Models/MaterialOrder.cs | 59 +++++ .../Models/Order.cs | 100 +++++++++ .../Models/Position.cs | 74 +++++++ .../ConstructionCompanyView.csproj | 16 ++ .../ConstructionCompanyView/Form1.Designer.cs | 39 ---- .../ConstructionCompanyView/Form1.cs | 10 - .../ConstructionCompanyView/Form1.resx | 120 ---------- .../FormMaterial.Designer.cs | 119 ++++++++++ .../ConstructionCompanyView/FormMaterial.cs | 96 ++++++++ .../ConstructionCompanyView/FormMaterial.resx | 60 +++++ .../FormMaterials.Designer.cs | 120 ++++++++++ .../ConstructionCompanyView/FormMaterials.cs | 110 +++++++++ .../FormMaterials.resx | 60 +++++ .../ConstructionCompanyView/Program.cs | 28 ++- .../nlogConstruction.config | 15 ++ 41 files changed, 2188 insertions(+), 170 deletions(-) create mode 100644 ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/MaterialLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj create mode 100644 ConstructionCompany/ConstructionCompanyContracts/BindingModels/EmployeeOrderBindingModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/BindingModels/MaterialOrderBindingModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeOrderLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IMaterialOrderLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/SearchModels/EmployeeOrderSearchModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/SearchModels/MaterialOrderSearchModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IEmployeeOrderStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IMaterialOrderStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/ViewModels/EmployeeOrderViewModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyContracts/ViewModels/MaterialOrderViewModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyDataModels/Models/IEmployeeOrderModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyDataModels/Models/IMaterialOrderModel.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyDatabase.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeOrderStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialOrderStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/OrderStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/PositionStorage.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/EmployeeOrder.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs create mode 100644 ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs delete mode 100644 ConstructionCompany/ConstructionCompanyView/Form1.Designer.cs delete mode 100644 ConstructionCompany/ConstructionCompanyView/Form1.cs delete mode 100644 ConstructionCompany/ConstructionCompanyView/Form1.resx create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterial.Designer.cs create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterial.cs create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterial.resx create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterials.Designer.cs create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterials.cs create mode 100644 ConstructionCompany/ConstructionCompanyView/FormMaterials.resx create mode 100644 ConstructionCompany/ConstructionCompanyView/nlogConstruction.config diff --git a/ConstructionCompany/ConstructionCompany.sln b/ConstructionCompany/ConstructionCompany.sln index 33e343d..428e4c2 100644 --- a/ConstructionCompany/ConstructionCompany.sln +++ b/ConstructionCompany/ConstructionCompany.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConstructionCompanyContract EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConstructionCompanyBusinessLogic", "ConstructionCompanyBusiness\ConstructionCompanyBusinessLogic.csproj", "{FB447245-07E1-4E0D-A4FC-701E295B7575}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConstructionCompanyPsqlImplement", "ConstructionCompanyPsqlImplement\ConstructionCompanyPsqlImplement.csproj", "{A92A2186-2021-4B42-B632-447A45EC3A58}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {FB447245-07E1-4E0D-A4FC-701E295B7575}.Debug|Any CPU.Build.0 = Debug|Any CPU {FB447245-07E1-4E0D-A4FC-701E295B7575}.Release|Any CPU.ActiveCfg = Release|Any CPU {FB447245-07E1-4E0D-A4FC-701E295B7575}.Release|Any CPU.Build.0 = Release|Any CPU + {A92A2186-2021-4B42-B632-447A45EC3A58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A92A2186-2021-4B42-B632-447A45EC3A58}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A92A2186-2021-4B42-B632-447A45EC3A58}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A92A2186-2021-4B42-B632-447A45EC3A58}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/MaterialLogic.cs b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/MaterialLogic.cs new file mode 100644 index 0000000..d2e35f0 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/MaterialLogic.cs @@ -0,0 +1,110 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyBusinessLogic.BusinessLogics +{ + public class MaterialLogic : IMaterialLogic + { + private readonly ILogger _logger; + private readonly IMaterialStorage _materialStorage; + + public MaterialLogic(ILogger logger, IMaterialStorage materialStorage) + { + _logger = logger; + _materialStorage = materialStorage; + } + + public List? ReadList(MaterialSearchModel? model) + { + _logger.LogInformation("ReadList. MaterialName:{MaterialName}. Id:{Id}", model?.MaterialName, model?.Id); + var list = model == null ? _materialStorage.GetFullList() : _materialStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public MaterialViewModel? ReadElement(MaterialSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. MaterialName:{MaterialName}. Id:{Id}", model.MaterialName, model.Id); + var element = _materialStorage.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(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MaterialBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_materialStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(MaterialBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.MaterialName)) + { + throw new ArgumentNullException("Нет названия материала", nameof(model.MaterialName)); + } + if (model.Quantity < 0) + { + throw new ArgumentNullException("Количество материалов должна быть не меньше 0!", nameof(model.Quantity)); + } + _logger.LogInformation("Material. IngredietnName:{MaterialName}. Cost:{Quantity}. Id:{Id}", model.MaterialName, model.Quantity, model.Id); + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj b/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj new file mode 100644 index 0000000..3a7f827 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusiness/ConstructionCompanyBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/ConstructionCompany/ConstructionCompanyContracts/BindingModels/EmployeeOrderBindingModel.cs b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/EmployeeOrderBindingModel.cs new file mode 100644 index 0000000..c9e12f4 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/EmployeeOrderBindingModel.cs @@ -0,0 +1,15 @@ +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.BindingModels +{ + public class EmployeeOrderBindingModel : IEmployeeOrderModel + { + public int EmployeeId { get; set; } + public int OrderId { get; set; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/BindingModels/MaterialOrderBindingModel.cs b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/MaterialOrderBindingModel.cs new file mode 100644 index 0000000..41b53b5 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/MaterialOrderBindingModel.cs @@ -0,0 +1,16 @@ +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.BindingModels +{ + public class MaterialOrderBindingModel : IMaterialOrderModel + { + public int MaterialId { get; set; } + public int OrderId { get; set; } + public int Quantity { get; set; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeOrderLogic.cs b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeOrderLogic.cs new file mode 100644 index 0000000..f16df49 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeOrderLogic.cs @@ -0,0 +1,20 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.BusinessLogicContracts +{ + public interface IEmployeeOrderLogic + { + List? ReadList(EmployeeOrderSearchModel? model); + EmployeeOrderViewModel? ReadElement(EmployeeOrderSearchModel model); + bool Create(EmployeeOrderBindingModel model); + bool Update(EmployeeOrderBindingModel model); + bool Delete(EmployeeOrderBindingModel model); + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IMaterialOrderLogic.cs b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IMaterialOrderLogic.cs new file mode 100644 index 0000000..cced54a --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IMaterialOrderLogic.cs @@ -0,0 +1,20 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.BusinessLogicContracts +{ + public interface IMaterialOrderLogic + { + List? ReadList(MaterialOrderSearchModel? model); + MaterialOrderViewModel? ReadElement(MaterialOrderSearchModel model); + bool Create(MaterialOrderBindingModel model); + bool Update(MaterialOrderBindingModel model); + bool Delete(MaterialOrderBindingModel model); + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/SearchModels/EmployeeOrderSearchModel.cs b/ConstructionCompany/ConstructionCompanyContracts/SearchModels/EmployeeOrderSearchModel.cs new file mode 100644 index 0000000..750f98b --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/SearchModels/EmployeeOrderSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.SearchModels +{ + public class EmployeeOrderSearchModel + { + public int? EmployeeId { get; set; } + public int? OrderId { get; set; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/SearchModels/MaterialOrderSearchModel.cs b/ConstructionCompany/ConstructionCompanyContracts/SearchModels/MaterialOrderSearchModel.cs new file mode 100644 index 0000000..aa189f3 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/SearchModels/MaterialOrderSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.SearchModels +{ + public class MaterialOrderSearchModel + { + public int? MaterialId { get; set; } + public int? OrderId { get; set; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IEmployeeOrderStorage.cs b/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IEmployeeOrderStorage.cs new file mode 100644 index 0000000..74c5a43 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IEmployeeOrderStorage.cs @@ -0,0 +1,20 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.StorageContracts +{ + public interface IEmployeeOrderStorage + { + List GetFullList(); + List GetFilteredList(EmployeeOrderSearchModel model); + EmployeeOrderViewModel? GetElement(EmployeeOrderSearchModel model); + EmployeeOrderViewModel? Insert(EmployeeOrderBindingModel model); + EmployeeOrderViewModel? Delete(EmployeeOrderBindingModel model); + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IMaterialOrderStorage.cs b/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IMaterialOrderStorage.cs new file mode 100644 index 0000000..31f7789 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/StorageContracts/IMaterialOrderStorage.cs @@ -0,0 +1,20 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.StorageContracts +{ + public interface IMaterialOrderStorage + { + List GetFullList(); + List GetFilteredList(MaterialOrderSearchModel model); + MaterialOrderViewModel? GetElement(MaterialOrderSearchModel model); + MaterialOrderViewModel? Insert(MaterialOrderBindingModel model); + MaterialOrderViewModel? Delete(MaterialOrderBindingModel model); + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/ViewModels/EmployeeOrderViewModel.cs b/ConstructionCompany/ConstructionCompanyContracts/ViewModels/EmployeeOrderViewModel.cs new file mode 100644 index 0000000..8f80214 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/ViewModels/EmployeeOrderViewModel.cs @@ -0,0 +1,20 @@ +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.ViewModels +{ + public class EmployeeOrderViewModel : IEmployeeOrderModel + { + public int EmployeeId { get; set; } + [DisplayName("ФИО сотрудника")] + public string EmployeeName { get; set; } = string.Empty; + public int OrderId { get; set; } + [DisplayName("Адресс заказа")] + public string OrderAdress { get; set; } = string.Empty; + } +} diff --git a/ConstructionCompany/ConstructionCompanyContracts/ViewModels/MaterialOrderViewModel.cs b/ConstructionCompany/ConstructionCompanyContracts/ViewModels/MaterialOrderViewModel.cs new file mode 100644 index 0000000..d23f7ed --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyContracts/ViewModels/MaterialOrderViewModel.cs @@ -0,0 +1,22 @@ +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyContracts.ViewModels +{ + public class MaterialOrderViewModel : IMaterialOrderModel + { + public int MaterialId { get; set; } + [DisplayName("Название материала")] + public string MaterialName { get; set; } = string.Empty; + public int OrderId { get; set; } + [DisplayName("Адресс заказа")] + public string OrderAdress { get; set; } = string.Empty; + [DisplayName("Количество")] + public int Quantity { get; set; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyDataModels/Models/IEmployeeOrderModel.cs b/ConstructionCompany/ConstructionCompanyDataModels/Models/IEmployeeOrderModel.cs new file mode 100644 index 0000000..3ebf254 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyDataModels/Models/IEmployeeOrderModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyDataModels.Models +{ + public interface IEmployeeOrderModel + { + int EmployeeId { get; } + int OrderId { get; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyDataModels/Models/IMaterialOrderModel.cs b/ConstructionCompany/ConstructionCompanyDataModels/Models/IMaterialOrderModel.cs new file mode 100644 index 0000000..160e26f --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyDataModels/Models/IMaterialOrderModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyDataModels.Models +{ + public interface IMaterialOrderModel + { + int MaterialId { get; } + int OrderId { get; } + int Quantity { get; } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyDatabase.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyDatabase.cs new file mode 100644 index 0000000..f5d7b9d --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyDatabase.cs @@ -0,0 +1,208 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConstructionCompanyPsqlImplement.Models; +using ConstructionCompanyContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using ConstructionCompanyDataModels.Enums; + +namespace ConstructionCompanyPsqlImplement +{ + public class ConstructionCompanyDatabase + { + static string connectionString = "Server=192.168.1.35;Port=5432;Database=ConstructionCompanyForwardEngineerd;User Id=postgres;Password=postgres;"; + + private static ConstructionCompanyDatabase? _instance; + private List _materials = new List(); + private List _employees = new List(); + private List _positions = new List(); + private List _orders = new List(); + private List _employeeOrders = new List(); + private List _materialOrders = new List(); + public List Materials + { + get + { + refreshDb(); + return _materials; + } + } + public List Employees + { + get + { + refreshDb(); + return _employees; + } + } + public List Positions + { + get + { + refreshDb(); + return _positions; + } + } + public List Orders + { + get + { + refreshDb(); + return _orders; + } + } + + public List EmployeeOrders + { + get + { + refreshDb(); + return _employeeOrders; + } + } + + public List MaterialOrders + { + get + { + refreshDb(); + return _materialOrders; + } + } + + public static ConstructionCompanyDatabase GetInstance() + { + if (_instance == null) + { + _instance = new ConstructionCompanyDatabase(); + } + return _instance; + } + + public void ExecuteSql(string commandString) + { + using var connection = new NpgsqlConnection(connectionString); + connection.Open(); + using var command = connection.CreateCommand(); + command.CommandText = commandString; + command.ExecuteNonQuery(); + refreshDb(); + connection.Close(); + } + + private void refreshDb() + { + _materials.Clear(); + _positions.Clear(); + _employees.Clear(); + _orders.Clear(); + _employeeOrders.Clear(); + _materialOrders.Clear(); + using var connection = new NpgsqlConnection(connectionString); + connection.Open(); + + using var commandMaterials = connection.CreateCommand(); + commandMaterials.CommandText = "SELECT * FROM material;"; + using var readerMaterials = commandMaterials.ExecuteReader(); + while (readerMaterials.Read()) + { + int id = readerMaterials.GetInt32(0); + string name = readerMaterials.GetString(1); + int quantity = readerMaterials.GetInt32(2); + Material? mat = Material.Create(new MaterialBindingModel { Id = id, MaterialName = name, Quantity = quantity }); + if (mat != null) _materials.Add(mat); + } + readerMaterials.Close(); + + using var commandPositions = connection.CreateCommand(); + commandPositions.CommandText = "SELECT * FROM position;"; + using var readerPositions = commandPositions.ExecuteReader(); + while (readerPositions.Read()) + { + int id = readerPositions.GetInt32(0); + string name = readerPositions.GetString(1); + double salary = readerPositions.GetDouble(2); + Position? position = Position.Create(new PositionBindingModel { Id = id, PositionName = name, Salary = salary }); + if (position != null) _positions.Add(position); + } + readerPositions.Close(); + + using var commandEmployees = connection.CreateCommand(); + commandEmployees.CommandText = "SELECT * FROM employee;"; + using var readerEmployees = commandEmployees.ExecuteReader(); + while (readerEmployees.Read()) + { + int id = readerEmployees.GetInt32(0); + string name = readerEmployees.GetString(1); + int positionId = readerEmployees.GetInt32(2); + Employee? employee = Employee.Create(new EmployeeBindingModel { Id = id, EmployeeName = name, PositionID = positionId }, _positions); + if (employee != null) _employees.Add(employee); + } + readerEmployees.Close(); + + using var commandOrders = connection.CreateCommand(); + commandOrders.CommandText = "SELECT * FROM \"order\";"; + using var readerOrders = commandOrders.ExecuteReader(); + while (readerOrders.Read()) + { + int id = readerOrders.GetInt32(0); + string description = readerOrders.GetString(1); + string adress = readerOrders.GetString(2); + double price = readerOrders.GetDouble(3); + string statusString = readerOrders.GetString(4); + OrderStatus status; + switch (statusString) + { + case "Принят": + status = OrderStatus.Принят; + break; + case "Выполняется": + status = OrderStatus.Выполняется; + break; + case "Завершён": + status = OrderStatus.Завершён; + break; + default: + status = OrderStatus.Неизвестен; + break; + } + string customerNumber = readerOrders.GetString(5); + DateTime dateBegin = readerOrders.GetDateTime(6); + DateTime? dateEnd = readerOrders.GetDateTime(7); + Order? order = Order.Create(new OrderBindingModel { Id = id, Description = description, Adress = adress, Price = price, Status = status, CustomerNumber = customerNumber, DateBegin = dateBegin, DateEnd = dateEnd}); + if (order != null) _orders.Add(order); + } + readerOrders.Close(); + + using var commandEmployeeOrders = connection.CreateCommand(); + commandEmployeeOrders.CommandText = "SELECT * FROM employee_order;"; + using var readerEmployeeOrders = commandEmployeeOrders.ExecuteReader(); + while (readerEmployeeOrders.Read()) + { + int employeeId = readerEmployeeOrders.GetInt32(0); + int orderId = readerEmployeeOrders.GetInt32(1); + EmployeeOrder? employeeOrder = EmployeeOrder.Create(new EmployeeOrderBindingModel { EmployeeId = employeeId, OrderId = orderId}, _employees, _orders); + if (employeeOrder != null) _employeeOrders.Add(employeeOrder); + } + readerEmployeeOrders.Close(); + + using var commandMaterialOrders = connection.CreateCommand(); + commandMaterialOrders.CommandText = "SELECT * FROM material_order;"; + using var readerMaterialOrders = commandMaterialOrders.ExecuteReader(); + while (readerMaterialOrders.Read()) + { + int materialId = readerMaterialOrders.GetInt32(0); + int orderId = readerMaterialOrders.GetInt32(1); + int quantity = readerMaterialOrders.GetInt32(2); + MaterialOrder? materialOrder = MaterialOrder.Create(new MaterialOrderBindingModel { MaterialId = materialId, OrderId = orderId, Quantity = quantity}, _materials, _orders); + if (materialOrder != null) _materialOrders.Add(materialOrder); + } + readerMaterialOrders.Close(); + + connection.Close(); + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj new file mode 100644 index 0000000..e88a907 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj @@ -0,0 +1,26 @@ + + + + net6.0 + enable + enable + 9152d6ad-e687-4e3f-836c-fd7263918b1d + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeOrderStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeOrderStorage.cs new file mode 100644 index 0000000..c516734 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeOrderStorage.cs @@ -0,0 +1,78 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class EmployeeOrderStorage : IEmployeeOrderStorage + { + private readonly ConstructionCompanyDatabase _source; + public EmployeeOrderStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.EmployeeOrders) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(EmployeeOrderSearchModel model) + { + if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue) + { + return new(); + } + List result = new List(); + foreach (var material in _source.EmployeeOrders) + { + if (material.EmployeeId == model.EmployeeId && material.OrderId == model.OrderId) result.Add(material.GetViewModel); + } + return result; + } + + public EmployeeOrderViewModel? GetElement(EmployeeOrderSearchModel model) + { + if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue) + { + return new(); + } + return _source.EmployeeOrders.FirstOrDefault(x => x.EmployeeId == model.EmployeeId && x.OrderId == model.OrderId)?.GetViewModel; + } + + public EmployeeOrderViewModel? Insert(EmployeeOrderBindingModel model) + { + var command = EmployeeOrder.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newEmployeeOrder = _source.EmployeeOrders[_source.EmployeeOrders.Count - 1]; + return newEmployeeOrder.GetViewModel; + } + + public EmployeeOrderViewModel? Delete(EmployeeOrderBindingModel model) + { + var command = EmployeeOrder.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedEmployeeOrder = _source.EmployeeOrders.First(x => x.EmployeeId == model.EmployeeId && x.OrderId == model.OrderId).GetViewModel; + _source.ExecuteSql(command); + return deletedEmployeeOrder; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeStorage.cs new file mode 100644 index 0000000..99a616b --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeStorage.cs @@ -0,0 +1,101 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class EmployeeStorage : IEmployeeStorage + { + private readonly ConstructionCompanyDatabase _source; + public EmployeeStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.Employees) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(EmployeeSearchModel model) + { + if (model == null || !model.Id.HasValue && string.IsNullOrEmpty(model.EmployeeName)) + { + return new(); + } + List result = new List(); + if (!string.IsNullOrEmpty(model.EmployeeName)) + { + foreach (var material in _source.Employees) + { + if (material.EmployeeName.Equals(model.EmployeeName)) result.Add(material.GetViewModel); + } + return result; + } + else + { + foreach (var material in _source.Employees) + { + if (material.Id == model.Id) result.Add(material.GetViewModel); + } + return result; + } + } + + public EmployeeViewModel? GetElement(EmployeeSearchModel model) + { + if (model == null || !model.Id.HasValue) + { + return new(); + } + return _source.Employees.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public EmployeeViewModel? Insert(EmployeeBindingModel model) + { + var command = Employee.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newEmployee = _source.Employees[_source.Employees.Count - 1]; + return newEmployee.GetViewModel; + } + + public EmployeeViewModel? Update(EmployeeBindingModel model) + { + var command = Employee.UpdateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var updatedEmployee = _source.Employees.First(x => x.Id == model.Id); + return updatedEmployee.GetViewModel; + } + + public EmployeeViewModel? Delete(EmployeeBindingModel model) + { + var command = Employee.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedEmployee = _source.Employees.First(x => x.Id == model.Id).GetViewModel; + _source.ExecuteSql(command); + return deletedEmployee; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialOrderStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialOrderStorage.cs new file mode 100644 index 0000000..6cd8bab --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialOrderStorage.cs @@ -0,0 +1,79 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class MaterialOrderStorage : IMaterialOrderStorage + { + private readonly ConstructionCompanyDatabase _source; + public MaterialOrderStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.MaterialOrders) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(MaterialOrderSearchModel model) + { + if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue) + { + return new(); + } + List result = new List(); + foreach (var material in _source.MaterialOrders) + { + if (material.MaterialId == model.MaterialId && material.OrderId == model.OrderId) result.Add(material.GetViewModel); + } + return result; + } + + public MaterialOrderViewModel? GetElement(MaterialOrderSearchModel model) + { + if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue) + { + return new(); + } + return _source.MaterialOrders.FirstOrDefault(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId)?.GetViewModel; + } + + public MaterialOrderViewModel? Insert(MaterialOrderBindingModel model) + { + var command = MaterialOrder.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newMaterialOrder = _source.MaterialOrders[_source.MaterialOrders.Count - 1]; + return newMaterialOrder.GetViewModel; + } + + public MaterialOrderViewModel? Delete(MaterialOrderBindingModel model) + { + var command = MaterialOrder.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedMaterialOrder = _source.MaterialOrders.First(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId).GetViewModel; + _source.ExecuteSql(command); + return deletedMaterialOrder; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialStorage.cs new file mode 100644 index 0000000..d4580c7 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialStorage.cs @@ -0,0 +1,101 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class MaterialStorage : IMaterialStorage + { + private readonly ConstructionCompanyDatabase _source; + public MaterialStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.Materials) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(MaterialSearchModel model) + { + if (model == null || !model.Id.HasValue || string.IsNullOrEmpty(model.MaterialName)) + { + return new(); + } + List result = new List(); + if (!string.IsNullOrEmpty(model.MaterialName)) + { + foreach (var material in _source.Materials) + { + if (material.MaterialName.Equals(model.MaterialName)) result.Add(material.GetViewModel); + } + return result; + } + else + { + foreach (var material in _source.Materials) + { + if (material.Id == model.Id) result.Add(material.GetViewModel); + } + return result; + } + } + + public MaterialViewModel? GetElement(MaterialSearchModel model) + { + if (model == null || !model.Id.HasValue) + { + return new(); + } + return _source.Materials.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public MaterialViewModel? Insert(MaterialBindingModel model) + { + var command = Material.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newMaterial = _source.Materials[_source.Materials.Count - 1]; + return newMaterial.GetViewModel; + } + + public MaterialViewModel? Update(MaterialBindingModel model) + { + var command = Material.UpdateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var updatedMaterial = _source.Materials.First(x => x.Id == model.Id); + return updatedMaterial.GetViewModel; + } + + public MaterialViewModel? Delete(MaterialBindingModel model) + { + var command = Material.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedMaterial = _source.Materials.First(x => x.Id == model.Id).GetViewModel; + _source.ExecuteSql(command); + return deletedMaterial; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/OrderStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1b835f4 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/OrderStorage.cs @@ -0,0 +1,90 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly ConstructionCompanyDatabase _source; + public OrderStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.Orders) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (model == null || !model.Id.HasValue) + { + return new(); + } + List result = new List(); + foreach (var material in _source.Orders) + { + if (material.Id == model.Id) result.Add(material.GetViewModel); + } + return result; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (model == null || !model.Id.HasValue) + { + return new(); + } + return _source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var command = Order.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newOrder = _source.Orders[_source.Orders.Count - 1]; + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var command = Order.UpdateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var updatedOrder = _source.Orders.First(x => x.Id == model.Id); + return updatedOrder.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var command = Order.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedOrder = _source.Orders.First(x => x.Id == model.Id).GetViewModel; + _source.ExecuteSql(command); + return deletedOrder; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/PositionStorage.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/PositionStorage.cs new file mode 100644 index 0000000..12c1446 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/PositionStorage.cs @@ -0,0 +1,101 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyPsqlImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Implements +{ + public class PositionStorage : IPositionStorage + { + private readonly ConstructionCompanyDatabase _source; + public PositionStorage() + { + _source = ConstructionCompanyDatabase.GetInstance(); + } + public List GetFullList() + { + List result = new List(); + foreach (var material in _source.Positions) + { + result.Add(material.GetViewModel); + } + return result; + } + + public List GetFilteredList(PositionSearchModel model) + { + if (model == null || !model.Id.HasValue && string.IsNullOrEmpty(model.PositionName)) + { + return new(); + } + List result = new List(); + if (!string.IsNullOrEmpty(model.PositionName)) + { + foreach (var material in _source.Positions) + { + if (material.PositionName.Equals(model.PositionName)) result.Add(material.GetViewModel); + } + return result; + } + else + { + foreach (var material in _source.Positions) + { + if (material.Id == model.Id) result.Add(material.GetViewModel); + } + return result; + } + } + + public PositionViewModel? GetElement(PositionSearchModel model) + { + if (model == null || !model.Id.HasValue) + { + return new(); + } + return _source.Positions.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public PositionViewModel? Insert(PositionBindingModel model) + { + var command = Position.CreateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var newPosition = _source.Positions[_source.Positions.Count - 1]; + return newPosition.GetViewModel; + } + + public PositionViewModel? Update(PositionBindingModel model) + { + var command = Position.UpdateCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + _source.ExecuteSql(command); + var updatedPosition = _source.Positions.First(x => x.Id == model.Id); + return updatedPosition.GetViewModel; + } + + public PositionViewModel? Delete(PositionBindingModel model) + { + var command = Position.DeleteCommand(model); + if (string.IsNullOrEmpty(command)) + { + return null; + } + var deletedPosition = _source.Positions.First(x => x.Id == model.Id).GetViewModel; + _source.ExecuteSql(command); + return deletedPosition; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs new file mode 100644 index 0000000..517fdd3 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs @@ -0,0 +1,76 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class Employee : IEmployeeModel + { + public string EmployeeName { get; private set; } = string.Empty; + + public int Id { get; private set; } + public int PositionID { get; private set; } + public Position Position { get; set; } = new(); + + public static Employee? Create(EmployeeBindingModel? model, List positions) + { + if (model == null) + { + return null; + } + return new Employee() + { + Id = model.Id, + EmployeeName = model.EmployeeName, + PositionID = model.PositionID, + Position = positions.First(x => x.Id == model.PositionID) + }; + } + public void Update(EmployeeBindingModel? model) + { + if (model == null) + { + return; + } + EmployeeName = model.EmployeeName; + PositionID = model.PositionID; + } + public static string CreateCommand(EmployeeBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO employee(name, position_id) VALUES(\'{model.EmployeeName}\', {model.PositionID});"; + } + public static string UpdateCommand(EmployeeBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"UPDATE employee SET \"name\" = \'{model.EmployeeName}\', position_id = {model.PositionID} WHERE id = {model.Id}"; + } + public static string DeleteCommand(EmployeeBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM employee WHERE id = {model.Id}"; + } + + public EmployeeViewModel GetViewModel => new() + { + Id = Id, + EmployeeName = EmployeeName, + PositionID = PositionID, + PositionName = Position.PositionName + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/EmployeeOrder.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/EmployeeOrder.cs new file mode 100644 index 0000000..73a5f8c --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/EmployeeOrder.cs @@ -0,0 +1,56 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class EmployeeOrder : IEmployeeOrderModel + { + public int EmployeeId { get; set; } + public int OrderId { get; set; } + public Employee Employee { get; set; } = new(); + public Order Order { get; set; } = new(); + public static EmployeeOrder? Create(EmployeeOrderBindingModel? model, List employees, List orders) + { + if (model == null) + { + return null; + } + return new EmployeeOrder() + { + EmployeeId = model.EmployeeId, + OrderId = model.OrderId, + Employee = employees.First(x => x.Id == model.EmployeeId), + Order = orders.First(x => x.Id == model.OrderId), + }; + } + public static string CreateCommand(EmployeeOrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO employee_order(employee_id, order_id) VALUES({model.EmployeeId}, {model.OrderId});"; + } + public static string DeleteCommand(EmployeeOrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM material WHERE employee_id = {model.EmployeeId} AND order_id = {model.OrderId}"; + } + public EmployeeOrderViewModel GetViewModel => new() + { + OrderId = OrderId, + EmployeeId = EmployeeId, + OrderAdress = Order.Adress, + EmployeeName = Employee.EmployeeName + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs new file mode 100644 index 0000000..5100712 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs @@ -0,0 +1,73 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class Material : IMaterialModel + { + public string MaterialName {get; private set;} = string.Empty; + + public int Quantity { get; set; } + + public int Id { get; private set; } + + public static Material? Create(MaterialBindingModel? model) + { + if (model == null) + { + return null; + } + return new Material() + { + Id = model.Id, + MaterialName = model.MaterialName, + Quantity = model.Quantity + }; + } + public static string CreateCommand(MaterialBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO material(name, quantity) VALUES(\'{model.MaterialName}\', {model.Quantity});"; + } + public static string UpdateCommand(MaterialBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"UPDATE material SET \"name\" = \'{model.MaterialName}\', quantity = {model.Quantity} WHERE id = {model.Id}"; + } + public static string DeleteCommand(MaterialBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM material WHERE id = {model.Id}"; + } + public void Update(MaterialBindingModel? model) + { + if (model == null) + { + return; + } + MaterialName = model.MaterialName; + Quantity = model.Quantity; + } + public MaterialViewModel GetViewModel => new() + { + Id = Id, + MaterialName = MaterialName, + Quantity = Quantity + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs new file mode 100644 index 0000000..a5d8e25 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs @@ -0,0 +1,59 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class MaterialOrder : IMaterialOrderModel + { + public int MaterialId { get; set; } + public int OrderId { get; set; } + public int Quantity { get; set; } + public Material Material { get; set; } = new(); + public Order Order { get; set; } = new(); + public static MaterialOrder? Create(MaterialOrderBindingModel? model, List materials, List orders) + { + if (model == null) + { + return null; + } + return new MaterialOrder() + { + MaterialId = model.MaterialId, + OrderId = model.OrderId, + Quantity = model.Quantity, + Material = materials.First(x => x.Id == model.MaterialId), + Order = orders.First(x => x.Id == model.OrderId), + }; + } + public static string CreateCommand(MaterialOrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO material_order(material_id, order_id, quantity) VALUES({model.MaterialId}, {model.OrderId}, {model.Quantity});"; + } + public static string DeleteCommand(MaterialOrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM material WHERE material_id = {model.MaterialId} AND order_id = {model.OrderId}"; + } + public MaterialOrderViewModel GetViewModel => new() + { + OrderId = OrderId, + MaterialId = MaterialId, + Quantity = Quantity, + OrderAdress = Order.Adress, + MaterialName = Material.MaterialName + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs new file mode 100644 index 0000000..a86a273 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs @@ -0,0 +1,100 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Enums; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public string Description { get; private set; } = string.Empty; + public string Adress { get; private set; } = string.Empty; + public string CustomerNumber { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary OrderEmolyees { get; private set; } = new Dictionary(); + public Dictionary OrderMaterials { get; private set; } = new Dictionary(); + + public OrderStatus Status { get; private set; } + + public DateTime DateBegin { get; private set; } + + public DateTime? DateEnd { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Description = model.Description, + Adress = model.Adress, + CustomerNumber = model.CustomerNumber, + Price = model.Price, + Status = model.Status, + DateBegin = model.DateBegin, + DateEnd = model.DateEnd, + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateEnd.HasValue) DateEnd = model.DateEnd; + } + public static string CreateCommand(OrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO \"order\"(description, adress, price, status, " + + $"customer_number, date_begin, date_end) VALUES(\'{model.Description}\', \'{model.Adress}\', {model.Price}, " + + $"\'{model.Status}\', \'{model.CustomerNumber}\', \'{model.DateBegin}\', " + + $"{(model.DateEnd.HasValue ? $"\'{model.DateEnd}\'" : "null")});"; + } + public static string UpdateCommand(OrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"UPDATE \"order\" SET description = \'{model.Description}\', adress = \'{model.Adress}\', " + + $"price = {model.Price}, status = \'{model.Status}\', customer_number = \'{model.CustomerNumber}\', " + + $"date_begin = \'{model.CustomerNumber}\', date_end = {(model.DateEnd.HasValue ? $"\'{model.DateEnd}\'" : "null")} " + + $"WHERE id = {model.Id}"; + + } + public static string DeleteCommand(OrderBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM \"order\" WHERE id = {model.Id}"; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + Description = Description, + Adress = Adress, + CustomerNumber = CustomerNumber, + Price = Price, + Status = Status, + DateBegin = DateBegin, + DateEnd = DateEnd + }; + + } +} diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs new file mode 100644 index 0000000..0ef7954 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs @@ -0,0 +1,74 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyPsqlImplement.Models +{ + public class Position : IPositionModel + { + public string PositionName { get; private set; } = string.Empty; + + public double Salary { get; set; } + + public int Id { get; private set; } + + public static Position? Create(PositionBindingModel? model) + { + if (model == null) + { + return null; + } + return new Position() + { + Id = model.Id, + PositionName = model.PositionName, + Salary = model.Salary + }; + } + public void Update(PositionBindingModel? model) + { + if (model == null) + { + return; + } + PositionName = model.PositionName; + Salary = model.Salary; + } + public static string CreateCommand(PositionBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"INSERT INTO position(name, salary) VALUES(\'{model.PositionName}\', {model.Salary});"; + } + public static string UpdateCommand(PositionBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"UPDATE position SET \"name\" = \'{model.PositionName}\', salary = {model.Salary} WHERE id = {model.Id}"; + } + public static string DeleteCommand(PositionBindingModel? model) + { + if (model == null) + { + return ""; + } + return $"DELETE FROM postition WHERE id = {model.Id}"; + } + + public PositionViewModel GetViewModel => new() + { + Id = Id, + PositionName = PositionName, + Salary = Salary + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj b/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj index b57c89e..0596a05 100644 --- a/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj +++ b/ConstructionCompany/ConstructionCompanyView/ConstructionCompanyView.csproj @@ -8,4 +8,20 @@ enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/Form1.Designer.cs b/ConstructionCompany/ConstructionCompanyView/Form1.Designer.cs deleted file mode 100644 index 37e4bd5..0000000 --- a/ConstructionCompany/ConstructionCompanyView/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace ConstructionCompanyView -{ - 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/ConstructionCompany/ConstructionCompanyView/Form1.cs b/ConstructionCompany/ConstructionCompanyView/Form1.cs deleted file mode 100644 index 030e137..0000000 --- a/ConstructionCompany/ConstructionCompanyView/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ConstructionCompanyView -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/Form1.resx b/ConstructionCompany/ConstructionCompanyView/Form1.resx deleted file mode 100644 index 1af7de1..0000000 --- a/ConstructionCompany/ConstructionCompanyView/Form1.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/ConstructionCompany/ConstructionCompanyView/FormMaterial.Designer.cs b/ConstructionCompany/ConstructionCompanyView/FormMaterial.Designer.cs new file mode 100644 index 0000000..0d19912 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterial.Designer.cs @@ -0,0 +1,119 @@ +namespace ConstructionCompanyView +{ + partial class FormMaterial + { + /// + /// 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.label1 = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxQuantity = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 9); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(77, 20); + this.label1.TabIndex = 0; + this.label1.Text = "Название"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(108, 6); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(276, 27); + this.textBoxName.TabIndex = 1; + // + // textBoxQuantity + // + this.textBoxQuantity.Location = new System.Drawing.Point(108, 56); + this.textBoxQuantity.Name = "textBoxQuantity"; + this.textBoxQuantity.Size = new System.Drawing.Size(113, 27); + this.textBoxQuantity.TabIndex = 3; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 59); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(90, 20); + this.label2.TabIndex = 2; + this.label2.Text = "Количество"; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(202, 117); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.TabIndex = 4; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(307, 117); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormMaterial + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(413, 158); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.textBoxQuantity); + this.Controls.Add(this.label2); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.label1); + this.Name = "FormMaterial"; + this.Text = "Материал"; + this.Load += new System.EventHandler(this.FormMaterial_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label label1; + private TextBox textBoxName; + private TextBox textBoxQuantity; + private Label label2; + private Button buttonCreate; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/FormMaterial.cs b/ConstructionCompany/ConstructionCompanyView/FormMaterial.cs new file mode 100644 index 0000000..258637d --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterial.cs @@ -0,0 +1,96 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConstructionCompanyView +{ + public partial class FormMaterial : Form + { + private readonly ILogger _logger; + private readonly IMaterialLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormMaterial(ILogger logger, IMaterialLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxQuantity.Text)) + { + MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение материала"); + try + { + var model = new MaterialBindingModel + { + Id = _id ?? 0, + MaterialName = textBoxName.Text, + Quantity = Convert.ToInt32(textBoxQuantity.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(); + } + + private void FormMaterial_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение материала"); + var view = _logic.ReadElement(new MaterialSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.MaterialName; + textBoxQuantity.Text = view.Quantity.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения материала"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyView/FormMaterial.resx b/ConstructionCompany/ConstructionCompanyView/FormMaterial.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterial.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ConstructionCompany/ConstructionCompanyView/FormMaterials.Designer.cs b/ConstructionCompany/ConstructionCompanyView/FormMaterials.Designer.cs new file mode 100644 index 0000000..9d43574 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterials.Designer.cs @@ -0,0 +1,120 @@ +namespace ConstructionCompanyView +{ + partial class FormMaterials + { + /// + /// 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.buttonRef = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(523, 186); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(94, 29); + this.buttonRef.TabIndex = 9; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.buttonRef_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(523, 128); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(94, 29); + this.buttonDel.TabIndex = 8; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(523, 71); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(94, 29); + this.buttonUpd.TabIndex = 7; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.buttonUpd_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(523, 14); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(94, 29); + this.buttonAdd.TabIndex = 6; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); + // + // dataGridView + // + this.dataGridView.BackgroundColor = System.Drawing.Color.White; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Dock = System.Windows.Forms.DockStyle.Left; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.MultiSelect = false; + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersVisible = false; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView.Size = new System.Drawing.Size(489, 452); + this.dataGridView.TabIndex = 5; + // + // FormMaterials + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(641, 452); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormMaterials"; + this.Text = "Материалы"; + this.Load += new System.EventHandler(this.FormMaterials_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.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/ConstructionCompany/ConstructionCompanyView/FormMaterials.cs b/ConstructionCompany/ConstructionCompanyView/FormMaterials.cs new file mode 100644 index 0000000..6ef1902 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterials.cs @@ -0,0 +1,110 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConstructionCompanyView +{ + public partial class FormMaterials : Form + { + private readonly ILogger _logger; + private readonly IMaterialLogic _logic; + + public FormMaterials(ILogger logger, IMaterialLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormMaterials_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["MaterialName"].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(FormMaterial)); + if (service is FormMaterial 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(FormMaterial)); + if (service is FormMaterial 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 MaterialBindingModel { 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/ConstructionCompany/ConstructionCompanyView/FormMaterials.resx b/ConstructionCompany/ConstructionCompanyView/FormMaterials.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/FormMaterials.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ConstructionCompany/ConstructionCompanyView/Program.cs b/ConstructionCompany/ConstructionCompanyView/Program.cs index 5175964..b474788 100644 --- a/ConstructionCompany/ConstructionCompanyView/Program.cs +++ b/ConstructionCompany/ConstructionCompanyView/Program.cs @@ -1,7 +1,17 @@ +using ConstructionCompanyBusinessLogic.BusinessLogics; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyPsqlImplement.Implements; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; + namespace ConstructionCompanyView { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +21,23 @@ namespace ConstructionCompanyView // 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("nlogConstruction.config"); + }); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyView/nlogConstruction.config b/ConstructionCompany/ConstructionCompanyView/nlogConstruction.config new file mode 100644 index 0000000..b94cde0 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyView/nlogConstruction.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file