コード例 #1
0
        public void UserAppointmentController()
        {
            //Assemble
            var controller      = new UserAppointmentController();
            var userAppointment = new UsersAppointment
            {
                Id       = 1,
                Create   = DateTime.Now,
                customer = new Customer {
                    Id = 1, Name = "John Meyor", Email = "*****@*****.**", PhoneNumber = "4172583698"
                },
                provider = new Provider {
                    Id = 1, Name = "Mia Simpson", JobTitle = "Hair Stylist"
                }
            };

            // Act
            var result = controller.Create(userAppointment);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
コード例 #2
0
        public void Add(UsersAppointment usersAppointment)
        {
            //Method to Check Conflict in Appointments
            checkAppointment(usersAppointment);
            _spaAppContext.UsersAppointments.Add(usersAppointment);
            _spaAppContext.SaveChanges();

            /*Using Transaction when there is More than one Contexts*/

            //using (var transaction = _spaAppContext.Database.BeginTransaction())
            //{
            //    try
            //    {
            //        checkAppointment(usersAppointment);
            //        _spaAppContext.UsersAppointments.Add(usersAppointment);
            //        _spaAppContext.SaveChanges();

            //        transaction.Commit();
            //    }

            //     catch (Exception ex)
            //    {
            //        throw new Exception("Transaction failed", ex);
            //    }
            //}
        }
コード例 #3
0
        public void UpdateMethod_Repository()
        {
            //ASSEMBLE
            var testAppointment = new UsersAppointment
            {
                Id       = 1,
                Create   = DateTime.Now,
                Customer = new Customer {
                    Id = 1, Name = "John Meyor", Email = "*****@*****.**", PhoneNumber = "4172583698"
                },
                Provider = new Provider {
                    Id = 1, Name = "Mia Simpson", JobTitle = "Hair Stylist"
                }
            };


            //ACT
            Repository.Update(1, testAppointment);
            var result = Repository.usersAppointments.FirstOrDefault(x => x.Id == testAppointment.Id);

            //ASSERT

            Assert.Equal(testAppointment.Customer.Name, result.Customer.Name);
            Assert.Equal(testAppointment.Customer.Id, result.Customer.Id);
            Assert.Equal(testAppointment.Create, result.Create);
            Assert.Equal(testAppointment.Provider.Name, result.Provider.Name);
            Assert.Equal(testAppointment.Provider.Id, result.Provider.Id);
        }
コード例 #4
0
        public void Update(int id, UsersAppointment usersAppointment)
        {
            usersAppointment.Id = id;

            checkAppointment(usersAppointment);

            _spaAppContext.UsersAppointments.Update(usersAppointment);
            _spaAppContext.SaveChanges();
        }
コード例 #5
0
        public static void Add(UsersAppointment usersAppointment)
        {
            usersAppointment.Id       = Interlocked.Increment(ref userKeyCounter);
            usersAppointment.customer = _customer.Find(x => x.Id == usersAppointment.customer?.Id);
            usersAppointment.provider = _provider.Find(x => x.Id == usersAppointment.provider?.Id);

            checkAppointment(usersAppointment);

            _usersAppointment.Add(usersAppointment);
        }
コード例 #6
0
 public ActionResult Delete(int id, UsersAppointment usersAppointment)
 {
     try
     {
         // TODO: Add delete logic here
         Repository.DeleteUsersAppointment(id);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
コード例 #7
0
        public static void Update(int id, UsersAppointment usersAppointment)
        {
            var index = _usersAppointment.FindIndex(x => x.Id == id);

            _usersAppointment.RemoveAt(index);
            usersAppointment.Id       = id;
            usersAppointment.customer = _customer.Find(x => x.Id == usersAppointment.customer?.Id);
            usersAppointment.provider = _provider.Find(x => x.Id == usersAppointment.provider?.Id);

            checkAppointment(usersAppointment);

            _usersAppointment.Insert(index, usersAppointment);
        }
コード例 #8
0
 public ActionResult Edit(int id, UsersAppointment usersAppointment)
 {
     try
     {
         // TODO: Add update logic here
         Repository.Update(id, usersAppointment);
         return(RedirectToAction(nameof(Index)));
     }
     catch (Exception e)
     {
         ModelState.AddModelError("", e.Message);
         return(View());
     }
 }
コード例 #9
0
 public ActionResult Create(UsersAppointment userAppointment)
 {
     try
     {
         // TODO: Add insert logic here
         Repository.Add(userAppointment);
         return(RedirectToAction(nameof(Index)));
     }
     catch (Exception e)
     {
         ModelState.AddModelError("", e.Message);
         return(View());
     }
 }
コード例 #10
0
        public void  checkAppointment(UsersAppointment usersAppointment)
        {
            foreach (var x in _readOnlyspaAppContext.UserAppointments.Include(x => x.Customer).Include(x => x.Provider))
            {
                if (x.CustomerId.Equals(usersAppointment.CustomerId) && x.Create.Equals(usersAppointment.Create))
                {
                    throw new Exception("Customer has a conflicting appointment.");
                }

                if (x.ProviderId.Equals(usersAppointment.ProviderId) && x.Create.Equals(usersAppointment.Create))
                {
                    throw new ArgumentException("Provider has a conflicting appointment.");
                }
            }
        }
コード例 #11
0
        public static void  checkAppointment(UsersAppointment usersAppointment)
        {
            foreach (var x in _usersAppointment)
            {
                if (x.customer.Id.Equals(usersAppointment.customer.Id) && x.Create.Equals(usersAppointment.Create))
                {
                    throw new Exception("Customer has a conflicting appointment.");
                }

                if (x.provider.Id.Equals(usersAppointment.provider.Id) && x.Create.Equals(usersAppointment.Create))
                {
                    throw new ArgumentException("Provider has a conflicting appointment.");
                }
            }
        }
コード例 #12
0
        public void AddAppointmentMethod_ThrowsExceptionWhenProviderIsNotAvailable()
        {
            // ARRANGE
            var newAppt = new UsersAppointment
            {
                Id         = 2,
                Create     = new DateTime(2018, 12, 1, 11, 00, 00),
                ProviderId = 1,
                CustomerId = 1
            };
            var testRepo = new Repository(CreateSpaAppContext(), CreateTestReadOnlyContext());

            //ACT & ASSERT
            Assert.Throws <System.Exception>(() => testRepo.Add(newAppt));
        }
コード例 #13
0
        public void UserAppointmentController_Create_Test()
        {
            var userAppointment = new UsersAppointment();

            // Arrange
            _mockRepo.Setup(c => c.Add(userAppointment));


            // Act
            var result = _userController.Create(userAppointment);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
コード例 #14
0
        public void UserAppointmentController_Create_Error_Test()
        {
            var userAppointment = new UsersAppointment()
            {
                Id         = 1,
                Create     = DateTime.Now,
                CustomerId = 1,
                ProviderId = 1
            };

            _mockRepo.Setup(c => c.Add(userAppointment)).Throws <Exception>();

            //Act
            var result = (ViewResult)_userController.Create(userAppointment);

            //Assert

            Assert.False(result.ViewData.ModelState.IsValid);
        }
コード例 #15
0
        public void UserAppointmentController_Edit_Test()
        {
            int Id = 1;
            var userAppointment = new UsersAppointment()
            {
                Id         = 1,
                Create     = DateTime.Now,
                CustomerId = 1,
                ProviderId = 1
            };

            _mockRepo.Setup(c => c.Update(Id, userAppointment));

            // Act
            var result = _userController.Edit(Id, userAppointment);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
コード例 #16
0
        public void AddAppointmentMethod_ThrowsNoException()
        {
            // ARRANGE
            var newAppt = new UsersAppointment
            {
                Id         = 1,
                Create     = new DateTime(2018, 12, 1, 11, 00, 00),
                ProviderId = 2
            };
            var testRepo = new Repository(CreateSpaAppContext(), CreateTestReadOnlyContext());

            //ACT
            try
            {
                testRepo.Add(newAppt);
            }
            //ASSERT
            catch (Exception)
            {
                Assert.True(false, "Succeed");
            }
        }