Exemple #1
0
        public static bool Contains(this AttachmentSettings settings, AttachmentSetting comparer)
        {
            if (settings == null)
            {
                return(false);
            }

            foreach (var setting in settings.Settings)
            {
                if (setting.RoleId == comparer.RoleId)
                {
                    return(true);
                }
            }

            return(false);
        }
        public static async Task <AttachmentSettings> SaveAsync(this IAttachmentSettingsStore <AttachmentSettings> store, AttachmentSetting model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (model.RoleId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(model.RoleId));
            }

            var output   = new List <AttachmentSetting>();
            var settings = await store.GetAsync();

            if (settings?.Settings != null)
            {
                if (settings.Contains(model))
                {
                    foreach (var setting in settings.Settings)
                    {
                        if (setting.RoleId == model.RoleId)
                        {
                            output.Add(model);
                        }
                        else
                        {
                            output.Add(setting);
                        }
                    }
                }
                else
                {
                    foreach (var setting in settings.Settings)
                    {
                        output.Add(setting);
                    }
                    output.Add(model);
                }
            }
            else
            {
                output.Add(model);
            }

            return(await store.SaveAsync(new AttachmentSettings()
            {
                Settings = output
            }));
        }