public (ICollection <SourceLiteModel> sources, ICollection <SourceItemLiteModel> items) Execute() { var roots = new List <SourceLiteModel>(); var items = new List <SourceItemLiteModel>(); roots.AddRange(SourceLiteModel.Load(this.Sources)); foreach (var source in this.Sources) { source.SourceItems = this.SourceItems.Where(c => c.SourceId == source.Id).ToList(); items.AddRange(SourceItemLiteModel.Loads(source.Name, source.SourceItems)); } return(roots, items); }
public async Task <IEnumerable <string> > ImportsItems(int productId, MemoryStream input) { var createdOn = this._datetimeGateway.GetCurrentDateTime(); var createdBy = this._identityGateway.GetIdentity(); var product = await this._dbContext.Products.Include(c => c.Customer) .Where(c => c.Id == productId).SingleAsync(); var logs = new List <string>(); var sources = this._dbContext.Sources.Where(c => c.ProductId == productId).ToList(); var productInstance = await this._dbContext.Products.Include(c => c.Customer) .Where(e => e.Id == productId).SingleAsync(); using (var package = new ExcelPackage(input)) { var sourceSheet = package.Workbook.Worksheets["Sources"]; var sourcesInstances = SourceLiteModel.Build(productInstance, createdOn, createdBy, new Builders.SheetRowAdapter(sourceSheet)); foreach (var item in sourcesInstances) { if (!sources.Exists(c => c.Name == item.Name)) { this._dbContext.Sources.Add(item); } } await this._dbContext.SaveChangesAsync(); productInstance.Sources = new SourceCollection(await this._dbContext.Sources.Where(c => c.ProductId == productId).ToListAsync()); var sourceItemsSheet = package.Workbook.Worksheets["SourceItems"]; var sourceItems = SourceItemLiteModel.Build(productInstance, createdOn, createdBy, new SheetRowAdapter(sourceItemsSheet)); await this._sourceItemComponent.BulkInsert(sourceItems); } return(logs); }