PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/MaterialStorage.cs

126 lines
4.6 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyMongoDBImplement.Models;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyMongoDBImplement.Implements
{
public class MaterialStorage : IMaterialStorage
{
private readonly ConstructionCompanyDatabase _source;
public MaterialStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<MaterialViewModel> GetFullList()
{
List<MaterialViewModel> result = new List<MaterialViewModel>();
foreach (var material in _source.Materials)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<MaterialViewModel> GetFilteredList(MaterialSearchModel model)
{
if (model == null || !model.Id.HasValue || string.IsNullOrEmpty(model.MaterialName))
{
return new();
}
List<MaterialViewModel> result = new List<MaterialViewModel>();
if (!string.IsNullOrEmpty(model.MaterialName))
{
foreach (var material in _source.Materials)
{
if (material.MaterialName.Equals(model.MaterialName)) result.Add(material.GetViewModel);
}
return result;
}
else
{
foreach (var material in _source.Materials)
{
if (material.Id == model.Id) result.Add(material.GetViewModel);
}
return result;
}
}
public MaterialViewModel? GetElement(MaterialSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
return _source.Materials.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public MaterialViewModel? Insert(MaterialBindingModel model)
{
model.Id = _source.Materials.Count > 0 ? _source.Materials.Max(x => x.Id) + 1 : 0;
var document = Material.CreateBSON(model);
if (document == null)
{
return null;
}
_source.InsertDocument(document, "Materials");
var newMaterial = _source.Materials[_source.Materials.Count - 1];
return newMaterial.GetViewModel;
}
public MaterialViewModel? Update(MaterialBindingModel model)
{
var document = Material.UpdateBSON(model);
if (document == null)
{
return null;
}
_source.ReplaceDocument(document, new BsonDocument { { "_id", model.Id } }, "Materials");
var updatedMaterial = _source.Materials.First(x => x.Id == model.Id);
return updatedMaterial.GetViewModel;
}
public MaterialViewModel? Delete(MaterialBindingModel model)
{
var deletedMaterial = _source.Materials.First(x => x.Id == model.Id).GetViewModel;
_source.DeleteDocument(new BsonDocument { { "_id", model.Id } }, "Materials");
return deletedMaterial;
}
public List<EmployeeViewModel>? GetEmployeesUsingMaterial(MaterialBindingModel model)
{
if (model == null)
{
return null;
}
//List<Employee> employeesList = _source.Employees;
//BsonDocument filter = new BsonDocument { { "materials.materialId", new BsonDocument("$eq", model.Id) } };
//List<BsonDocument> fiteredOrders = _source.FilterDocuments(filter, "Orders");
//List<EmployeeViewModel> employees = new List<EmployeeViewModel>();
//foreach (var order in fiteredOrders)
//{
// var orderEmployees = order[8].AsBsonArray;
// foreach (var employee in orderEmployees)
// {
// var emp = employees.FirstOrDefault(x => x.Id == employee.ToInt32());
// if (emp == null)
// {
// employees.Add(employeesList.First(x => x.Id == employee.ToInt32()).GetViewModel);
// }
// }
//}
var employees = _source.reportMaterials(model.Id);
return employees;
}
}
}