public ActionResult Create(AzureSite azuresite)
        {
            if (ModelState.IsValid)
            {
                db.AzureSites.Add(azuresite);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(azuresite);
        }
        public IEnumerable<Project> AzureWebsiteBuilds(string readOnlyToken)
        {
            var sites = new AzureSite[0];
            using (var db = new CcTrayDbContext())
            {
                sites = db.AzureSites.Where(s => s.ReadOnlyToken == readOnlyToken).ToArray();
            }

            foreach (AzureSite site in sites)
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    string authInfo = string.Format("{0}:{1}", site.Username, site.Password);
                    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);

                    string responseBody = string.Empty;
                    try
                    {
                        HttpResponseMessage response =
                            httpClient.GetAsync(string.Format("https://{0}.scm.azurewebsites.net/deployments",
                                                              site.Name))
                                .Result;
                        responseBody = response.Content.ReadAsStringAsync().Result;
                    }
                    catch (Exception)
                    {
                    }
                    var json = JsonConvert.DeserializeObject(responseBody) as dynamic;
                    var applicationArray = json as IEnumerable<dynamic>;
                    dynamic build = applicationArray.OrderByDescending(c=>c.start_time).FirstOrDefault();
                    if (build != null)
                    {
                        yield return (new Project
                                          {
                                              name = site.Name,
                                              webUrl = build.Url,
                                              activity = (bool) build.complete ? "Sleeping" : "Building",
                                              lastBuildStatus = build.status == 4 ? "Success" : "Failure",
                                              lastBuildLabel = build.message,
                                              lastBuildTime = build.end_time.ToString(),
                                          });
                    }
                }
            }
        }
 public ActionResult Edit(AzureSite azuresite)
 {
     if (ModelState.IsValid)
     {
         db.Entry(azuresite).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(azuresite);
 }