public ActionResult Add(SectionModel model) { if (ModelState.IsValid) { try { Section section = CreateSectionFromSectionModel(model); sectionRepository.AddObject(section); return RedirectToAction("Index"); } catch (DbUpdateException ex) { throw new DbUpdateException("Wystąpił błąd podczas dodawania sekcji. Pole skrócona nazwa nie może się powtarzać: " + ex.Message); } catch (Exception ex) { throw new Exception("Wystąpił błąd podczas dodawania sekcji. Proszę o kontakt z administratorem. Error message: " + ex.Message); } } else { if (Request.IsAjaxRequest()) { return PartialView("Section/_SectionAdd", model); } return View(model); } }
public ActionResult Add() { SectionModel model = new SectionModel(); if (Request.IsAjaxRequest()) { return PartialView("Section/_SectionAdd", model); } return View(model); }
public Section CreateSectionFromSectionModel(SectionModel model) { Section temp = new Section(); temp.email = model.email; temp.id = model.id; temp.locality = model.locality; temp.name = model.name; temp.phone_number = model.phone_number; temp.post = model.post; temp.postal_code = model.postal_code; temp.short_name = model.short_name; temp.street = model.street; return temp; }
public void CanEditSection() { Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>(); mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable()); SectionController controller = new SectionController(mock_section.Object); SectionModel section = new SectionModel(); Section temp = mock_section.Object.Repository.FirstOrDefault(x => x.short_name == "IMZ6"); Assert.IsNotNull(temp); Assert.AreEqual(temp.id, 6); Assert.AreEqual(temp.name, "FFF"); Assert.AreEqual(temp.email, "*****@*****.**"); Assert.AreEqual(temp.locality, "Łódź"); Assert.AreEqual(temp.phone_number, "723178153"); Assert.AreEqual(temp.postal_code, "22-336"); Assert.AreEqual(temp.post, "Łódź"); Assert.AreEqual(temp.short_name, "IMZ6"); Assert.AreEqual(temp.street, "Spółdzielców 61"); section.id = 6; section.email = "*****@*****.**"; section.locality = "Radom"; section.name = "YYY"; section.phone_number = "551111111"; section.post = "Radom"; section.postal_code = "11-111"; section.short_name = "IMZ111"; section.street = "Łódzka 55"; var redirectToRouteResult = controller.Edit(section) as RedirectToRouteResult; temp = mock_section.Object.Repository.FirstOrDefault(x => x.id == 6); Assert.AreEqual(temp.name, "YYY"); Assert.AreEqual(temp.email, "*****@*****.**"); Assert.AreEqual(temp.locality, "Radom"); Assert.AreEqual(temp.phone_number, "551111111"); Assert.AreEqual(temp.postal_code, "11-111"); Assert.AreEqual(temp.post, "Radom"); Assert.AreEqual(temp.short_name, "IMZ111"); Assert.AreEqual(temp.street, "Łódzka 55"); mock_section.Verify(m => m.EditObject(temp), Times.Once()); Assert.IsNotNull(redirectToRouteResult); Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]); }
public void CanAddSection() { Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>(); mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable()); SectionController controller = new SectionController(mock_section.Object); SectionModel section = new SectionModel(); section.email = "*****@*****.**"; section.locality = "Gdańsk"; section.name = "XXX"; section.phone_number = "552134152"; section.post = "Gdańsk"; section.postal_code = "22-242"; section.short_name = "IMZ8"; section.street = "Królewska 11"; var redirectToRouteResult = controller.Add(section) as RedirectToRouteResult; mock_section.Verify(m => m.AddObject(It.IsAny<Section>()), Times.Once()); Assert.IsNotNull(redirectToRouteResult); Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]); }
public SectionModel CreateSectionModelFromSection(Section section) { SectionModel temp = new SectionModel(); temp.email = section.email; temp.id = section.id; temp.locality = section.locality; temp.name = section.name; temp.phone_number = section.phone_number; temp.post = section.post; temp.postal_code = section.postal_code; temp.short_name = section.short_name; temp.street = section.street; return temp; }
public void UpdateSection(ref Section section, SectionModel model) { section.email = model.email; section.id = model.id; section.locality = model.locality; section.name = model.name; section.phone_number = model.phone_number; section.post = model.post; section.postal_code = model.postal_code; section.short_name = model.short_name; section.street = model.street; }
public ActionResult Edit(SectionModel model) { ModelState.Remove("short_name"); if (ModelState.IsValid) { try { Section sec = sectionRepository.Repository.FirstOrDefault(x => x.id == model.id); UpdateSection(ref sec, model); sectionRepository.EditObject(sec); return RedirectToAction("Index"); } catch (DbUpdateException ex) { throw new DbUpdateException("Wystąpił błąd podczas edytowania sekcji. Pole skrócona nazwa istnieje w bazie danych. Proszę podać inną nazwę.", ex.InnerException); } catch (Exception ex) { throw new Exception("Nie udało się edytować sekcji. Proszę skontaktować się z administratorem. Error message: " + ex.Message); } } else { if (Request.IsAjaxRequest()) { return PartialView("Section/_SectionEdit", model); } return View(model); } }