// GET: /Subscriber/Create public ActionResult Create() { var lists = GetListNames(); ViewBag.ListName = new SelectList(lists, "ListName", "Description"); var model = new Subscriber() { Verified = false }; return View(model); }
// Use this URL format to test // http://127.0.0.1:81/api/[email protected]&listName=contoso1 // public async Task<HttpResponseMessage> GetSubscribeToList(string emailAddress, string listName) { var newSubscriber = new Subscriber() { EmailAddress = emailAddress, ListName = listName, Verified = false, SubscriberGUID = Guid.NewGuid().ToString() }; // If the list doesn't exist, send an error response. if (await IsListAsync(listName) == false) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No such list " + listName); } // Handle the special case of user currently subscribed var subscriber = await GetSubscribedUserAsync(listName, emailAddress); if (subscriber != null) { return await HandleSubscribedUserAsync(subscriber); } var insertOperation = TableOperation.Insert(newSubscriber); mailingListTable.Execute(insertOperation); // Create a queue message which will send a subscribe confirmation email. await CreateQueueMessageAsync(newSubscriber); return Request.CreateResponse(HttpStatusCode.Accepted); }
public ActionResult Create(Subscriber subscriber) { if (ModelState.IsValid) { subscriber.SubscriberGUID = Guid.NewGuid().ToString(); if (subscriber.Verified.HasValue == false) { subscriber.Verified = false; } var insertOperation = TableOperation.Insert(subscriber); mailingListTable.Execute(insertOperation); return RedirectToAction("Index"); } var lists = GetListNames(); ViewBag.ListName = new SelectList(lists, "ListName", "Description", subscriber.ListName); return View(subscriber); }
private static void SendSubscribeEmail(string subscriberGUID, Subscriber subscriber, MailingList mailingList) { var email = SendGrid.GetInstance(); email.From = new MailAddress(mailingList.FromEmailAddress); email.AddTo(subscriber.EmailAddress); string subscribeURL = RoleEnvironment.GetConfigurationSettingValue("AzureMailServiceURL") + "/subscribe?id=" + subscriberGUID + "&listName=" + subscriber.ListName; email.Html = String.Format("<p>Click the link below to subscribe to {0}. " + "If you don't confirm your subscription, you won't be subscribed to the list.</p>" + "<a href=\"{1}\">Confirm Subscription</a>", mailingList.Description, subscribeURL); email.Text = String.Format("Copy and paste the following URL into your browser in order to subscribe to {0}. " + "If you don't confirm your subscription, you won't be subscribed to the list.\n" + "{1}", mailingList.Description, subscribeURL); email.Subject = "Subscribe to " + mailingList.Description; var credentials = new NetworkCredential(RoleEnvironment.GetConfigurationSettingValue("SendGridUserName"), RoleEnvironment.GetConfigurationSettingValue("SendGridPassword")); var transportREST = Web.GetInstance(credentials); transportREST.Deliver(email); }
private async Task<HttpResponseMessage> HandleSubscribedUserAsync(Subscriber subscriber) { // If the user is Verified, send an error response. if (subscriber.Verified == true) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, subscriber.EmailAddress + " Currently subscribed to: " + subscriber.ListName); } else { // The user is currently subscribed to the list but not Verified // they might have lost the previous confirm subscribe email and be requesting // another confirmation email. // Send a queue message which will send a subscribe confirmation email. await CreateQueueMessageAsync(subscriber); return Request.CreateResponse(HttpStatusCode.Accepted); } }
private async Task CreateQueueMessageAsync(Subscriber newSubscriber) { await subscribeQueue.AddMessageAsync(new CloudQueueMessage(newSubscriber.SubscriberGUID + "," + newSubscriber.ListName)); }
public async Task<ActionResult> Edit(string partitionKey, string rowKey, string listName, string emailAddress, Subscriber editedSubscriber) { // Since MailingList and UpdateModel are in the view as EditorFor fields, // and since they refer to the same properties as PartitionKey and RowKey, // exclude PartitionKey and RowKey from model binding when calling UpdateModel. var excludeProperties = new string[] { "PartitionKey", "RowKey" }; if (ModelState.IsValid) { try { UpdateModel(editedSubscriber, string.Empty, null, excludeProperties); if (editedSubscriber.PartitionKey == partitionKey && editedSubscriber.RowKey == rowKey) { //Keys didn't change -- Update the row var replaceOperation = TableOperation.Replace(editedSubscriber); await mailingListTable.ExecuteAsync(replaceOperation); } else { // Keys changed, delete the old record and insert the new one. if (editedSubscriber.PartitionKey != partitionKey) { // PartitionKey changed, can't do delete/insert in a batch. var deleteOperation = TableOperation.Delete(new Subscriber { PartitionKey = partitionKey, RowKey = rowKey, ETag = editedSubscriber.ETag }); await mailingListTable.ExecuteAsync(deleteOperation); var insertOperation = TableOperation.Insert(editedSubscriber); await mailingListTable.ExecuteAsync(insertOperation); } else { // RowKey changed, do delete/insert in a batch. var batchOperation = new TableBatchOperation(); batchOperation.Delete(new Subscriber { PartitionKey = partitionKey, RowKey = rowKey, ETag = editedSubscriber.ETag }); batchOperation.Insert(editedSubscriber); await mailingListTable.ExecuteBatchAsync(batchOperation); } } return RedirectToAction("Index"); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 412) { // Concurrency error. // Only catching concurrency errors for non-key fields. If someone // changes a key field we'll get a 404 and we have no way to know // what they changed it to. var currentSubscriber = FindRow(partitionKey, rowKey); if (currentSubscriber.Verified != editedSubscriber.Verified) { ModelState.AddModelError("Verified", "Current value: " + currentSubscriber.Verified); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); ModelState.SetModelValue("ETag", new ValueProviderResult(currentSubscriber.ETag, currentSubscriber.ETag, null)); } else { throw; } } } var lists = await GetListNamesAsync(); ViewBag.ListName = new SelectList(lists, "ListName", "Description", listName); return View(editedSubscriber); }
// GET: /Subscriber/Create public async Task<ActionResult> Create() { var lists = await GetListNamesAsync(); ViewBag.ListName = new SelectList(lists, "ListName", "Description"); var model = new Subscriber() { Verified = false }; return View(model); }
private void UpdateSubscriber(string partitionKey, string rowKey, Subscriber editedSubscriber) { try { var replaceOperation = TableOperation.Replace(editedSubscriber); mailingListTable.Execute(replaceOperation); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 412) { // Concurrency error. // Only catching concurrency errors for non-key fields. If someone // changes a key field we'll get a 404 and we have no way to know // what they changed it to. var currentSubscriber = FindRow(partitionKey, rowKey); if (currentSubscriber.Verified != editedSubscriber.Verified) { ModelState.AddModelError("Verified", "Current value: " + currentSubscriber.Verified); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); ModelState.SetModelValue("ETag", new ValueProviderResult(currentSubscriber.ETag, currentSubscriber.ETag, null)); } else { throw; } } }
private void CreateQueueMessage(Subscriber newSubscriber) { subscribeQueue.AddMessage(new CloudQueueMessage(newSubscriber.SubscriberGUID + "," + newSubscriber.ListName)); }