/// <summary> /// Takes a Schedule and returns it for addition /// </summary> /// <param name="schedule"></param> /// <returns>The Schedule to be added</returns> public Schedule AddSchedule(CreateScheduleViewModel createScheduleView) { var existingSchedule = _repo.Find(null, d => d.Name.ToLower() == createScheduleView.Name.ToLower())?.Items?.FirstOrDefault(); if (existingSchedule != null) { throw new EntityAlreadyExistsException("Schedule name already exists"); } var timeZone = GetTimeZoneId(createScheduleView.CRONExpressionTimeZone); if (timeZone == null) { throw new EntityOperationException("Unable to find the specified TimeZoneId"); } if (createScheduleView.IsDisabled == false && !createScheduleView.StartingType.ToLower().Equals("manual"))//if schedule is not a manual starting type { if (createScheduleView.StartDate == null) { throw new EntityOperationException("StartDate value cannot be null"); } } Schedule newSchedule = createScheduleView.Map(createScheduleView); //assign request to schedule entity return(newSchedule); }
public async Task <IActionResult> Post([FromBody] CreateScheduleViewModel request) { if (request == null) { ModelState.AddModelError("Save", "No data passed"); return(BadRequest(ModelState)); } //validate the cron expression if (!string.IsNullOrWhiteSpace(request.CRONExpression)) { 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 { Schedule requestObj = request.Map(request); //assign request to schedule entity 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(requestObj); recurringJobManager.RemoveIfExists(requestObj.Id?.ToString()); if (request.IsDisabled == false && !request.StartingType.ToLower().Equals("manual")) { var jsonScheduleObj = JsonSerializer.Serialize <Schedule>(requestObj); backgroundJobClient.Schedule(() => hubManager.ScheduleNewJob(jsonScheduleObj), new DateTimeOffset(requestObj.StartDate.Value)); } return(response); } catch (Exception ex) { return(ex.GetActionResult()); } }