public OperationResult <TemplatePageDefinition> Handle(AddTemplatePageAreas command) { ThrowOnInvalidInput(command); using (var context = new TemplatingContext(_persistenceConfiguration)) { var pageFromDb = context.Pages .Include(p => p.ReferenceCanvas) .Include(p => p.DefinedAreas) .SingleOrDefault(p => p.Id == command.TemplatePageId); var page = pageFromDb?.AsDomainModel(); if (page == null) { return(OperationResult <TemplatePageDefinition> .Failure( new ObjectNotFoundById(typeof(TemplatePageDefinition), command.TemplatePageId))); } foreach (var area in command.Areas) { var newArea = new TemplatePageArea( new Rectangle(area.DimensionX, area.DimensionY, area.DimensionWidth, area.DimensionHeight), area.AreaName, area.ExpectedData); foreach (var areaPart in area.AreaParts) { var addAreaPartResult = newArea.DefinePointOfInterest( new Rectangle(areaPart.DimensionX, areaPart.DimensionY, areaPart.DimensionWidth, areaPart.DimensionHeight), (uint)areaPart.OrderInArea); if (!addAreaPartResult.Successful) { return(OperationResult <TemplatePageDefinition> .Failure(addAreaPartResult.Details)); } } var addPageResult = page.DefineArea(newArea); if (!addPageResult.Successful) { return(OperationResult <TemplatePageDefinition> .Failure(addPageResult.Details)); } pageFromDb.DefinedAreas.Add(newArea.AsPersistenceModel()); } context.Update(pageFromDb); context.SaveChanges(); return(OperationResult <TemplatePageDefinition> .Success(page)); } }
public OperationResult <TemplateDefinition> Handle(AddTemplateDefinitionPage command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } using (var context = new TemplatingContext(_persistenceConfiguration)) { var templateFromDb = context.Templates .Include(t => t.DefinedPages) .SingleOrDefault(t => t.Id == command.TemplateId); var template = templateFromDb?.AsDomainModel(); if (template == null) { return(OperationResult <TemplateDefinition> .Failure( new ObjectNotFoundById(typeof(TemplateDefinition), command.TemplateId))); } var templatePage = new TemplatePageDefinition((int)command.PageNumber); using (var bitmap = command.ReferenceCanvas.FileData.AsBitmap()) { var modificationResult = templatePage .ModifyReferenceCanvas(new Rectangle(0, 0, bitmap.Width, bitmap.Height)); if (!modificationResult.Successful) { return(OperationResult <TemplateDefinition> .Failure(modificationResult.Details)); } } var addPageResult = template.AddPageDefinition(templatePage); if (!addPageResult.Successful) { return(OperationResult <TemplateDefinition> .Failure(addPageResult.Details)); } templateFromDb.DefinedPages.Add(templatePage.AsPersistenceModel()); context.Update(templateFromDb); context.SaveChanges(); var saveBitmapCommand = new SaveBitmapForTemplatePageCanvasDefinition { FileData = command.ReferenceCanvas.FileData, FileLabel = command.ReferenceCanvas.FileLabel, FileType = command.ReferenceCanvas.FileType, TemplatePageId = templatePage.Id, TemplateId = template.Id }; var savedCanvas = _saveCanvasHandler.Handle(saveBitmapCommand); if (!savedCanvas.Success) { return(OperationResult <TemplateDefinition> .Failure(new FileStorageSaveFailed(saveBitmapCommand))); } return(OperationResult <TemplateDefinition> .Success(template)); } }