using ConstructionCompanyMongoDBImplement.Models; using ConstructionCompanyContracts.BindingModels; using MongoDB.Bson; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConstructionCompanyDataModels.Enums; namespace ConstructionCompanyMongoDBImplement { public class ConstructionCompanyDatabase { static string connectionString = "mongodb://localhost:27017/"; static string databaseName = "ConstructionCompany"; private static ConstructionCompanyDatabase? _instance; private MongoClient client = new MongoClient(connectionString); 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 InsertDocument(BsonDocument bson, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.InsertOne(bson); refreshDb(); } public void ReplaceDocument(BsonDocument bson, BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.ReplaceOne(filter, bson); refreshDb(); } public void DeleteDocument(BsonDocument filter, string collectionName) { var collection = client.GetDatabase(databaseName).GetCollection(collectionName); collection.DeleteOne(filter); refreshDb(); } private void refreshDb() { _materials.Clear(); _positions.Clear(); _employees.Clear(); _orders.Clear(); _employeeOrders.Clear(); _materialOrders.Clear(); IMongoDatabase database = client.GetDatabase(databaseName); List positions = database.GetCollection("Positions").Find(new BsonDocument()).ToList(); foreach (var position in positions) { int id = position[0].ToInt32(); string? name = position[1].ToString(); double salary = position[2].ToDouble(); Position? newPosition = Position.Create(new PositionBindingModel { Id = id, PositionName = name, Salary = salary }); if (newPosition != null) _positions.Add(newPosition); } List materials = database.GetCollection("Materials").Find(new BsonDocument()).ToList(); foreach (var material in materials) { int id = material[0].ToInt32(); string? name = material[1].ToString(); int quantity = material[2].ToInt32(); Material? newMaterial = Material.Create(new MaterialBindingModel { Id = id, MaterialName = name, Quantity = quantity }); if (newMaterial != null) _materials.Add(newMaterial); } List employees = database.GetCollection("Employees").Find(new BsonDocument()).ToList(); foreach (var employee in employees) { int id = employee[0].ToInt32(); string? name = employee[1].ToString(); int positionId = employee[2].ToInt32(); Employee? newEmployee = Employee.Create(new EmployeeBindingModel { Id = id, EmployeeName = name, PositionID = positionId }, _positions); if (newEmployee != null) _employees.Add(newEmployee); } List orders = database.GetCollection("Orders").Find(new BsonDocument()).ToList(); foreach (var order in orders) { int id = order[0].ToInt32(); string? description = order[1].ToString(); string? adress = order[2].ToString(); double price = order[3].ToDouble(); string? status = order[4].ToString(); OrderStatus orderStatus = OrderStatus.Неизвестен; switch (status) { case "Принят": orderStatus = OrderStatus.Принят; break; case "Выполняется": orderStatus = OrderStatus.Выполняется; break; case "Завершён": orderStatus = OrderStatus.Завершён; break; } string? customerNumber = order[5].ToString(); DateTime dateBegin = order[6].ToUniversalTime(); DateTime dateEnd = order[7].ToUniversalTime(); Order? newOrder = Order.Create(new OrderBindingModel { Id = id, Description = description, Adress = adress, Price = price, Status = orderStatus, CustomerNumber = customerNumber, DateBegin = dateBegin, DateEnd = dateEnd }); if (newOrder != null) _orders.Add(newOrder); BsonArray employeesId = order[8].AsBsonArray; foreach (var employee in employeesId) { EmployeeOrder? employeeOrder = EmployeeOrder.Create(new EmployeeOrderBindingModel { EmployeeId = employee.ToInt32(), OrderId = id }, _employees, _orders); if (employeeOrder != null) _employeeOrders.Add(employeeOrder); } BsonArray orderMaterials = order[9].AsBsonArray; foreach (var material in orderMaterials) { int materialId = material[0].ToInt32(); int quantity = material[1].ToInt32(); MaterialOrder? materialOrder = MaterialOrder.Create(new MaterialOrderBindingModel { MaterialId = materialId, OrderId = id, Quantity = quantity }, _materials, _orders); } } } } }