예제 #1
0
        public HttpResponseMessage Get()
        {
            string username = Thread.CurrentPrincipal.Identity.Name;

            using (subscribersEntities = new SubscribersDBEntities())
            {
                switch (username.ToLower())
                {
                case UserRoles.Admin:
                    var adminRequest = subscribersEntities.TopArtistsSubscribers;
                    return(Request.CreateResponse(HttpStatusCode.OK, adminRequest.ToList()));

                case UserRoles.Employee:
                    var employeeRequest = from subscribers in subscribersEntities.TopArtistsSubscribers
                                          select new SubscribersDto
                    {
                        SubscriberEmail = subscribers.SubscriberEmail,
                        SubscriberName  = subscribers.SubscriberName
                    };
                    return(Request.CreateResponse(HttpStatusCode.OK, employeeRequest.ToList()));

                default:
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
        }
예제 #2
0
 public static bool LogIn(string username, string password)
 {
     using (SubscribersDBEntities entities = new SubscribersDBEntities())
     {
         return(entities.Users.Any(user => user.Username.Equals(username, StringComparison.OrdinalIgnoreCase) &&
                                   user.Password == password));
     }
 }
예제 #3
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()));
                }
            }
        }
예제 #4
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()));
            }
        }