Пример #1
0
        public async Task <IActionResult> Update(int id, [FromBody] WebSiteStatusResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var webSiteStatus = await dbContext.WebSiteStatuses.FirstOrDefaultAsync(wss => wss.Id == id);

            if (webSiteStatus == null)
            {
                ModelState.AddModelError(nameof(webSiteStatus), $"{nameof(webSiteStatus)} was not found.");
                return(BadRequest(ModelState));
            }

            webSiteStatus.UpdateFromResource(resource);

            if (webSiteStatus.LastActionId.HasValue)
            {
                scheduleService.Unschedule(webSiteStatus.LastActionId.Value);
            }

            webSiteStatus.LastActionId = scheduleService.Schedule(() => webSiteCrawlService.Crawl(webSiteStatus), webSiteStatus.Interval);
            dbContext.WebSiteStatuses.Update(webSiteStatus);
            await dbContext.SaveChangesAsync();

            var result = Ok(webSiteStatus.ToResource());

            return(result);
        }
        public static void UpdateFromResource(this WebSiteStatus webSiteStatus, WebSiteStatusResource resource)
        {
            if (webSiteStatus == null)
            {
                throw new ArgumentNullException(nameof(webSiteStatus));
            }

            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            webSiteStatus.Interval    = resource.Interval;
            webSiteStatus.SiteName    = resource.SiteName;
            webSiteStatus.Url         = resource.Url;
            webSiteStatus.LastUpdated = DateTime.UtcNow;
        }
        public static WebSiteStatus ToWebSiteStatus(this WebSiteStatusResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            var result = new WebSiteStatus
            {
                Interval    = resource.Interval,
                LastUpdated = DateTime.UtcNow,
                SiteName    = resource.SiteName,
                Url         = resource.Url
            };

            return(result);
        }
Пример #4
0
        public async Task <IActionResult> Create([FromBody] WebSiteStatusResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var webSiteStatus = resource.ToWebSiteStatus();

            webSiteStatus.LastActionId = scheduleService.Schedule(() => webSiteCrawlService.Crawl(webSiteStatus), webSiteStatus.Interval);
            dbContext.WebSiteStatuses.Add(webSiteStatus);
            await dbContext.SaveChangesAsync();

            var result = Ok(webSiteStatus.ToResource());

            return(result);
        }
        public static WebSiteStatusResource ToResource(this WebSiteStatus webSiteStatus)
        {
            if (webSiteStatus == null)
            {
                throw new ArgumentNullException(nameof(webSiteStatus));
            }

            var result = new WebSiteStatusResource
            {
                Id          = webSiteStatus.Id,
                SiteName    = webSiteStatus.SiteName,
                Url         = webSiteStatus.Url,
                IsActive    = webSiteStatus.IsActive,
                LastUpdated = webSiteStatus.LastUpdated,
                Interval    = webSiteStatus.Interval
            };

            return(result);
        }