Models for MongoDB + MongoDB database singleton

This commit is contained in:
abazov73 2023-05-09 13:13:47 +04:00
parent 51947e29e9
commit d8186eecc0
8 changed files with 480 additions and 3 deletions

View File

@ -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<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);
}
}
}
}
}

View File

@ -1,4 +1,6 @@
using ConstructionCompanyDataModels.Models; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -14,5 +16,48 @@ namespace ConstructionCompanyMongoDBImplement.Models
public int Id { get; private set; } public int Id { get; private set; }
public int PositionID { get; private set; } public int PositionID { get; private set; }
public Position Position { get; set; } = new(); public Position Position { get; set; } = new();
public static Employee? Create(EmployeeBindingModel? model, List<Position> 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}" }
};
}
} }
} }

View File

@ -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<Employee> employees, List<Order> 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
};
}
}

View File

@ -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}" }
};
}
}
}

View File

@ -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<Material> materials, List<Order> 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
};
}
}

View File

@ -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}
};
}
}
}

View File

@ -1,4 +1,5 @@
using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models; using ConstructionCompanyDataModels.Models;
using MongoDB.Bson; using MongoDB.Bson;
using System; using System;
@ -40,6 +41,7 @@ namespace ConstructionCompanyMongoDBImplement.Models
} }
return new BsonDocument return new BsonDocument
{ {
{"_id", model.Id},
{"PositionName", $"{model.PositionName}"}, {"PositionName", $"{model.PositionName}"},
{"Salary", $"{model.Salary}" }, {"Salary", $"{model.Salary}" },
{"EmployeesId", new BsonArray{}} {"EmployeesId", new BsonArray{}}
@ -54,10 +56,17 @@ namespace ConstructionCompanyMongoDBImplement.Models
} }
return new BsonDocument return new BsonDocument
{ {
{"_id", model.Id},
{"PositionName", $"{model.PositionName}"}, {"PositionName", $"{model.PositionName}"},
{"Salary", $"{model.Salary}" }, {"Salary", $"{model.Salary}" },
{"EmployeesId", new BsonArray(ids)} {"EmployeesId", new BsonArray(ids)}
}; };
} }
public PositionViewModel GetViewModel => new()
{
Id = Id,
PositionName = PositionName,
Salary = Salary
};
} }
} }

View File

@ -17,8 +17,6 @@ namespace ConstructionCompanyPsqlImplement.Models
public string Adress { get; private set; } = string.Empty; public string Adress { get; private set; } = string.Empty;
public string CustomerNumber { get; private set; } = string.Empty; public string CustomerNumber { get; private set; } = string.Empty;
public double Price { get; private set; } public double Price { get; private set; }
public Dictionary<int, IOrderModel> OrderEmolyees { get; private set; } = new Dictionary<int, IOrderModel>();
public Dictionary<int, (IMaterialModel, int)> OrderMaterials { get; private set; } = new Dictionary<int, (IMaterialModel, int)>();
public OrderStatus Status { get; private set; } public OrderStatus Status { get; private set; }