public static TemplatePagePersistenceModel AsPersistenceModel(this TemplatePageDefinition domainModel)
 {
     return(new TemplatePagePersistenceModel
     {
         Id = domainModel.Id,
         PageNumber = domainModel.PageNumber,
         ReferenceCanvas = domainModel.ReferenceCanvas.AsPersistenceModel(),
         DefinedAreas = domainModel.DefinedAreas
                        .OfType <TemplatePageArea>()
                        .Select(da => da.AsPersistenceModel())
                        .ToList()
     });
 }
        public static TemplatePageDefinition AsDomainModel(this TemplatePagePersistenceModel dbModel)
        {
            var page = new TemplatePageDefinition(dbModel.PageNumber, dbModel.Id);

            page.ModifyReferenceCanvas(dbModel.ReferenceCanvas.AsDomainModel().CanvasDimensions);

            foreach (var pageArea in dbModel.DefinedAreas.Select(da => da.AsDomainModel()))
            {
                page.DefineArea(pageArea);
            }

            return(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));
            }
        }