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())); } } }
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())); } }