Exemplo n.º 1
0
        public Attempt <IValueSet> Add(IValueSet valueSet)
        {
            if (!valueSet.IsCustom)
            {
                return(Attempt <IValueSet> .Failed(new ValueSetOperationException("Only custom Value Sets may be created or updated.")));
            }

            valueSet.SetIdsForCustomInsert();

            var valueSetDto = valueSet.AsDto();
            var codeDtos    = valueSet.ValueSetCodes.Select(code => code.AsDto());

            this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = false;
            using (var transaction = this.ClientTermContext.Database.BeginTransaction())
            {
                try
                {
                    this.ClientTermContext.ValueSetDescriptions.Add(valueSetDto);
                    this.ClientTermContext.ValueSetCodes.AddRange(codeDtos);
                    this.ClientTermContext.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    this.Logger.Error(ex, "Failed to save a custom ValueSet");
                    this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = true;
                    return(Attempt <IValueSet> .Failed(
                               new ValueSetOperationException("Failed to save a custom ValueSet", ex),
                               valueSet));
                }
                finally
                {
                    this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = true;
                }
            }

            // Get the updated ValueSet
            // TODO remove IsTest parameter
            var added = !this.IsTest ?
                        this.GetValueSet(valueSetDto.ValueSetUniqueID, Enumerable.Empty <string>()) :
                        this.GetCustomValueSet(valueSetDto.ValueSetUniqueID);

            return(!added.HasValue ?
                   Attempt <IValueSet> .Failed(new ValueSetNotFoundException("Could not retrieved newly saved ValueSet")) :
                   Attempt <IValueSet> .Successful(added.Single()));
        }