public async Task <IActionResult> Post([FromBody] Website data) { MonitorAPI.Data.Website Website = new Data.Website(); var website = new Website(); website.Link = data.Link; website.Word = data.Word; website.TimeStamp = DateTime.Now.ToString("dd-MM-yyyyTHH:mm:sszzz"); if (Website.GetStatus(website.Link, website.Word)) { website.Status = true; } else { website.Status = false; } using (var db = new MonitorContext()) { db.Websites.Add(website); await db.SaveChangesAsync(); } return(CreatedAtAction("GetWebsite", new { id = website.Id }, website)); }
public async Task <IActionResult> Put(int id, Website data) { using (var db = new MonitorContext()) { var website = db.Websites.SingleOrDefault(s => s.Id == id); if (website == null) { return(NotFound()); } website.Word = data.Word; website.TimeStamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"); MonitorAPI.Data.Website tester = new Data.Website(); if (tester.GetStatus(website.Link, website.Word)) { website.Status = true; } else { website.Status = false; } db.Websites.Update(website); await db.SaveChangesAsync(); return(Ok(website)); } }
public Website(Data.Website website) { if (website == default) { throw new ArgumentNullException($"{nameof(website)} is null when creating domain website."); } Id = website.Id; Seed = new Uri(website.Seed); }
public static async Task <Website> CreateWebsite(string seed) { if (!Uri.TryCreate(seed, UriKind.Absolute, out Uri uri)) { throw new InvalidCastException($"{seed} is not a valid uri."); } if (uri.Scheme != "http" && uri.Scheme != "https") { throw new Exception("Only http and https are accepted."); } var host = new Uri($"{uri.Scheme}://{uri.Host}"); var webResuest = WebRequest.CreateHttp(host); webResuest.AutomaticDecompression = DecompressionMethods.All; webResuest.AllowAutoRedirect = true; webResuest.Method = "GET"; var response = webResuest.GetResponse(); var websiteHomeUri = response.ResponseUri.ToString(); if (seed != websiteHomeUri) { throw new Exception($"{seed} redirects to {websiteHomeUri} please use this as the seed."); } using ItsyBitsyDbContext context = new ItsyBitsyDbContext(); if (context.Website.Any(x => x.Seed == websiteHomeUri)) { throw new InvalidCastException($"{websiteHomeUri} already exists."); } var dbWebsite = new Data.Website() { Seed = seed }; context.Website.Add(dbWebsite); await context.SaveChangesAsync(); return(new Website(dbWebsite)); }