Esempio n. 1
0
        /// <summary>
        /// Создать запись информации по сайту
        /// </summary>
        public async Task <SiteInfoScheduleDto> CreateAsync(SiteInfoScheduleDto dto)
        {
            var entity = new SiteInfo
            {
                Name = dto.Name,
                Url  = dto.Url
            };

            await _appDbContext.AddAsync(entity);

            await _appDbContext.SaveChangesAsync();

            dto.Id           = entity.Id;
            dto.IntervalUnit = IntervalUnit.Second;

            var scheduleJob = await _scheduleJobService.AddScheduleJobAsync(dto, nameof(PingJob));

            await _appDbContext.SaveChangesAsync();

            await _scheduleJobService.ConfigureAsync(scheduleJob);

            dto.Interval     = scheduleJob.Interval;
            dto.IntervalUnit = scheduleJob.IntervalUnit;

            return(dto);
        }
Esempio n. 2
0
        /// <summary>
        /// Обновить запись информации по сайту
        /// </summary>
        public async Task <SiteInfoScheduleDto> UpdateAsync(SiteInfoScheduleDto dto)
        {
            var entity = await _appDbContext.Set <SiteInfo>().FirstOrDefaultAsync(x => x.Id == dto.Id);

            if (entity == null)
            {
                throw new ValidationException("Ошибка обновления. Не найдена информация по сайту.");
            }

            entity.Name = dto.Name;
            entity.Url  = dto.Url;

            //задаем ед. измерения в секундах
            dto.IntervalUnit = IntervalUnit.Second;

            var scheduleJob = await _scheduleJobService.GetScheduleJobAsync <PingJob>(entity.Id.ToString()) ??
                              await _scheduleJobService.AddScheduleJobAsync(dto, nameof(PingJob));

            scheduleJob.IntervalUnit = IntervalUnit.Second;
            scheduleJob.Interval     = dto.Interval;

            await _appDbContext.SaveChangesAsync();

            await _scheduleJobService.ConfigureAsync(scheduleJob);

            return(dto);
        }
        public async Task <IActionResult> Put([FromBody] SiteInfoScheduleDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.Name) ||
                string.IsNullOrWhiteSpace(dto.Url))
            {
                return(Fail(RequiredFieldsMessage));
            }

            var entity = await _siteInfoService.UpdateAsync(dto);

            return(Success(entity));
        }
        public async Task <IActionResult> Post([FromBody] SiteInfoScheduleDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.Name) ||
                string.IsNullOrWhiteSpace(dto.Url))
            {
                return(Fail(RequiredFieldsMessage));
            }

            var entity = await _siteInfoService.CreateAsync(dto);

            return(CreatedAtAction("Get", new ResponseResult
            {
                Success = true,
                Data = new { entity.Id }
            }));
        }