コード例 #1
0
        /*
         * public IActionResult SearchForPetsUserInput()
         * {
         *  //Dynamic drop down list (Clients from database)
         *
         *  ViewData["AllClients"] = new SelectList(iPetRepo.ListAllClients(),"Id" , "Fullname"); //list of items, Value, Text
         *
         *  SearchForPetsViewModel searchForPetsViewModel = new SearchForPetsViewModel();
         *
         *  return View(searchForPetsViewModel);
         * }
         */

        public IActionResult SearchForPets(SearchForPetsViewModel viewModel)

        //(string clientID, string petType, DateTime? startDate, DateTime? endDate)
        {
            ViewData["AllClients"] = new SelectList(iPetRepo.ListAllClients(), "Id", "Fullname");

            //is this the first time the user has visited this view?

            List <Pet> searchList;

            if (viewModel.UserFirstVisit != "No")
            {
                searchList = null;
            }
            else
            {
                searchList = iPetRepo.ListAllPets();


                if (!string.IsNullOrEmpty(viewModel.ClientID))
                {
                    searchList = searchList.Where(p => p.ClientID == viewModel.ClientID).ToList();
                }


                if (!String.IsNullOrEmpty(viewModel.PetType))
                {
                    searchList = searchList.Where(p => p.PetType == viewModel.PetType).ToList();
                }

                if (viewModel.StartDate.HasValue)
                {
                    searchList = searchList.Where(p => p.VoucherRequestsForPet.Any(vr => vr.RequestDecisionDate >= viewModel.StartDate.Value.Date)).ToList();
                }

                //return View(searchList);

                if (viewModel.EndDate.HasValue)
                {
                    searchList = searchList.Where(p => p.VoucherRequestsForPet.Any(vr => vr.RequestDecisionDate <= viewModel.EndDate.Value.Date)).ToList();
                }
            }

            //1. create an object of the SearchForPetsViewModel  (use the parameter object)
            //SearchForPetsViewModel searchForPetsViewModel = new SearchForPetsViewModel();
            //2. Assign
            viewModel.ResultPetList = searchList;

            return(View(viewModel));
        }
コード例 #2
0
        public void ShouldSearchForPetsByOwner()
        {
            //AAA
            //1. Arrange
            mockPetRepo = new Mock <IPetRepo>();

            List <Pet> mockPets = CreateMockPetData();

            mockPetRepo.Setup(m => m.ListAllPets()).Returns(mockPets); //Logic will be in the controller method

            mockPetRepo.Setup(m => m.ListAllClients()).Returns(new List <Client>());

            int expectedNumberOfPetsInList = 2;

            PetController petController = new PetController(mockPetRepo.Object);

            //DropDownList for owners [Text = Full name of the owner ? Value = Id]
            string   clientID  = "001";
            string   petType   = null;
            DateTime?startDate = null;
            DateTime?endDate   = null;

            SearchForPetsViewModel viewModel = new SearchForPetsViewModel();

            viewModel.ClientID       = clientID;
            viewModel.PetType        = petType;
            viewModel.StartDate      = startDate;
            viewModel.EndDate        = endDate;
            viewModel.UserFirstVisit = "No";

            //2. Act
            ViewResult             result      = petController.SearchForPets(viewModel) as ViewResult;
            SearchForPetsViewModel resultModel = result.Model as SearchForPetsViewModel;
            int actualNumberOfPetsInList       = resultModel.ResultPetList.Count;

            //3. Assert
            Assert.Equal(expectedNumberOfPetsInList, actualNumberOfPetsInList);
        }