194 lines
7.5 KiB
C#
194 lines
7.5 KiB
C#
|
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<Material> _materials = new List<Material>();
|
|||
|
private List<Employee> _employees = new List<Employee>();
|
|||
|
private List<Position> _positions = new List<Position>();
|
|||
|
private List<Order> _orders = new List<Order>();
|
|||
|
private List<EmployeeOrder> _employeeOrders = new List<EmployeeOrder>();
|
|||
|
private List<MaterialOrder> _materialOrders = new List<MaterialOrder>();
|
|||
|
public List<Material> Materials
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
refreshDb();
|
|||
|
return _materials;
|
|||
|
}
|
|||
|
}
|
|||
|
public List<Employee> Employees
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
refreshDb();
|
|||
|
return _employees;
|
|||
|
}
|
|||
|
}
|
|||
|
public List<Position> Positions
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
refreshDb();
|
|||
|
return _positions;
|
|||
|
}
|
|||
|
}
|
|||
|
public List<Order> Orders
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
refreshDb();
|
|||
|
return _orders;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<EmployeeOrder> EmployeeOrders
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
refreshDb();
|
|||
|
return _employeeOrders;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<MaterialOrder> 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<BsonDocument>(collectionName);
|
|||
|
collection.InsertOne(bson);
|
|||
|
refreshDb();
|
|||
|
}
|
|||
|
|
|||
|
public void ReplaceDocument(BsonDocument bson, BsonDocument filter, string collectionName)
|
|||
|
{
|
|||
|
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(collectionName);
|
|||
|
collection.ReplaceOne(filter, bson);
|
|||
|
refreshDb();
|
|||
|
}
|
|||
|
|
|||
|
public void DeleteDocument(BsonDocument filter, string collectionName)
|
|||
|
{
|
|||
|
var collection = client.GetDatabase(databaseName).GetCollection<BsonDocument>(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<BsonDocument> positions = database.GetCollection<BsonDocument>("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<BsonDocument> materials = database.GetCollection<BsonDocument>("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<BsonDocument> employees = database.GetCollection<BsonDocument>("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<BsonDocument> orders = database.GetCollection<BsonDocument>("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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|