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 MaterialOrderStorage : IMaterialOrderStorage { private readonly ConstructionCompanyDatabase _source; public MaterialOrderStorage() { _source = ConstructionCompanyDatabase.GetInstance(); } public List GetFullList() { List result = new List(); foreach (var material in _source.MaterialOrders) { result.Add(material.GetViewModel); } return result; } public List GetFilteredList(MaterialOrderSearchModel model) { if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue) { return new(); } List result = new List(); foreach (var material in _source.MaterialOrders) { if (material.MaterialId == model.MaterialId && material.OrderId == model.OrderId) result.Add(material.GetViewModel); } return result; } public MaterialOrderViewModel? GetElement(MaterialOrderSearchModel model) { if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue) { return new(); } return _source.MaterialOrders.FirstOrDefault(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId)?.GetViewModel; } public MaterialOrderViewModel? Insert(MaterialOrderBindingModel model) { var order = _source.ReadDocument(new BsonDocument { { "_id", model.OrderId } }, "Orders"); var employeeIds = order[8].AsBsonArray; var materials = order[9].AsBsonArray; materials.Add(new BsonDocument {{"materialId", model.MaterialId}, {"quantity", model.Quantity}}); var orderView = _source.Orders.First(x => x.Id == model.OrderId); OrderBindingModel orderModel = new OrderBindingModel { Id = orderView.Id, Description = orderView.Description, Adress = orderView.Adress, Price = orderView.Price, Status = orderView.Status, CustomerNumber = orderView.CustomerNumber, DateBegin = orderView.DateBegin, DateEnd = orderView.DateEnd, }; var document = Order.UpdateBSON(orderModel, employeeIds, materials); if (document == null) { return null; } _source.ReplaceDocument(document, new BsonDocument { { "_id", model.OrderId } }, "Orders"); var newMaterialOrder = _source.MaterialOrders[_source.MaterialOrders.Count - 1]; return newMaterialOrder.GetViewModel; } public MaterialOrderViewModel? Delete(MaterialOrderBindingModel model) { var deletedMaterialOrder = _source.MaterialOrders.First(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId).GetViewModel; var order = _source.ReadDocument(new BsonDocument { { "_id", model.OrderId } }, "Orders"); var employeeIds = order[8].AsBsonArray; var materials = order[9].AsBsonArray; employeeIds.Remove(new BsonDocument {{"materialId", model.MaterialId}, {"quantity", model.Quantity}}); var orderView = _source.Orders.First(x => x.Id == model.OrderId); OrderBindingModel orderModel = new OrderBindingModel { Id = orderView.Id, Description = orderView.Description, Adress = orderView.Adress, Price = orderView.Price, Status = orderView.Status, CustomerNumber = orderView.CustomerNumber, DateBegin = orderView.DateBegin, DateEnd = orderView.DateEnd, }; var document = Order.UpdateBSON(orderModel, employeeIds, materials); if (document == null) { return null; } _source.ReplaceDocument(document, new BsonDocument { { "_id", model.OrderId } }, "Orders"); return deletedMaterialOrder; } } }