Ejemplo n.º 1
0
        public async Task <ActionResult> OnPostSend(string emailAddress)
        {
            Result result;

            try
            {
                SubscriberModel subscriber = await Data.Item(SiteSettings.Site, Area.Blog, emailAddress);

                if (subscriber == null)
                {
                    throw new NullReferenceException($"No subscriber found for {emailAddress}.");
                }

                result = await Data.Delete(SiteSettings.Site, subscriber.Id);

                if (result.Status == ResultStatus.Failed)
                {
                    return(new JsonResult(result));
                }
                result = new Result(ResultStatus.Succeeded, $"{emailAddress} has been unsubscribed.", Guid.Empty);
            }
            catch (Exception ex)
            {
                result = new Result(ResultStatus.Failed, ex.Message, Guid.Empty);
            }

            return(new JsonResult(result));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> OnPostDeleteSubscriber(string emailAddress)
        {
            Result result;

            try
            {
                result = await Data.Delete(SiteSettings.Site, emailAddress);

                if (result.Status == ResultStatus.Succeeded)
                {
                    result = new Result(ResultStatus.Succeeded, $"{emailAddress} has been unsubscribed.", Guid.Empty);
                }
            }
            catch (System.Exception ex)
            {
                result = new Result(ResultStatus.Failed, ex.Message, Guid.Empty);
            }

            return(new JsonResult(result));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnGet()
        {
            Item = await new Base().Get(SiteSettings, Admin, PageName);

            List = await Data.List(SiteSettings.Site, Area.Blog, (CurrentPage - 1),
                                   SiteSettings.AdminSubscriberPagingSize);

            var sbSubscribers = new StringBuilder();

            List <Subscriber> subscribers = await Data.List(SiteSettings.Site, Area.Blog);

            foreach (Subscriber subscriber in subscribers)
            {
                sbSubscribers.Append(subscriber.EmailAddress);
                sbSubscribers.Append("; ");
            }

            EmailAddresses = sbSubscribers.ToString();

            Count = await Data.PagingCount(SiteSettings.Site, Area.Blog);

            return(Item == null?NotFound() : (IActionResult)Page());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> OnPostSubscribe()
        {
            Result result;

            try
            {
                var stream = new MemoryStream();
                await Request.Body.CopyToAsync(stream);

                stream.Position  = 0;
                using var reader = new StreamReader(stream);
                string requestBody = await reader.ReadToEndAsync();

                if (requestBody.Length <= 0)
                {
                    throw new IndexOutOfRangeException("requestBody is empty.");
                }

                var obj = JsonConvert.DeserializeObject <SubscriberModel>(requestBody);
                if (obj == null)
                {
                    throw new NullReferenceException("Model could not be derived from JSON object.");
                }

                var dataModel = new SubscriberDataModel
                {
                    Id           = Guid.NewGuid(),
                    EmailAddress = obj.EmailAddress,
                    FirstName    = obj.FirstName,
                    LastName     = obj.LastName
                };

                result = await SubscriberRepository.Save(SiteSettings.Site, Area.Blog, dataModel);

                if (result.Status == ResultStatus.Succeeded)
                {
                    result.Messages = new List <string> {
                        "You're subscribed!"
                    };
                }

                string fullName = obj.FirstName + " " + obj.LastName;
                var    body     = new StringBuilder();
                body.Append($"Thank you for subscribing to my blog, {obj.FirstName}.");
                body.Append("<p>");
                body.Append("You can expect an email or two per week announcing my latest blog post(s). ");
                body.Append("Please feel free to comment whenever you feel the urge. ");
                body.Append("Also, please be aware subscribers will be the first to know about any giveaways I might offer in the future. ");
                body.Append("Until next time...");
                body.Append("</p>");
                body.Append("<p>");
                body.Append("Happy reading!");
                body.Append("</p>");
                body.Append("<p>&nbsp;</p>");
                body.Append("<p>");
                body.Append("Cinta Garcia, <a href=\"https://professionaltranslator.net\">Professional Translator</a>");
                body.Append("</p>");
                body.Append("<p>&nbsp;</p>");
                body.Append("<p>");
                body.Append("P.S. You can unsubscribe from my blog at any time by visiting <a href=\"https://professionaltranslator.net/unsubscribe\">professionaltranslator.net/unsubscribe</a>.");
                body.Append("</p>");

                var toList = new List <MailAddress>
                {
                    new MailAddress(obj.EmailAddress, fullName)
                };

                var ccList = new List <MailAddress>
                {
                    new MailAddress(SiteSettings.DefaultTo, SiteSettings.DefaultToDisplayName)
                };

                var replyToList = new List <MailAddress>
                {
                    new MailAddress(obj.EmailAddress, fullName)
                };

                Smtp.SendMail(SiteSettings, replyToList, toList, ccList, new List <MailAddress>(),
                              $"Welcome, {fullName}!", body.ToString(), Smtp.BodyType.Html, Smtp.SslSetting.Off);
            }
            catch (System.Exception ex)
            {
                result = new Result(ResultStatus.Failed, ex.Message, Guid.Empty);
            }

            return(new JsonResult(result));
        }