protected override SalesPackage CreateFromResource(SalesPackageResource resource) { var salesPackage = new SalesPackage { Description = resource.Description, SalesPackageId = resource.SalesPackageId }; foreach (var salesPackageElementResource in resource.Elements) { var newElement = new SalesPackageElement { ElementType = salesPackageElementResource.ElementType, Quantity = salesPackageElementResource.Quantity, SalesPackageId = salesPackage.SalesPackageId, Sequence = salesPackageElementResource.Sequence }; salesPackage.Elements.Add(new SalesPackageElementJoin { BridgeId = salesPackage.Id, Id = this.linnappsDatabaseService.GetIdSequence("SPEJ_SEQ"), SalesPackageId = salesPackage.SalesPackageId, ElementType = salesPackageElementResource.ElementType, SalesPackage = salesPackage, SalesPackageElement = newElement }); } return(salesPackage); }
protected override void UpdateFromResource(SalesPackage salesPackage, SalesPackageResource resource) { salesPackage.Description = resource.Description; foreach (var salesPackageElementResource in resource.Elements) { var element = salesPackage.Elements.FirstOrDefault(a => a.ElementType == salesPackageElementResource.ElementType); if (element != null) { element.SalesPackageElement.Quantity = salesPackageElementResource.Quantity; element.SalesPackageElement.Sequence = salesPackageElementResource.Sequence; } else { var newElement = new SalesPackageElement { ElementType = salesPackageElementResource.ElementType, Quantity = salesPackageElementResource.Quantity, SalesPackageId = salesPackage.SalesPackageId, Sequence = salesPackageElementResource.Sequence }; salesPackage.Elements.Add(new SalesPackageElementJoin { BridgeId = salesPackage.Id, Id = this.linnappsDatabaseService.GetIdSequence("SPEJ_SEQ"), SalesPackageId = salesPackage.SalesPackageId, ElementType = salesPackageElementResource.ElementType, SalesPackage = salesPackage, SalesPackageElement = newElement }); } } var toBeRemoved = salesPackage.Elements .Except(salesPackage.Elements.Where(e => resource.Elements.Select(r => r.ElementType).Contains(e.ElementType))).ToList(); foreach (var salesPackageElementJoin in toBeRemoved) { salesPackage.Elements.Remove(salesPackageElementJoin); } }