Пример #1
0
        public void Test()
        {
            //Arrange
            SubscribersMailService mailService = new SubscribersMailService
            {
                RecipientName        = "Igor Sushelski",
                RecipientMailAddress = "*****@*****.**",
                RecipientID          = "",
            };

            //Act
            bool isSent = mailService.SendMail();

            //Assert
            Assert.IsTrue(isSent, "Mail is successfuly sent");
        }
Пример #2
0
        public HttpResponseMessage Post([FromBody] TopArtistsSubscribers subscriber)
        {
            #region Create response messages
            responseMessage = new SubscribersResponseMessages
                              (
                subscriberMail: subscriber.SubscriberEmail,
                subscriberName: subscriber.SubscriberName
                              );
            #endregion

            bool isValidEmali = ValidationMetods.ValidSubscriberEmail(subscriber.SubscriberEmail);
            bool isValidName  = ValidationMetods.ValidSubscriberName(subscriber.SubscriberName);

            if (isValidEmali && isValidName)
            {
                try
                {
                    using (subscribersEntities = new SubscribersDBEntities())
                    {
                        var mailExsist = subscribersEntities.TopArtistsSubscribers.Any(
                            m => m.SubscriberEmail == subscriber.SubscriberEmail);
                        if (!mailExsist)
                        {
                            var newSubscriber = new TopArtistsSubscribers
                            {
                                Id                      = Guid.NewGuid(),
                                SubscriberName          = subscriber.SubscriberName.Trim(),
                                SubscriberEmail         = subscriber.SubscriberEmail.Trim(),
                                SubscribtionDateCreated = DateTime.UtcNow
                            };

                            subscribersEntities.TopArtistsSubscribers.Add(newSubscriber);
                            subscribersEntities.SaveChanges();

                            #region Send Email

                            //  var sender = ConfigurationManager.AppSettings.AllKeys.GetValue;

                            mailService = new SubscribersMailService
                                          (
                                recipientId: newSubscriber.Id.ToString(),
                                recipientName: newSubscriber.SubscriberName,
                                recipientMail: newSubscriber.SubscriberEmail
                                          );
                            mailService.MailSettings
                            (
                                sender: mailSettings.Sender,
                                password: mailSettings.Password,
                                smtpClient: mailSettings.SmtpClient,
                                smtpPort: mailSettings.SmtpPort,
                                mailSubject: mailSettings.Subject,
                                mailBodySubscribed: mailSettings.BodySubscribe,
                                mailBodyUnsubscribed: string.Empty
                            );
                            string mailSent = mailService.SendMail() == true?responseMessage.ResponseMailSubscribeSent()
                                                  : responseMessage.ErrorRresponseMail();

                            #endregion

                            return(Request.CreateResponse(HttpStatusCode.OK, responseMessage.ResponseCreated() + mailSent));
                        }
                        else
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.Conflict, responseMessage.ErrorResponseIsRegistrated()));
                        }
                    }
                }
                catch (Exception ex)
                {
                    responseMessage.Ex = ex.Message;
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, responseMessage.ErrorResponseEx()));
                }
            }
            else
            {
                if (!isValidEmali && !isValidName)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidNameAndEmail()));
                }
                else if (!isValidEmali)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidEmail()));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidName()));
                }
            }
        }
Пример #3
0
        public HttpResponseMessage Delete([FromBody] TopArtistsSubscribers subscriber)
        {
            #region Create response messages
            responseMessage = new SubscribersResponseMessages
                              (
                subscriberMail: subscriber.SubscriberEmail,
                subscriberName: subscriber.SubscriberName
                              );
            #endregion

            bool isValidEmali = ValidationMetods.ValidSubscriberEmail(subscriber.SubscriberEmail);

            if (isValidEmali)
            {
                try
                {
                    using (subscribersEntities = new SubscribersDBEntities())
                    {
                        var mailExsist = subscribersEntities.TopArtistsSubscribers.SingleOrDefault(
                            m => m.SubscriberEmail == subscriber.SubscriberEmail.Trim());

                        if (mailExsist == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.NotFound, responseMessage.ErrorResponseNotExsistEmail()));
                        }
                        else
                        {
                            var exSubscriberName = subscribersEntities.TopArtistsSubscribers.SingleOrDefault(
                                s => s.SubscriberEmail == subscriber.SubscriberEmail)?.SubscriberName;

                            subscribersEntities.TopArtistsSubscribers.Remove(mailExsist);
                            subscribersEntities.SaveChanges();

                            #region Send email
                            mailService = new SubscribersMailService
                                          (
                                recipientName: exSubscriberName,
                                recipientMail: subscriber.SubscriberEmail
                                          );
                            mailService.MailSettings
                            (
                                sender: mailSettings.Sender,
                                password: mailSettings.Password,
                                smtpClient: mailSettings.SmtpClient,
                                smtpPort: mailSettings.SmtpPort,
                                mailSubject: mailSettings.Subject,
                                mailBodySubscribed: string.Empty,
                                mailBodyUnsubscribed: mailSettings.BodyUnsubscribe
                            );
                            string mailSent = mailService.SendMail() == true?responseMessage.ResponseMaiUnsubscribeSent()
                                                  : responseMessage.ErrorRresponseMail();

                            #endregion

                            return(Request.CreateResponse(HttpStatusCode.OK, responseMessage.ResponseDeleted() + mailSent));
                        }
                    }
                }
                catch (Exception ex)
                {
                    responseMessage.Ex = ex.Message;
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, responseMessage.ErrorResponseEx()));
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, responseMessage.ErrorResponseNotValidEmailOnUnsubscribe()));
            }
        }