Exemplo n.º 1
0
        public ActionResult Edit(ReferencesViewModel model)
        {
            string oldFileName = string.Empty;
            try
            {
                Reference reference = setupRepository.GetReference(model.Id) ?? new Reference();
                Mapper.Map(model, reference, typeof(ReferencesViewModel), typeof(Reference));
                oldFileName = reference?.FileName ?? string.Empty;
                if (!string.IsNullOrEmpty(model.NewFilePath)) reference.FileName = fileService.UploadReference(model.NewFilePath);

                if (ModelState.IsValid)
                {
                    setupRepository.Save(reference);
                    TempData["savemessage"] = string.Format("{0} has been added", model.Name);
                    if (!string.IsNullOrEmpty(oldFileName)) try { fileService.DeleteReference(oldFileName); } catch (Exception) { /*Logs*/ };
                    return RedirectToAction("List");
                }
                else
                {
                    return View("Reference", model);
                }                
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View("Reference", model);
            }
        }
Exemplo n.º 2
0
 public void Cannot_EditInvalidReference_Redirect()
 {
     referencesController.ModelState.AddModelError("Error", "Error");
     ReferencesViewModel reference = new ReferencesViewModel() { Name = "ok" };
     var actionResult = referencesController.Edit(reference) as ViewResult;
     Assert.AreEqual("Reference", actionResult.ViewName);
 }
Exemplo n.º 3
0
        public void Cannot_Delete_Reference_WhenException()
        {
            ServiceMock.Setup(x => x.DeleteReference(It.IsAny<int>())).Throws(new Exception());

            ReferencesViewModel reference = new ReferencesViewModel() { Id = 3, Name = "ok" };
            var result = referencesController.DeleteConfirmed(reference) as RedirectToRouteResult;
            ServiceMock.Verify(x => x.DeleteReference(It.IsAny<int>()), Times.Once);
            Assert.AreEqual("List", result.RouteValues["action"]);
        }
Exemplo n.º 4
0
 public void Cannot_EditNotUploaded_Redirect()
 {
     ReferencesViewModel reference = new ReferencesViewModel() { Name = "ok" };
     var actionResult = referencesController.Edit(reference) as ViewResult;
     Assert.AreEqual("Reference", actionResult.ViewName);
 }
Exemplo n.º 5
0
        public void Can_EditSolution_RedirectToAction()
        {
            ReferencesViewModel reference = new ReferencesViewModel() { Name = "ok" };
            var actionResult = referencesController.Edit(reference) as RedirectToRouteResult;

            ServiceMock.Verify(x => x.Save(It.IsAny<Reference>()), Times.Once);
            Assert.AreNotEqual(null, referencesController.TempData["savemessage"]);
            Assert.AreEqual("List", actionResult.RouteValues["action"]);
        }
Exemplo n.º 6
0
 public void Can_Delete_References()
 {
     ReferencesViewModel reference = new ReferencesViewModel() { Id = 3, Name = "ok" };
     var result = referencesController.DeleteConfirmed(reference) as RedirectToRouteResult;
     ServiceMock.Verify(x => x.DeleteReference(It.IsAny<int>()), Times.Once);
     Assert.AreNotEqual(null, referencesController.TempData["savemessage"]);
     Assert.AreEqual("List", result.RouteValues["action"]);
 }
Exemplo n.º 7
0
        public void Cannot_EditSolution_SaveException()
        {
            ServiceMock.Setup(x => x.Save(It.IsAny<Reference>())).Throws(new Exception());
            ReferencesViewModel reference = new ReferencesViewModel() { Name = "ok" };
            var actionResult = referencesController.Edit(reference) as ViewResult;
            var model = (actionResult as ViewResult).Model as ReferencesViewModel;

            ServiceMock.Verify(x => x.Save(It.IsAny<Reference>()), Times.Once);
            Assert.AreEqual(null, referencesController.TempData["savemessage"]);
            Assert.AreEqual("Reference", actionResult.ViewName);
            Assert.AreEqual("ok", model.Name);
        }
Exemplo n.º 8
0
 public ActionResult DeleteConfirmed(ReferencesViewModel model)
 {
     try
     {
         Reference reference = setupRepository.GetReference(model.Id);
         string fileName = reference?.FileName ?? string.Empty;
         setupRepository.DeleteReference(model.Id);
         if (!string.IsNullOrEmpty(fileName)) fileService.DeleteReference(fileName);
         TempData["savemessage"] = string.Format("Solution has been deleted");
     }
     catch (Exception /*ex*/ )
     {
         ModelState.AddModelError(String.Empty, string.Format("An error occurred when deleting"));
     }
     return RedirectToAction("List");
 }