public void Constructor_populates_viewmodel_with_application_properties_if_one_is_supplied()
        {
            // setup
            var application = new ApplicationBuilder().WithName("Name").WithDefaultTeam().Build();

            // act
            var model = new ApplicationViewModel(application);

            // verify
            Assert.AreEqual(application.Id, model.Id);
            Assert.AreEqual(application.Name, model.Name);
            Assert.AreEqual(application.Description, model.Description);
            Assert.AreEqual(application.TeamId, model.TeamId);
            Assert.AreEqual(application.Team.Name, model.TeamName);
        }
        public void Delete_sets_error_message_on_unhandled_DAL_exception_and_returns_same_view()
        {
            // setup
            var application = new ApplicationBuilder().WithDefaultTeam().WithName("A").Build();
            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);
            applicationRepository.Setup(p => p.SoftDeleteAndSubmit(It.IsAny<Application>())).Throws(new InvalidOperationException());
            var viewModel = new ApplicationViewModel(application);

            // act
            var controller = CreateInstance();
            var model = (ViewResult)controller.Delete(viewModel);

            // verify 
            Assert.IsNull(controller.ViewBag.SuccessMessage);
            Assert.IsNotNull(controller.ViewBag.ErrorMessage);
        }
        public void Delete_soft_deletes_application_upon_confirm_and_redirects_to_index_with_success_message()
        {
            // setup
            var application = new ApplicationBuilder().WithDefaultTeam().WithName("A").Build();
            applicationRepository.Setup(p => p.GetById(application.Id)).Returns(application);

            // set up application change for name and team
            var viewModel = new ApplicationViewModel(application);

            // act
            var controller = CreateInstance();
            var model = (RedirectToRouteResult)controller.Delete(viewModel);

            // verify if correct application is passed to DAL
            applicationRepository.Verify(p => p.SoftDeleteAndSubmit(It.Is<Application>(
                x => x.Id == application.Id)), Times.Once());

            // verify
            Assert.AreEqual("Index", model.RouteValues["Action"]);
            Assert.IsNotNull(controller.ViewBag.SuccessMessage);
            Assert.IsNull(controller.ViewBag.ErrorMessage);
        }
        public ActionResult Delete(ApplicationViewModel model)
        {
            var application = applicationRepository.GetById(model.Id);
            if (application == null) { throw new ArgumentException(string.Format("Application with Id [{0}] does not exist", model.Id)); }

            try
            {
                applicationRepository.SoftDeleteAndSubmit(application);

                base.SetSuccessMessage("The application has been (soft) deleted.");
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                base.SetErrorMessage("Whoops! Couldn't delete the application. The error was [{0}]", ex.Message);
            }

            return View(model);
        }