示例#1
0
        /// <summary>
        /// Saves changes to a notifier
        /// </summary>
        /// <param name="id">ID of changed notifier</param>
        /// <param name="jsonBody">JSON string with notifier properties and values</param>
        public async Task <int> ChangeNotifierAsync(string id, string jsonBody)
        {
            // Find notifier by Id
            BaseNotifierData notifier = GetNotifierById(id);

            if (notifier == null)
            {
                throw new KeyNotFoundException("Notifier not found");
            }

            // Apply changes
            await notifier.ApplyJsonPropertiesAsync(jsonBody);

            // Save changes
            return(await _dbContext.SaveChangesAsync());
        }
        /// <summary>
        /// Adds a new entry to the history of notifications
        /// </summary>
        /// <param name="history">History data</param>
        public async Task <int> AddHistoryForTypeAsync(Type dataType)
        {
            // Extract notifier name from data type
            var history = new NotificationHistory(
                dataType
                .GetCustomAttributes(false)
                .Where(a => a is NotifierAttribute)
                .Cast <NotifierAttribute>()
                .Select(a => a.Name)
                .SingleOrDefault());

            // Add new history entry
            await _dbContext.NotificationHistory.AddAsync(history);

            // save
            await _dbContext.SaveChangesAsync();

            return(history.Id);
        }
示例#3
0
        /// <summary>
        /// Saves new application settings
        /// </summary>
        /// <param name="baseUrl">Web page base URL</param>
        /// <param name="allowRegistration">Allow user registration</param>
        /// <param name="htmlMessageTemplate">HTML message template text</param>
        public async Task <int> SaveSettingsAsync(string baseUrl, bool allowRegistration, string htmlMessageTemplate)
        {
            // AppSettings has only one record
            var appSettings = await _dbContext.AppSettings.FirstOrDefaultAsync();

            if (appSettings == null)
            {
                // Create first record as part of page setup
                appSettings = new AppSettings(baseUrl);
                await _dbContext.AppSettings.AddAsync(appSettings);
            }

            // Update record
            appSettings.BaseUrl             = baseUrl;
            appSettings.AllowRegistration   = allowRegistration;
            appSettings.HtmlMessageTemplate = htmlMessageTemplate;

            // Save
            return(await _dbContext.SaveChangesAsync());
        }
示例#4
0
        /// <summary>
        /// Removes a user from all rules where he is a recipient
        /// </summary>
        /// <param name="user">User to remove</param>
        public async Task DeleteUserRecipientAsync(User user)
        {
            // Find all roles where the user is a recipient
            var rules = _dbContext.Rules
                        .ToList()
                        .Select(rule => new { Rule = rule, Recipient = rule.Recipients.FirstOrDefault(recipient => recipient.User == user) })
                        .Where(rule => rule.Recipient != null)
                        .ToList();

            // First remove the user from recipient entry (because foreign-key-constraint)
            rules.ForEach(rule => rule.Recipient.User = null);
            await _dbContext.SaveChangesAsync();

            // Then remove the recipient entries
            rules.ForEach(rule => rule.Rule.Recipients.Remove(rule.Recipient));
            await _dbContext.SaveChangesAsync();
        }
示例#5
0
        /// <summary>
        /// Saves a new trigger invocation to the history
        /// </summary>
        /// <param name="call">Trigger call information</param>
        public async Task StoreTriggerCallAsync(TriggerCall call)
        {
            // remove old values
            _dbContext.TriggerVariables.RemoveRange(_dbContext.TriggerVariables.Where(v => v.Trigger == call.Type));

            // prepare new values
            List <TriggerVariable> variables =
                new List <TriggerVariable>(
                    call.EnvironmentVars.Select(var => new TriggerVariable(call.Type, var.Key, var.Value))
                    );

            // add new values
            await _dbContext.TriggerHistory.AddAsync(TriggerHistory.From(call));

            await _dbContext.TriggerVariables.AddRangeAsync(variables);

            // save
            await _dbContext.SaveChangesAsync();
        }