public async Task <ActionResult> DeleteConfirmed(string email)
        {
            if (!User.IsInRole("Admin") || email == "*****@*****.**")
            {
                return(RedirectToAction("Index", "Home"));
            }

            var user = await dbContext.Users.FirstOrDefaultAsync(x => x.Email == email);

            var smartJobs = await dbContext.SmartJobs.Include("CreatedBy").Where(x => x.CreatedBy.Id == user.Id).ToListAsync();

            var events = await dbContext.JobEvents.Include("SmartJob").Include("SmartJob.CreatedBy").Where(x => x.SmartJob.CreatedBy.Id == user.Id).ToListAsync();

            foreach (var smartJob in smartJobs)
            {
                await QuartzTasks.DeleteJob(smartJob.ID, false);
            }

            dbContext.JobEvents.RemoveRange(events);
            dbContext.SmartJobs.RemoveRange(smartJobs);
            dbContext.Users.Remove(user);

            await dbContext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Beispiel #2
0
 protected void Application_Start()
 {
     AreaRegistration.RegisterAllAreas();
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     QuartzTasks.Initialise();
 }
Beispiel #3
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            var smartJob = await dbContext.SmartJobs.FirstOrDefaultAsync(x => x.ID == id);

            var events = await dbContext.JobEvents.Include("SmartJob").Where(x => x.SmartJob.ID == id).ToListAsync();

            dbContext.JobEvents.RemoveRange(events);
            dbContext.SmartJobs.Remove(smartJob);

            await dbContext.SaveChangesAsync();

            await QuartzTasks.DeleteJob(id, false);

            return(RedirectToAction("Index"));
        }
        public async Task <JsonResult> AddNewTask(NewTaskModel model)
        {
            var result = new ApiJsonResult();

            try
            {
                using (var dbContext = new DatabaseContext())
                {
                    var user = dbContext.Users.FirstOrDefault(x => x.ApiKey == model.ApiKey);
                    if (user == null)
                    {
                        result.IsSuccess = false;
                        result.Message   = "The key value is wrong";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(model.Name))
                        {
                            result.IsSuccess = false;
                            result.Message   = "The name value is required";
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(model.Network))
                            {
                                result.IsSuccess = false;
                                result.Message   = "The network value is required";
                            }
                            else
                            {
                                if (model.Network != "testnet-r4_2" && model.Network != "DevsDappsTestnet" && model.Network != "CreditsNetwork")
                                {
                                    result.IsSuccess = false;
                                    result.Message   = "The network value is wrong. Supported values: 'testnet-r4_2', 'DevsDappsTestnet', 'CreditsNetwork'";
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(model.Method))
                                    {
                                        result.IsSuccess = false;
                                        result.Message   = "The method value is required";
                                    }
                                    else
                                    {
                                        if (string.IsNullOrEmpty(model.Address))
                                        {
                                            result.IsSuccess = false;
                                            result.Message   = "The address value is required";
                                        }
                                        else
                                        {
                                            if (string.IsNullOrEmpty(model.ExecutionMode))
                                            {
                                                result.IsSuccess = false;
                                                result.Message   = "The executionMode value is required";
                                            }
                                            else
                                            {
                                                if (model.ExecutionMode == "Regular")
                                                {
                                                    if (CheckDate(model.RegularDateFrom) == DateTime.MinValue)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The regularDateFrom value is wrong. Supported mask: mm-DD-YYYY-hh-MM-ss";
                                                    }
                                                    else if (CheckDate(model.RegularDateTo) == DateTime.MinValue)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The regularDateTo value is wrong. Supported mask: mm-DD-YYYY-hh-MM-ss";
                                                    }
                                                    else if (CheckPeriodName(model.RegularPeriod) == 0)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The regularPeriod value is wrong. Supported values: 'Minutes', 'Hours', 'Days'";
                                                    }
                                                    else if (CheckPeriodValue(model.RegularValue) == 0)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The regularValue value is wrong. Supported values: 1, 2, 3, and so on...";
                                                    }
                                                }
                                                else if (model.ExecutionMode == "Once")
                                                {
                                                    if (CheckDate(model.OnceDate) == DateTime.MinValue)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The onceDate value is wrong. Supported mask: mm-DD-YYYY-hh-MM-ss";
                                                    }
                                                }
                                                else if (model.ExecutionMode == "InSeconds")
                                                {
                                                    if (CheckInSeconds(model.InSecondsValue) == false)
                                                    {
                                                        result.IsSuccess = false;
                                                        result.Message   = "The InSecondsValue value is wrong. Value must be integer value";
                                                    }
                                                    else
                                                    {
                                                        var seconds       = Convert.ToInt32(model.InSecondsValue);
                                                        var dateExecution = DateTime.Now.AddSeconds(seconds);
                                                        model.OnceDate = dateExecution.ToString("MM-dd-yyyy-HH-mm-ss");
                                                    }
                                                }
                                                else if (model.ExecutionMode == "CronExpression")
                                                {
                                                }
                                                else
                                                {
                                                    result.IsSuccess = false;
                                                    result.Message   = "The executionMode value is wrong. Supported values: 'Regular', 'Once', 'CronExpression'";
                                                }

                                                //Ошибок нет, создаем новую задачу
                                                if (!result.IsSuccess)
                                                {
                                                    var smartJob = new SmartJob();
                                                    smartJob           = new SmartJob();
                                                    smartJob.CreatedBy = user;
                                                    smartJob.CreatedAt = DateTime.Now;
                                                    smartJob.Rule      = new Rule();
                                                    smartJob.Events    = new List <JobEvent>();
                                                    smartJob.Name      = model.Name;
                                                    smartJob.IsActive  = true;
                                                    smartJob.Method    = model.Method;
                                                    smartJob.Address   = model.Address;
                                                    smartJob.IsDeleted = false;
                                                    smartJob.DeleteTaskAfterExecution = model.DeleteTaskAfterExecution == "1";
                                                    smartJob.CreditsNet           = dbContext.CreditsNets.FirstOrDefault(x => x.Name == model.Network);
                                                    smartJob.ExecutionMode        = CheckExecutionMode(model.ExecutionMode);
                                                    smartJob.Rule.RegularDateFrom = CheckDate(model.RegularDateFrom).ToString("MM/dd/yyyy HH:mm:ss");
                                                    smartJob.Rule.RegularDateTo   = CheckDate(model.RegularDateTo).ToString("MM/dd/yyyy HH:mm:ss");
                                                    smartJob.Rule.RegularPeriod   = ConvertPeriod(model.RegularPeriod);
                                                    smartJob.Rule.RegularValue    = CheckPeriodValue(model.RegularValue);
                                                    smartJob.Rule.OnceDate        = CheckDate(model.OnceDate).ToString("MM/dd/yyyy HH:mm:ss");
                                                    smartJob.Rule.CronExpression  = model.CronExpression;
                                                    smartJob.Rule.Presentation    = Rule.GeneratePresentation(smartJob);

                                                    dbContext.SmartJobs.Add(smartJob);
                                                    await dbContext.SaveChangesAsync();

                                                    //Обновляем данные по задаче
                                                    await QuartzTasks.UpdateJob(smartJob.ID);

                                                    result.IsSuccess = true;
                                                    result.Message   = "Action completed";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (DbEntityValidationException err)
            {
                var outputLines = new List <string>();

                foreach (var eve in err.EntityValidationErrors)
                {
                    outputLines.Add(
                        $"{DateTime.Now}: Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:");
                    outputLines.AddRange(eve.ValidationErrors.Select(ve =>
                                                                     $"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\""));
                }

                result.IsSuccess = false;
                result.Message   = String.Join(", ", outputLines.ToArray());
            }
            catch (Exception err)
            {
                result.IsSuccess = false;
                result.Message   = err.ToString();
            }

            return(new JsonResult
            {
                MaxJsonLength = Int32.MaxValue,
                Data = result,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Beispiel #5
0
        public async Task <ActionResult> Execute(int id)
        {
            await QuartzTasks.ExecuteJob(id);

            return(RedirectToAction("Edit", new { id = id }));
        }
Beispiel #6
0
        public async Task <ActionResult> Edit(SmartJob model)
        {
            var identity = HttpContext.GetOwinContext().Authentication.GetExternalIdentity(DefaultAuthenticationTypes.ApplicationCookie);

            if (identity == null || identity.Name == null)
            {
                return(RedirectToAction("Login", "Account"));
            }
            else
            {
                var user = dbContext.Users.FirstOrDefault(x => x.Email == identity.Name);

                if (user == null)
                {
                    return(RedirectToAction("Login", "Account"));
                }
                else if (!user.IsActivated)
                {
                    return(RedirectToAction("WaitActivation", "Account"));
                }

                if (ModelState.IsValid)
                {
                    var smartJob = await dbContext.SmartJobs.Include("Rule").Include("CreditsNet").Include("Events").Include("CreatedBy").FirstOrDefaultAsync(x => x.ID == model.ID);

                    if (smartJob == null)
                    {
                        smartJob           = new SmartJob();
                        smartJob.CreatedBy = user;
                        smartJob.CreatedAt = DateTime.Now;
                        smartJob.Rule      = new Rule();
                        smartJob.Events    = new List <JobEvent>();

                        dbContext.SmartJobs.Add(smartJob);
                    }

                    smartJob.Name       = model.Name;
                    smartJob.IsActive   = model.IsActive;
                    smartJob.Method     = model.Method;
                    smartJob.Address    = model.Address;
                    smartJob.CreditsNet = await dbContext.CreditsNets.FirstOrDefaultAsync(x => x.ID == model.CreditsNet.ID);

                    smartJob.ExecutionMode        = model.ExecutionMode;
                    smartJob.Rule.RegularDateFrom = model.Rule.RegularDateFrom;
                    smartJob.Rule.RegularDateTo   = model.Rule.RegularDateTo;
                    smartJob.Rule.RegularPeriod   = model.Rule.RegularPeriod;
                    smartJob.Rule.RegularValue    = model.Rule.RegularValue;
                    smartJob.Rule.OnceDate        = model.Rule.OnceDate;
                    smartJob.Rule.CronExpression  = model.Rule.CronExpression;
                    smartJob.Rule.Presentation    = Rule.GeneratePresentation(smartJob);

                    await dbContext.SaveChangesAsync();

                    //Обновляем данные по задаче
                    await QuartzTasks.UpdateJob(smartJob.ID);

                    return(RedirectToAction("Index", "Home"));
                }

                return(View(model));
            }
        }