Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CreateScheduleViewModel request)
        {
            ParametersViewModel.VerifyParameterNameAvailability(request.Parameters);
            if (request == null)
            {
                ModelState.AddModelError("Save", "No data passed");
                return(BadRequest(ModelState));
            }

            //validate the cron expression
            if (!string.IsNullOrWhiteSpace(request.CRONExpression))
            {
                if (string.IsNullOrWhiteSpace(request.CRONExpressionTimeZone))
                {
                    request.CRONExpressionTimeZone = "UTC";
                }
                try
                {
                    CronExpression expression = CronExpression.Parse(request.CRONExpression, CronFormat.Standard);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Save", string.Concat("Invalid cron expression, ", ex.Message));
                    return(BadRequest(ModelState));
                }
            }

            Guid entityId = Guid.NewGuid();

            if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty))
            {
                request.Id = entityId;
            }

            try
            {
                if (request.StartingType.ToLower() == "queuearrival")
                {
                    if (request.QueueId == null)
                    {
                        throw new EntityOperationException("Schedule of starting type \"QueueArrival\" must contain a Queue id");
                    }
                }

                Schedule newSchedule = _manager.AddSchedule(request);

                foreach (var parameter in request.Parameters ?? Enumerable.Empty <ParametersViewModel>())
                {
                    ScheduleParameter scheduleParameter = new ScheduleParameter
                    {
                        Name       = parameter.Name,
                        DataType   = parameter.DataType,
                        Value      = parameter.Value,
                        ScheduleId = entityId,
                        CreatedBy  = applicationUser?.UserName,
                        CreatedOn  = DateTime.UtcNow,
                        Id         = Guid.NewGuid()
                    };
                    _scheduleParameterRepository.Add(scheduleParameter);
                }

                var response = await base.PostEntity(newSchedule);

                _recurringJobManager.RemoveIfExists(newSchedule.Id?.ToString());

                if (request.IsDisabled == false && !request.StartingType.ToLower().Equals("manual"))//if schedule is not a manual starting type
                {
                    var jsonScheduleObj = JsonSerializer.Serialize <Schedule>(newSchedule);

                    _backgroundJobClient.Schedule(() => _hubManager.ScheduleNewJob(jsonScheduleObj),
                                                  new DateTimeOffset(newSchedule.StartDate.Value));
                }

                return(response);
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }