Exemple #1
0
        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 static TemplatePageArea AsDomainModel(this TemplatePageAreaPersistenceModel dbModel)
        {
            var area = new TemplatePageArea(
                new Rectangle(dbModel.DimensionX, dbModel.DimensionY, dbModel.DimensionWidth, dbModel.DimensionHeight),
                dbModel.AreaName, (TemplatePartExpectedDataType)dbModel.ExpectedData, dbModel.Id);

            foreach (var areaPart in dbModel.InterestPoints.Select(part => part.AsDomainModel()))
            {
                area.DefinePointOfInterest(areaPart.Dimension, areaPart.OrderInArea);
            }

            return(area);
        }