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 EmployeeOrderStorage : IEmployeeOrderStorage { private readonly ConstructionCompanyDatabase _source; public EmployeeOrderStorage() { _source = ConstructionCompanyDatabase.GetInstance(); } public List GetFullList() { List result = new List(); foreach (var material in _source.EmployeeOrders) { result.Add(material.GetViewModel); } return result; } public List GetFilteredList(EmployeeOrderSearchModel model) { if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue) { return new(); } List result = new List(); foreach (var material in _source.EmployeeOrders) { if (material.EmployeeId == model.EmployeeId && material.OrderId == model.OrderId) result.Add(material.GetViewModel); } return result; } public EmployeeOrderViewModel? GetElement(EmployeeOrderSearchModel model) { if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue) { return new(); } return _source.EmployeeOrders.FirstOrDefault(x => x.EmployeeId == model.EmployeeId && x.OrderId == model.OrderId)?.GetViewModel; } public EmployeeOrderViewModel? Insert(EmployeeOrderBindingModel model) { var order = _source.ReadDocument(new BsonDocument { { "_id", model.OrderId } }, "Orders"); var employeeIds = order[8].AsBsonArray; var materials = order[9].AsBsonArray; employeeIds.Add(model.EmployeeId); 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 newEmployeeOrder = _source.EmployeeOrders[_source.EmployeeOrders.Count - 1]; return newEmployeeOrder.GetViewModel; } public EmployeeOrderViewModel? Delete(EmployeeOrderBindingModel model) { var deletedEmployeeOrder = _source.EmployeeOrders.First(x => x.EmployeeId == model.EmployeeId && 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(model.EmployeeId); 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 deletedEmployeeOrder; } } }