Added db sync feature

This commit is contained in:
abazov73 2023-05-11 13:03:42 +04:00
parent f1976a82d2
commit 7234654471
9 changed files with 133 additions and 11 deletions

View File

@ -79,12 +79,7 @@ namespace ConstructionCompanyMongoDBImplement.Implements
public PositionViewModel? Update(PositionBindingModel model) public PositionViewModel? Update(PositionBindingModel model)
{ {
List<int> ids = new List<int>(); var document = Position.UpdateBSON(model);
foreach (var employee in _source.Employees)
{
if (employee.PositionID == model.Id) ids.Add(employee.Id);
}
var document = Position.UpdateBSON(model, ids);
if (document == null) if (document == null)
{ {
return null; return null;

View File

@ -43,12 +43,11 @@ namespace ConstructionCompanyMongoDBImplement.Models
{ {
{"_id", model.Id}, {"_id", model.Id},
{"PositionName", $"{model.PositionName}"}, {"PositionName", $"{model.PositionName}"},
{"Salary", $"{model.Salary}" }, {"Salary", $"{model.Salary}" }
{"EmployeesId", new BsonArray{}}
}; };
} }
public static BsonDocument? UpdateBSON(PositionBindingModel? model, List<int> ids) public static BsonDocument? UpdateBSON(PositionBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -58,8 +57,7 @@ namespace ConstructionCompanyMongoDBImplement.Models
{ {
{"_id", model.Id}, {"_id", model.Id},
{"PositionName", $"{model.PositionName}"}, {"PositionName", $"{model.PositionName}"},
{"Salary", $"{model.Salary}" }, {"Salary", $"{model.Salary}" }
{"EmployeesId", new BsonArray(ids)}
}; };
} }
public PositionViewModel GetViewModel => new() public PositionViewModel GetViewModel => new()

View File

@ -13,6 +13,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MongoDB.Bson" Version="2.19.1" />
<PackageReference Include="Npgsql" Version="7.0.2" /> <PackageReference Include="Npgsql" Version="7.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="7.0.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="7.0.3" />

View File

@ -1,10 +1,12 @@
using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ConstructionCompanyPsqlImplement.Models namespace ConstructionCompanyPsqlImplement.Models
@ -72,5 +74,23 @@ namespace ConstructionCompanyPsqlImplement.Models
PositionID = PositionID, PositionID = PositionID,
PositionName = Position.PositionName 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}
};
} }
} }

View File

@ -1,6 +1,7 @@
using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -72,6 +73,24 @@ namespace ConstructionCompanyPsqlImplement.Models
MaterialName = model.MaterialName; MaterialName = model.MaterialName;
Quantity = model.Quantity; 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() public MaterialViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,

View File

@ -1,6 +1,7 @@
using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -47,6 +48,13 @@ namespace ConstructionCompanyPsqlImplement.Models
} }
return $"DELETE FROM material WHERE material_id = {model.MaterialId} AND order_id = {model.OrderId}"; 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() public MaterialOrderViewModel GetViewModel => new()
{ {
OrderId = OrderId, OrderId = OrderId,

View File

@ -2,6 +2,7 @@
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Enums; using ConstructionCompanyDataModels.Enums;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -80,6 +81,37 @@ namespace ConstructionCompanyPsqlImplement.Models
} }
return $"DELETE FROM \"order\" WHERE id = {model.Id}"; 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() public OrderViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,

View File

@ -1,6 +1,7 @@
using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; 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"; 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() public PositionViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,

View File

@ -1,4 +1,5 @@
using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.BusinessLogicContracts;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -111,6 +112,37 @@ namespace ConstructionCompanyView
ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase psqlDb = ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase.GetInstance(); ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase psqlDb = ConstructionCompanyPsqlImplement.ConstructionCompanyDatabase.GetInstance();
ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase mongoDb = ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase.GetInstance(); ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase mongoDb = ConstructionCompanyMongoDBImplement.ConstructionCompanyDatabase.GetInstance();
mongoDb.flushDb(); 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("Бд синхронизрованы", "Готово!");
} }
} }
} }