public ActionResult UpdateAccountSubscriptionPlan(SiteAccountsViewModel viewModel)
        {
            if (viewModel != null)
            {
                // Update the subscription plan.
                IValidationResult validationResult = this.doctrineShipsServices.UpdateAccountSubscriptionPlan(viewModel.AccountId, viewModel.SubscriptionPlanId);

                // If the validationResult is not valid, something did not validate in the service layer.
                if (validationResult.IsValid)
                {
                    TempData["Status"] = "The subscription plan was successfully updated.";
                }
                else
                {
                    TempData["Status"] = "Error: The subscription plan was not updated, a validation error occured.<br /><b>Error Details: </b>";

                    foreach (var error in validationResult.Errors)
                    {
                        TempData["Status"] += error.Value + "<br />";
                    }
                }
            }

            return(RedirectToAction("Accounts"));
        }
        public ActionResult DeleteAccount(SiteAccountsViewModel viewModel)
        {
            if (viewModel.RemoveList != null)
            {
                IValidationResult validationResults = new ValidationResult();

                foreach (var accountId in viewModel.RemoveList)
                {
                    // Delete the current account and merge any validation errors in to a validation result object.
                    validationResults.Merge(this.doctrineShipsServices.DeleteAccount(accountId));
                }

                // If the validationResult is not valid, something did not validate in the service layer.
                if (validationResults.IsValid)
                {
                    TempData["Status"] = "All selected accounts were successfully removed.";
                }
                else
                {
                    TempData["Status"] = "Error: One or more errors were detected while removing the selected accounts.<br /><b>Error Details: </b>";

                    foreach (var error in validationResults.Errors)
                    {
                        TempData["Status"] += error.Value + "<br />";
                    }
                }
            }

            return(RedirectToAction("Accounts"));
        }
        public ActionResult AddAccount(SiteAccountsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                string generatedKey = string.Empty;
                int    newAccountId = 0;

                // Clean the passed description.
                string cleanDescription = Server.HtmlEncode(viewModel.Description);

                IValidationResult validationResult = this.doctrineShipsServices.AddAccount(cleanDescription, viewModel.SubscriptionPlanId, out generatedKey, out newAccountId);

                // If the validationResult is not valid, something did not validate in the service layer.
                if (validationResult.IsValid)
                {
                    TempData["Status"] = "The account was successfully added.";
                    if (generatedKey != string.Empty && newAccountId != 0)
                    {
                        // Assign the new key to TempData to be passed to the accounts view.
                        string authUrl = Request.Url.Scheme + "://" + Request.Url.Host + "/A/" + newAccountId + "/" + generatedKey;
                        TempData["Status"] += " The account admin auth url is: <a href=\"" + authUrl + "\">" + authUrl + "</a>";
                    }
                }
                else
                {
                    TempData["Status"] = "Error: The account was not added, a validation error occured.<br /><b>Error Details: </b>";

                    foreach (var error in validationResult.Errors)
                    {
                        TempData["Status"] += error.Value + "<br />";
                    }
                }

                return(RedirectToAction("Accounts"));
            }
            else
            {
                // Re-populate the view model and return with any validation errors.
                viewModel.Accounts = this.doctrineShipsServices.GetAccounts();

                // Re-populate the subscription plan selection list.
                viewModel.SubscriptionPlans = this.doctrineShipsServices.GetSubscriptionPlans()
                                              .Select(x => new SelectListItem
                {
                    Value = x.SubscriptionPlanId.ToString(),
                    Text  = x.Name
                })
                                              .ToList();

                return(View("~/Views/Site/Accounts.cshtml", viewModel));
            }
        }
        public ActionResult Accounts()
        {
            SiteAccountsViewModel viewModel = new SiteAccountsViewModel();

            viewModel.Accounts = this.doctrineShipsServices.GetAccounts();

            // Populate the subscription plan selection list.
            viewModel.SubscriptionPlans = this.doctrineShipsServices.GetSubscriptionPlans()
                                          .Select(x => new SelectListItem
            {
                Value = x.SubscriptionPlanId.ToString(),
                Text  = x.Name
            })
                                          .ToList();

            // Set the ViewBag to the TempData status value passed from the Add & Delete methods.
            ViewBag.Status = TempData["Status"];

            return(View(viewModel));
        }