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(); } } }