コード例 #1
0
        public ActionResult Create(User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    unitOfWork.UserRepository.InsertUser(user);
                    unitOfWork.Save();

                    TempData["DataUrl"] = "data-url=/User";
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                ModelState.AddModelError(string.Empty, defaultErrorMessage);
            }
            return View("Create",user);
        }
コード例 #2
0
        public void TestCreate_WhenNoError_InsertUserAndReturnIndexView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            var newUser = userRepository.GetUserByID(1);

            User user = new User()
            {
                Email = "*****@*****.**",
                FirstName = "FirstName1",
                LastName = "LastName1"
            };

            // Act
            RedirectToRouteResult actual = target.Create(user) as RedirectToRouteResult;

            // Assert
            Assert.IsTrue(user.UserID != 0);
            Assert.IsTrue(target.ModelState.IsValid);

            Assert.IsNotNull(user.DateAdded);
            Assert.IsNull(user.DateModified);
            Assert.IsNull(user.IsDeleted);

            Assert.AreEqual(user.FirstName, newUser.FirstName);
            Assert.AreEqual(user.LastName, newUser.LastName);
            Assert.AreEqual(user.Email, newUser.Email);

            Assert.AreEqual("Index", actual.RouteValues["action"]);
        }
コード例 #3
0
        public void TestCreate_WhenModelError_TooLongStrings_ReturnCreateView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            User user = new User()
            {
                Email = "*****@*****.**",
                FirstName = "longfirstnamelongfirstnamelongfirstnamelongfirstnamelongfirstname",
                LastName = "longlastnamelonglastnamelonglastnamelonglastnamelonglastnamelonglastname"
            };

            // Act
            ViewResult actual = target.Create(user) as ViewResult;

            // Assert
            Assert.IsFalse(target.ModelState.IsValid);
            Assert.IsTrue(user.UserID == 0);
            Assert.AreEqual("Create", actual.ViewName);
        }
コード例 #4
0
        public void TestCreate_WhenModelError_InvalidEmail_ReturnCreateView()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();
            UserController target = new UserController(unitOfWork);

            User user = new User()
            {
                Email = "notvalid@[email protected]",
                FirstName = "FirstName1",
                LastName = "LastName1"
            };

            // Act
            ViewResult actual = target.Create(user) as ViewResult;

            // Assert
            Assert.IsFalse(target.ModelState.IsValid);
            Assert.IsTrue(user.UserID == 0);
            Assert.AreEqual("Create", actual.ViewName);
        }
コード例 #5
0
        public ActionResult DeleteConfirmed(FormCollection formCollection)
        {
            int userID = 0;
            Byte[] timestamp = new Byte[8];
            bool deleteConfirmed = false;

            User user = new User();

            try
            {
                foreach (string _formData in formCollection)
                {
                    switch (_formData)
                    {
                        case "UserToDeleteID":
                            userID = int.Parse(formCollection[_formData]);
                            break;
                        case "TimestampToDelete":
                            timestamp = Convert.FromBase64String(formCollection[_formData]);
                            break;
                        case "DeleteConfirmed":
                            deleteConfirmed = true;
                            break;
                    }
                }

                user = unitOfWork.UserRepository.GetUserByID(userID);

                if (user.IsDeleted == true)
                {
                    ModelState.AddModelError(string.Empty, "The record has been already deleted by another user. Click 'Back' button to come back to list.");
                    ViewBag.ButtonsDisabled = true;
                    return View("Edit", user);
                }

                if ((!user.Timestamp.SequenceEqual(timestamp)) && (!deleteConfirmed))
                {
                    ModelState.AddModelError(string.Empty, "The record was modified by another user. "
                        + "Please confirm the Delete operation or click the Back button.");
                    ViewBag.ButtonsDisabled = true;
                    ViewBag.ConfirmDeletionButton = true;
                    return View("Edit", user);
                }

                user.Timestamp = timestamp;

                unitOfWork.UserRepository.DeleteUser(user.UserID);
                unitOfWork.Save();

            }
            catch (DbUpdateConcurrencyException ex)
            {
                var entry = ex.Entries.Single();
                var databaseValues = (User)entry.GetDatabaseValues().ToObject();
                var clientValues = (User)entry.Entity;

                ModelState.AddModelError(string.Empty, "The record was modified by another user. "
                + "The current values have been displayed. "
                + "Please confirm the Delete operation or click the Back button.");

                user.Timestamp = databaseValues.Timestamp;

                return View("Edit", user);

            }
            catch
            {
                TempData["DataUrl"] = "data-url=/User";
                return RedirectToAction("Index", "User", new { errorOccurred = true });
            }

            TempData["DataUrl"] = "data-url=/User";
            return RedirectToAction("Index", "User");
        }
コード例 #6
0
        public ActionResult Edit(User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    unitOfWork.UserRepository.UpdateUser(user);
                    unitOfWork.Save();

                    TempData["DataUrl"] = "data-url=/User";
                    return RedirectToAction("Index");
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var entry = ex.Entries.Single();
                var databaseValues = (User)entry.GetDatabaseValues().ToObject();
                var clientValues = (User)entry.Entity;

                if (databaseValues.IsDeleted == true)
                {
                    ModelState.AddModelError(string.Empty, "The record has been already deleted by another user. Click the Back button to come back to the list.");
                    ViewBag.ButtonsDisabled = true;
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "The record was modified by another user. "
                    + "Please confirm the Save operation or click the Back button.");

                    if (databaseValues.FirstName != clientValues.FirstName)
                        ModelState.AddModelError("FirstName", "Current value: " + databaseValues.FirstName);
                    if (databaseValues.LastName != clientValues.LastName)
                        ModelState.AddModelError("LastName", "Current value: " + databaseValues.LastName);
                    if (databaseValues.Email != clientValues.Email)
                        ModelState.AddModelError("Email", "Current value: " + databaseValues.Email);
                }

                user.Timestamp = databaseValues.Timestamp;

                return View("Edit", user);
            }
            catch
            {
                ModelState.AddModelError(string.Empty, defaultErrorMessage);
            }
            return View("Edit",user);
        }
コード例 #7
0
 public void UpdateUser(User user)
 {
     user.DateModified = DateTime.Now;
     context.Entry(user).State = EntityState.Modified;
 }
コード例 #8
0
 public void InsertUser(User user)
 {
     user.DateAdded = DateTime.Now;
     context.Users.Add(user);
 }