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); }
public static TemplatePageAreaPersistenceModel AsPersistenceModel(this TemplatePageArea domainModel) { return(new TemplatePageAreaPersistenceModel { Id = domainModel.Id, AreaName = domainModel.AreaName, DimensionX = domainModel.AreaDimension.X, DimensionY = domainModel.AreaDimension.Y, DimensionWidth = domainModel.AreaDimension.Width, DimensionHeight = domainModel.AreaDimension.Height, ExpectedData = (int)domainModel.ExpectedData, InterestPoints = domainModel.InterestPoints.Select(ip => ip.AsPersistenceModel()).ToList() }); }