From 72346544710889a288205bbfdf3aa32c3809ca8a Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Thu, 11 May 2023 13:03:42 +0400 Subject: [PATCH] Added db sync feature --- .../Implements/PositionStorage.cs | 7 +--- .../Models/Position.cs | 8 ++--- .../ConstructionCompanyPsqlImplement.csproj | 1 + .../Models/Employee.cs | 20 ++++++++++++ .../Models/Material.cs | 19 +++++++++++ .../Models/MaterialOrder.cs | 8 +++++ .../Models/Order.cs | 32 +++++++++++++++++++ .../Models/Position.cs | 17 ++++++++++ .../ConstructionCompanyView/FormLogin.cs | 32 +++++++++++++++++++ 9 files changed, 133 insertions(+), 11 deletions(-) diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs index f91f90b..a4c0abd 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs @@ -79,12 +79,7 @@ namespace ConstructionCompanyMongoDBImplement.Implements public PositionViewModel? Update(PositionBindingModel model) { - List ids = new List(); - foreach (var employee in _source.Employees) - { - if (employee.PositionID == model.Id) ids.Add(employee.Id); - } - var document = Position.UpdateBSON(model, ids); + var document = Position.UpdateBSON(model); if (document == null) { return null; diff --git a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs index 0d9fe55..cf1f9ee 100644 --- a/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs +++ b/ConstructionCompany/ConstructionCompanyMongoDBImplement/Models/Position.cs @@ -43,12 +43,11 @@ namespace ConstructionCompanyMongoDBImplement.Models { {"_id", model.Id}, {"PositionName", $"{model.PositionName}"}, - {"Salary", $"{model.Salary}" }, - {"EmployeesId", new BsonArray{}} + {"Salary", $"{model.Salary}" } }; } - public static BsonDocument? UpdateBSON(PositionBindingModel? model, List ids) + public static BsonDocument? UpdateBSON(PositionBindingModel? model) { if (model == null) { @@ -58,8 +57,7 @@ namespace ConstructionCompanyMongoDBImplement.Models { {"_id", model.Id}, {"PositionName", $"{model.PositionName}"}, - {"Salary", $"{model.Salary}" }, - {"EmployeesId", new BsonArray(ids)} + {"Salary", $"{model.Salary}" } }; } public PositionViewModel GetViewModel => new() diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj index e88a907..7cd29a6 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/ConstructionCompanyPsqlImplement.csproj @@ -13,6 +13,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs index 517fdd3..123a763 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Employee.cs @@ -1,10 +1,12 @@ using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace ConstructionCompanyPsqlImplement.Models @@ -72,5 +74,23 @@ namespace ConstructionCompanyPsqlImplement.Models PositionID = PositionID, PositionName = Position.PositionName }; + + public BsonDocument CreateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"EmployeeName", $"{EmployeeName}"}, + {"positionId", PositionID - 1} + }; + + + + public BsonDocument UpdateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"EmployeeName", $"{EmployeeName}"}, + {"positionId", PositionID - 1} + }; } } diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs index 46f00d9..2d70952 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs @@ -1,6 +1,7 @@ using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; @@ -72,6 +73,24 @@ namespace ConstructionCompanyPsqlImplement.Models MaterialName = model.MaterialName; Quantity = model.Quantity; } + + public BsonDocument CreateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"materialName", $"{MaterialName}"}, + {"quantity", $"{Quantity}" } + }; + + public BsonDocument UpdateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"materialName", $"{MaterialName}"}, + {"quantity", $"{Quantity}" } + }; + + public MaterialViewModel GetViewModel => new() { Id = Id, diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs index da7918f..d6c2d66 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs @@ -1,6 +1,7 @@ using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; @@ -47,6 +48,13 @@ namespace ConstructionCompanyPsqlImplement.Models } return $"DELETE FROM material WHERE material_id = {model.MaterialId} AND order_id = {model.OrderId}"; } + + public BsonDocument CreateBSON => + new BsonDocument + { + {"materialId", MaterialId - 1 }, + {"quantity", Quantity } + }; public MaterialOrderViewModel GetViewModel => new() { OrderId = OrderId, diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs index 1fcb42e..5d4349d 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs @@ -2,6 +2,7 @@ using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Enums; using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; @@ -80,6 +81,37 @@ namespace ConstructionCompanyPsqlImplement.Models } return $"DELETE FROM \"order\" WHERE id = {model.Id}"; } + + public BsonDocument CreateBSON => + new BsonDocument + { + {"_id", Id}, + {"description", $"{Description}"}, + {"adress", $"{Adress}"}, + {"price", Price}, + {"status", $"{Status}"}, + {"customerNumber", $"{CustomerNumber}"}, + {"dateBegin", new BsonDateTime(DateBegin)}, + {"dateEnd", DateEnd.HasValue ? new BsonDateTime(DateEnd.Value) : ""}, + {"employeesId", new BsonArray() }, + {"materials", new BsonArray()} + }; + + public BsonDocument UpdateBSON(BsonArray employeesId, BsonArray materials) => + new BsonDocument + { + {"_id", Id - 1}, + {"description", $"{Description}"}, + {"adress", $"{Adress}"}, + {"price", Price}, + {"status", $"{Status}"}, + {"customerNumber", $"{CustomerNumber}"}, + {"dateBegin", new BsonDateTime(DateBegin)}, + {"dateEnd", DateEnd.HasValue ? new BsonDateTime(DateEnd.Value) : ""}, + {"employeesId", employeesId }, + {"materials", materials} + }; + public OrderViewModel GetViewModel => new() { Id = Id, diff --git a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs index a142d8c..e3e09ca 100644 --- a/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs +++ b/ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Position.cs @@ -1,6 +1,7 @@ using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; @@ -69,6 +70,22 @@ namespace ConstructionCompanyPsqlImplement.Models return $"SELECT \"position\".id, AVG(material_order.quantiny) FROM \"position\"\r\nJOIN employee ON employee.position_id = \"position\".id\r\nJOIN employee_order ON employee_order.employee_id = employee.id\r\nJOIN \"order\" ON \"order\".id = employee_order.order_id\r\nJOIN material_order ON material_order.order_id = \"order\".id\r\nJOIN material ON material.id = material_order.material_id\r\nWHERE \"order\".date_begin >= '{dateFrom}' AND \"order\".date_begin <= '{dateTo}'\r\nGROUP BY \"position\".id"; } + public BsonDocument CreateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"PositionName", $"{PositionName}"}, + {"Salary", $"{Salary}" } + }; + + public BsonDocument UpdateBSON => + new BsonDocument + { + {"_id", Id - 1}, + {"PositionName", $"{PositionName}"}, + {"Salary", $"{Salary}" } + }; + public PositionViewModel GetViewModel => new() { Id = Id, diff --git a/ConstructionCompany/ConstructionCompanyView/FormLogin.cs b/ConstructionCompany/ConstructionCompanyView/FormLogin.cs index 40e2321..f41ae82 100644 --- a/ConstructionCompany/ConstructionCompanyView/FormLogin.cs +++ b/ConstructionCompany/ConstructionCompanyView/FormLogin.cs @@ -1,4 +1,5 @@ using ConstructionCompanyContracts.BusinessLogicContracts; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.ComponentModel; @@ -111,6 +112,37 @@ namespace ConstructionCompanyView ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase psqlDb = ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase.GetInstance(); ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase mongoDb = ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase.GetInstance(); mongoDb.flushDb(); + + var positions = psqlDb.Positions.Select(x => x.CreateBSON).ToList(); + foreach (var position in positions) + { + mongoDb.InsertDocument(position, "Positions"); + } + + var employees = psqlDb.Employees.Select(x => x.CreateBSON).ToList(); + foreach (var employee in employees) + { + mongoDb.InsertDocument(employee, "Employees"); + } + + var materials = psqlDb.Materials.Select(x => x.CreateBSON).ToList(); + foreach(var material in materials) + { + mongoDb.InsertDocument(material, "Materials"); + } + + var orders = psqlDb.Orders; + var materialOrders = psqlDb.MaterialOrders; + var employeeOrders = psqlDb.EmployeeOrders; + foreach (var order in orders) + { + var orderMaterials = materialOrders.Where(x => x.OrderId == order.Id).Select(x => x.CreateBSON).ToList(); + var orderEmployees = employeeOrders.Where(x => x.OrderId == order.Id).Select(x => x.EmployeeId - 1).ToList(); + var orderBSON = order.UpdateBSON(new BsonArray(orderEmployees), new BsonArray(orderMaterials)); + mongoDb.InsertDocument(orderBSON, "Orders"); + } + + MessageBox.Show("Бд синхронизрованы", "Готово!"); } } }