public async Task<IHttpActionResult> Submit(FeedbackModel feedback)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest();
            }

            FeedbackSubmissionsProvider feedbackProvider = new FeedbackSubmissionsProvider();
            await feedbackProvider.CreateFeedbackSubmission(feedback.Email, feedback.Name, feedback.Message);

            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendContactUsMail(feedback);

            return this.Ok();
        }
        public async Task<ActionResult> Send()
        {
            Guid userId = new Guid("5c771db7-ee95-4bda-8bbe-f44eef49de79");
            string userIdString = userId.ToString();

            using (ApplicationUserManager userManager = Startup.UserManagerFactory())
            {
                userManager.SetUserTokenProvider("ForgottenPassword");

                string passwordResetToken = await userManager.GeneratePasswordResetTokenAsync(userIdString);

                //ApplicationUser user = await userManager.Users.FirstAsync(u => u.Id == userIdString);
                string baseUrl = ConfigurationManager.AppSettings["Site.BaseUrl"];
                string resetPasswordPath = ConfigurationManager.AppSettings["Site.ResetPasswordPath"];
                string resetPasswordUrl = string.Concat(baseUrl, resetPasswordPath.TrimStart('/'), "/", passwordResetToken);
                MailSendingFacade mailSender = new MailSendingFacade();
                mailSender.SendForgottenPasswordMail(baseUrl, resetPasswordUrl, userIdString);
            }

            return this.Content("Done!");
        }
        public async Task<IHttpActionResult> SendResetPasswordMail(string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return this.BadRequest("Email is required!");
            }

            ApplicationUser user = await this.UserManager.FindByNameAsync(email);
            if (user == null)
            {
                return this.BadRequest("User with email: " + email + " is not found!");
            }

            if (string.IsNullOrEmpty(user.PasswordHash))
            {
                return this.BadRequest("The user with email: " + email + "does not have local login!");
            }

            this.UserManager.SetUserTokenProvider("ForgottenPassword");
            string passwordResetToken = await this.UserManager.GeneratePasswordResetTokenAsync(user.Id);
            string baseUrl = ConfigurationManager.AppSettings["Site.BaseUrl"];
            string resetPasswordPath = ConfigurationManager.AppSettings["Site.ResetPasswordPath"];
            string resetPasswordUrl = string.Concat(baseUrl, resetPasswordPath.TrimStart('/'), "/", HttpUtility.UrlEncode(email), "?token=", HttpUtility.UrlEncode(passwordResetToken));
            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendForgottenPasswordMail(baseUrl, resetPasswordUrl, email);

            return this.Ok();
        }
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ApplicationUser user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }

            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendNewUserRegisteredMail(model.Email, model.FirstName, model.LastName);

            return Ok();
        }
        public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return Redirect(Url.Content("~/") + "?code=0&data=" + Uri.EscapeDataString(error) + "#/autherror");
            }

            if (!User.Identity.IsAuthenticated)
            {
                return new ChallengeResult(provider, this);
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return InternalServerError();
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return new ChallengeResult(provider, this);
            }

            ApplicationUser user = await UserManager.FindByNameAsync(externalLogin.Email);
            if (user == null)
            {
                user = new ApplicationUser()
                {
                    UserName = externalLogin.Email,
                    Email = externalLogin.Email,
                    FirstName = externalLogin.FirstName,
                    LastName = externalLogin.LastName
                };

                user.Logins.Add(new IdentityUserLogin()
                {
                    LoginProvider = externalLogin.LoginProvider,
                    ProviderKey = externalLogin.ProviderKey,
                    UserId = user.Id
                });

                IdentityResult result = await UserManager.CreateAsync(user);
                IHttpActionResult errorResult = GetErrorResult(result);

                if (errorResult != null)
                {
                    return errorResult;
                }

                MailSendingFacade mailSender = new MailSendingFacade();
                mailSender.SendNewUserRegisteredMail(externalLogin.Email, externalLogin.FirstName, externalLogin.LastName);
            }
            else
            {
                bool isExistingLogin = user.Logins.Any(l => l.LoginProvider == externalLogin.LoginProvider && l.ProviderKey == externalLogin.ProviderKey);
                if (!isExistingLogin)
                {
                    user.Logins.Add(new IdentityUserLogin()
                    {
                        LoginProvider = externalLogin.LoginProvider,
                        ProviderKey = externalLogin.ProviderKey,
                        UserId = user.Id
                    });

                    await UserManager.UpdateAsync(user);
                }
            }

            Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user,
                OAuthDefaults.AuthenticationType);
            ClaimsIdentity cookieIdentity = await UserManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            //AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user);
            Authentication.SignIn(/*properties, */oAuthIdentity, cookieIdentity);

            return Ok();
        }
        public async Task<IHttpActionResult> ValidateAndSaveOrder(FullOrderModel order)
        {
            if (!this.ModelState.IsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid model state");

                return this.BadRequest(this.ModelState);
            }

            if (order.Items.Count() == 0)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: no items");

                return this.BadRequest("Empty order");
            }

            OrderItemsGrouper itemsGrouper = new OrderItemsGrouper();
            IList<OrderedItemModel> normalizedItems = itemsGrouper.NormalizeOrderedItems(order.Items.ToList());
            if (normalizedItems == null)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: grouping error");

                return this.BadRequest("Different prices of the same item");
            }

            OrderValidator orderValidator = new OrderValidator();
            bool areOrderItemsValid = await orderValidator.ValidateOrderItems(normalizedItems);

            if (!areOrderItemsValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities");

                return this.BadRequest("Invalid quantities");
            }

            bool freeShipping = order.Coupones != null && order.Coupones.Any(c => c.FreeShipping.Value);

            IModelTracker<DeliveryMethodModel> deliveryMethodModelTracker = new DeliveryMethodsTrackerFactory().GetDeliveryMethodsTracker(freeShipping);
            IModelTracker<PaymentMethodModel> paymentMethodModelTracker = new PaymentMethodsTrackerFactory().GetPaymentMethodsTracker(freeShipping);
            bool isLoyal = deliveryMethodModelTracker is FreeShippingDeliveryMethodsTracker;
            DeliveryMethodsProvider deliveryMethodsProvider = new DeliveryMethodsProvider(deliveryMethodModelTracker);
            PaymentMethodsProvider paymentMethodsProvider = new PaymentMethodsProvider(paymentMethodModelTracker);

            if (order.Coupones != null)
            {
                bool areCouponesValid = await orderValidator.ValidateCoupones(this.User.Identity.GetUserId(), order);
                if (!areCouponesValid)
                {
                    Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid coupones");

                    return this.BadRequest("Invalid coupones");
                }
            }

            bool areOrderPricesValid = await orderValidator.ValidatePrices(order, deliveryMethodsProvider, paymentMethodsProvider);
            if (!areOrderPricesValid)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid prive");

                return this.BadRequest("Invalid price");
            }

            StocksProvider stocksProvider = new StocksProvider();
            bool stockQuantitiesUpdatedSuccessfully = await stocksProvider.UpdateStocks(
                normalizedItems.Select(o => new StockChangeModel(o.ArticleId.Value, o.SizeId.Value, o.ColorId, -o.Quantity.Value)),
                Logger.Current);
            if (!stockQuantitiesUpdatedSuccessfully)
            {
                Logger.Current.LogWarning("Orders.ValidateAndSaveOrder: invalid quantities (update stocks)");

                return this.BadRequest("Invalid quantities");
            }

            
            Task<DeliveryMethodModel> getDeliveryMethodTask = deliveryMethodsProvider.GetDeliveryMethodById(order.DeliveryMethodId.Value);
            Task<PaymentMethodModel> getPaymentMethodTask = paymentMethodsProvider.GetPaymentMethodById(order.PaymentMethodId.Value);

            DeliveryMethodModel deliveryMethod = await getDeliveryMethodTask;
            PaymentMethodModel paymentMethod = await getPaymentMethodTask;

            Order newOrder = new Order();
            newOrder.UserId = new Guid(this.User.Identity.GetUserId());
            newOrder.DeliveryMerchant = deliveryMethod.Name;
            newOrder.DeliveryPrice = deliveryMethod.DeliveryPrice;
            newOrder.PaymentMethodId = paymentMethod.Id;
            newOrder.HasCommission = paymentMethod.ApplyDeliveryTax;
            newOrder.CommissionPercents = deliveryMethod.CODTax;
            newOrder.ShippingPrice = order.Total.Shipping.Value;
            newOrder.ItemsPrice = order.Total.Order.Value;
            newOrder.TotalPrice = order.Total.Full.Value;
            newOrder.StatusId = 1;
            newOrder.DateCreated = DateTime.UtcNow;

            List<OrderDetail> orderDetails = new List<OrderDetail>();
            foreach (OrderedItemModel orderedItem in normalizedItems)
            {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.ItemId = orderedItem.ArticleId.Value;
                orderDetail.SizeId = orderedItem.SizeId.Value;
                orderDetail.ColorId = orderedItem.ColorId;
                orderDetail.Quantity = orderedItem.Quantity.Value;
                orderDetail.ItemPrice = orderedItem.Price.Value;

                orderDetails.Add(orderDetail);
            }

            OrdersProvider ordersProvider = new OrdersProvider();
            int newOrderId = await ordersProvider.SaveOrder(newOrder, orderDetails);

            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendNewOrderMail(newOrderId);

            return this.Ok(newOrderId);
        }