public static void Save(Database db, Purchase purchase) { var existing = db.SingleOrDefault<Purchase>("SELECT * FROM purchases WHERE UniqueId=@0 ", purchase.UniqueId); if (existing != null) db.Update("purchases", "PurchaseId", existing); else db.Insert("purchases", "PurchaseId", purchase); }
public ActionResult Create(ProvisioningRequest provisionRequest) { try { string requestBody = Request.GetBody(); //Emailer.SendEmail("Addon Action", "Create - " + requestBody); provisionRequest = JsonConvert.DeserializeObject<ProvisioningRequest>(requestBody); Plan plan; if (!Enum.TryParse<Plan>(provisionRequest.plan, true, out plan)) { Emailer.SendEmail("Addon Debug Response", "No plan exceptio"); throw new ArgumentException(string.Format("Plan \"{0}\" is not a valid plan", provisionRequest.plan)); } var db = new Database("DefaultConnection"); var purchase = new Purchase() { CreatedBy = string.Format("{0};{1}", Request.GetForwardedHostAddress(), User.Identity.Name), UniqueId = Guid.NewGuid().ToString(), Plan = plan, ProviderId = provisionRequest.heroku_id, ProvisionStatus = ProvisionStatus.Provisioning, ApiKey = Guid.NewGuid().ToString(), ApiSecretKey = Guid.NewGuid().ToString() }; // TODO: Provision the resource purchase.ProvisionStatus = ProvisionStatus.Provisioned; Purchase.Save(db, purchase); var output = new { id = purchase.UniqueId, config = new { CONFIG_ApiKey = purchase.ApiKey, CONFIG_ApiSecretKey = purchase.ApiSecretKey } }; Emailer.SendEmail("Addon Debug Response", output.ToString()); return Json(output, JsonRequestBehavior.AllowGet); } catch(Exception ex) { Emailer.SendEmail("Addon - Error Provisioning Body", ex.Message); } return Json("fail", JsonRequestBehavior.AllowGet); }