示例#1
0
        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));
        }
示例#2
0
        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));
            }
        }
示例#3
0
        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);
        }
示例#4
0
        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));
        }