From d8186eecc03c4b8452207cf64a8d09d481eb5f55 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 9 May 2023 13:13:47 +0400 Subject: [PATCH] Models for MongoDB + MongoDB database singleton --- .../ConstructionCompanyDatabase.cs | 193 ++++++++++++++++++ .../Models/Employee.cs | 47 ++++- .../Models/EmployeeOrder.cs | 40 ++++ .../Models/Material.cs | 61 ++++++ .../Models/MaterialOrder.cs | 43 ++++ .../Models/Order.cs | 88 ++++++++ .../Models/Position.cs | 9 + .../Models/Order.cs | 2 - 8 files changed, 480 insertions(+), 3 deletions(-) create mode 100644 ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs create mode 100644 ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/EmployeeOrder.cs create mode 100644 ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Material.cs create mode 100644 ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/MaterialOrder.cs create mode 100644 ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs new file mode 100644 index 0000000..68b5574 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/ConstructionCompanyDatabase.cs @@ -0,0 +1,193 @@ +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); + } + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs index 9b0468d..48c1b0e 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Employee.cs @@ -1,4 +1,6 @@ -using ConstructionCompanyDataModels.Models; +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; @@ -14,5 +16,48 @@ namespace ConstructionCompanyMongoDBImplement.Models 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 static BsonDocument? CreateBSON(EmployeeBindingModel? model) + { + if (model == null) + { + return null; + } + return new BsonDocument + { + {"_id", model.Id}, + {"EmployeeName", $"{model.EmployeeName}"}, + {"positionId", $"{model.PositionID}" } + }; + } + + public static BsonDocument? UpdateBSON(EmployeeBindingModel? model) + { + if (model == null) + { + return null; + } + return new BsonDocument + { + {"_id", model.Id}, + {"EmployeeName", $"{model.EmployeeName}"}, + {"positionId", $"{model.PositionID}" } + }; + } } } diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/EmployeeOrder.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/EmployeeOrder.cs new file mode 100644 index 0000000..39c713d --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/EmployeeOrder.cs @@ -0,0 +1,40 @@ +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 ConstructionCompanyMongoDBImplement.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 EmployeeOrderViewModel GetViewModel => new() + { + OrderId = OrderId, + EmployeeId = EmployeeId, + OrderAdress = Order.Adress, + EmployeeName = Employee.EmployeeName + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Material.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Material.cs new file mode 100644 index 0000000..42a44ac --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Material.cs @@ -0,0 +1,61 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyMongoDBImplement.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 BsonDocument? CreateBSON(MaterialBindingModel? model) + { + if (model == null) + { + return null; + } + return new BsonDocument + { + {"_id", model.Id}, + {"materialName", $"{model.MaterialName}"}, + {"quantity", $"{model.Quantity}" } + }; + } + + public static BsonDocument? UpdateBSON(MaterialBindingModel? model) + { + if (model == null) + { + return null; + } + return new BsonDocument + { + {"_id", model.Id}, + {"materialName", $"{model.MaterialName}"}, + {"quantity", $"{model.Quantity}" } + }; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/MaterialOrder.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/MaterialOrder.cs new file mode 100644 index 0000000..29616f6 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/MaterialOrder.cs @@ -0,0 +1,43 @@ +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 ConstructionCompanyMongoDBImplement.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 MaterialOrderViewModel GetViewModel => new() + { + OrderId = OrderId, + MaterialId = MaterialId, + Quantity = Quantity, + OrderAdress = Order.Adress, + MaterialName = Material.MaterialName + }; + } +} diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs new file mode 100644 index 0000000..b916f82 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Order.cs @@ -0,0 +1,88 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyDataModels.Enums; +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyMongoDBImplement.Models +{ + public class Order + { + 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 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 static BsonDocument? CreateBSON(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + BsonArray bsonArray = new BsonArray(); + return new BsonDocument + { + {"_id", model.Id}, + {"description", $"{model.Description}"}, + {"adress", $"{model.Adress}"}, + {"customerNumber", $"{model.CustomerNumber}"}, + {"price", $"{model.Price}"}, + {"status", $"{model.Status}"}, + {"dateBegin", $"{model.DateBegin}"}, + {"dateEnd", $"{model.DateEnd}"}, + {"employeesId", new BsonArray() }, + {"materials", new BsonArray()} + }; + } + + public static BsonDocument? UpdateBSON(OrderBindingModel? model, BsonArray employeesId, BsonArray materials) + { + if (model == null) + { + return null; + } + return new BsonDocument + { + {"_id", model.Id}, + {"description", $"{model.Description}"}, + {"adress", $"{model.Adress}"}, + {"customerNumber", $"{model.CustomerNumber}"}, + {"price", $"{model.Price}"}, + {"status", $"{model.Status}"}, + {"dateBegin", $"{model.DateBegin}"}, + {"dateEnd", $"{model.DateEnd}"}, + {"employeesId", employeesId }, + {"materials", materials} + }; + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs index 2f35678..a375379 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs @@ -1,4 +1,5 @@ using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; using MongoDB.Bson; using System; @@ -40,6 +41,7 @@ namespace ConstructionCompanyMongoDBImplement.Models } return new BsonDocument { + {"_id", model.Id}, {"PositionName", $"{model.PositionName}"}, {"Salary", $"{model.Salary}" }, {"EmployeesId", new BsonArray{}} @@ -54,10 +56,17 @@ namespace ConstructionCompanyMongoDBImplement.Models } return new BsonDocument { + {"_id", model.Id}, {"PositionName", $"{model.PositionName}"}, {"Salary", $"{model.Salary}" }, {"EmployeesId", new BsonArray(ids)} }; } + public PositionViewModel GetViewModel => new() + { + Id = Id, + PositionName = PositionName, + Salary = Salary + }; } } diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs index 57d1f77..1fcb42e 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs @@ -17,8 +17,6 @@ namespace ConstructionCompanyPsqlImplement.Models 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; }