Exemple #1
0
        public IViewModel NewArea(Account account, NewAreaViewModel model)
        {
            var action   = (ActionTypeEnum)model.ActionId;
            var reaction = (ReactionTypeEnum)model.ReactionId;

            if (account.Triggers.Where(t => t.ActionType == action && t.ReactionType == reaction).Any())
            {
                return new ErrorViewModel()
                       {
                           Error = "AREA déjà existant, impossible de le créé"
                       }
            }
            ;
            Console.WriteLine("AreaService(NewArea): Creating new AREA...");
            var trigger = new Trigger()
            {
                ActionType   = action,
                ReactionType = reaction
            };

            Console.WriteLine("AreaService(NewArea): Saving new AREA...");
            _context.Add(trigger);
            account.Triggers.Add(trigger);
            _context.Update(account);
            _context.SaveChanges();
            Console.WriteLine("AreaService(NewArea): Creating the AREA template...");
            _triggerFactory.CreateTriggerTemplate(trigger);
            Console.WriteLine("AreaService(NewArea): AREA created");
            return(new SuccessViewModel());
        }
 private void ExecuteCore()
 {
     Console.WriteLine("BackgroundJob: Starting...");
     while (true)
     {
         try
         {
             Thread.Sleep(2000);
             var accounts = _context.Accounts.Include(t => t.Tokens).Include(t => t.Triggers).Where(t => t.Triggers.Count > 0);
             foreach (var account in accounts)
             {
                 Console.WriteLine("Checking triggers for account " + account.UserName);
                 foreach (var trigger in account.Triggers)
                 {
                     Console.WriteLine("Checking trigger...");
                     if (trigger.LastVerificationDate == null)
                     {
                         trigger.LastVerificationDate = DateTime.Now;
                     }
                     _triggerFactory.CreateTriggerTemplate(trigger);
                     trigger.Template.TryActivate(account, string.Empty, (DateTime)trigger.LastVerificationDate);
                     trigger.LastVerificationDate = trigger.Template.GetTriggerDate();
                     _context.Update(trigger);
                 }
             }
             try {
                 _context.SaveChanges();
             }catch (Exception) {
             }
         }catch (Exception e)
         {
             Console.WriteLine($"BackgroundJob: Stoping...\n{e.StackTrace}\n{e.Message}");
             break;
         }
     }
     Console.WriteLine("BackgroundJob: Stoped");
 }