Example #1
0
        private static IReadOnlyCollection <FieldWithPreviousAndNewValue> SaveCharacterFieldsImpl(
            int currentUserId,
            [CanBeNull]
            Character character,
            [CanBeNull]
            Claim claim,
            [NotNull]
            IReadOnlyDictionary <int, string> newFieldValue,
            IFieldDefaultValueGenerator generator)
        {
            if (newFieldValue == null)
            {
                throw new ArgumentNullException(nameof(newFieldValue));
            }

            FieldSaveStrategyBase strategy;

            if (claim == null)
            {
                strategy =
                    new SaveToCharacterOnlyStrategy(null, character, currentUserId, generator);
            }
            else if (!claim.IsApproved)
            {
                strategy = new SaveToClaimOnlyStrategy(claim, null, currentUserId, generator);
            }
            else
            {
                strategy =
                    new SaveToCharacterAndClaimStrategy(claim, character, currentUserId, generator);
            }

            var fields = strategy.LoadFields();

            foreach (var keyValuePair in newFieldValue)
            {
                var field = fields[keyValuePair.Key];

                strategy.EnsureEditAccess(field);

                var normalizedValue = NormalizeValueBeforeAssign(field, keyValuePair.Value);

                strategy.AssignFieldValue(field, normalizedValue);
            }

            foreach (var field in fields.Values.Where(
                         f => !f.HasEditableValue && f.Field.CanHaveValue() &&
                         f.Field.IsAvailableForTarget(character)))
            {
                var newValue = strategy.GenerateDefaultValue(field);

                var normalizedValue = NormalizeValueBeforeAssign(field, newValue);

                strategy.AssignFieldValue(field, normalizedValue);
            }

            strategy.Save(fields);
            return(strategy.GetUpdatedFields());
        }
Example #2
0
        private static void SaveCharacterFieldsImpl(
            int currentUserId,
            [CanBeNull] Character character,
            [CanBeNull] Claim claim,
            [NotNull] IDictionary <int, string> newFieldValue)
        {
            if (newFieldValue == null)
            {
                throw new ArgumentNullException(nameof(newFieldValue));
            }

            FieldSaveStrategyBase strategy;

            if (claim == null)
            {
                strategy = new SaveToCharacterOnlyStrategy(null, character, currentUserId);
            }
            else if (!claim.IsApproved)
            {
                strategy = new SaveToClaimOnlyStrategy(claim, null, currentUserId);
            }
            else
            {
                strategy = new SaveToCharacterAndClaimStrategy(claim, character, currentUserId);
            }

            var fields = strategy.LoadFields();



            foreach (var keyValuePair in newFieldValue)
            {
                var field = fields[keyValuePair.Key];

                strategy.EnsureEditAccess(field);

                var normalizedValue = NormalizeValueBeforeAssign(field, keyValuePair.Value);
                if (field.Value != normalizedValue)
                {
                    field.Value = normalizedValue;

                    field.MarkUsed();
                }
            }

            strategy.Save(fields);
        }
Example #3
0
        private static FieldSaveStrategyBase CreateStrategy(int currentUserId, Character character,
                                                            Claim claim, IFieldDefaultValueGenerator generator)
        {
            FieldSaveStrategyBase strategy;

            if (claim == null)
            {
                strategy =
                    new SaveToCharacterOnlyStrategy(null, character, currentUserId, generator);
            }
            else if (!claim.IsApproved)
            {
                strategy = new SaveToClaimOnlyStrategy(claim, null, currentUserId, generator);
            }
            else
            {
                strategy =
                    new SaveToCharacterAndClaimStrategy(claim, character, currentUserId, generator);
            }

            return(strategy);
        }