コード例 #1
0
        //provide double filter to search user by first name, last name or together
        public SearchUserByNameModel ServSearchUserByName(string firstName, string lastName)
        {
            //stores all user information
            var name = _repoUserInfo.GetAllUserInfo();
            SearchUserByNameModel userOrder = new SearchUserByNameModel();

            //if both first name and last name is empty display no name
            if (string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(lastName))
            {
                name = name.Where(x => x.fName.Contains("0"));
                userOrder.userInfos = name.ToList();
            }
            //if something is entered into first name search any first name that contain that letter
            if (!string.IsNullOrEmpty(firstName))
            {
                name = name.Where(x => x.fName.Contains(firstName.ToLower()));
            }
            //if something is entered into last name search any last name that contain that letter
            if (!string.IsNullOrEmpty(lastName))
            {
                name = name.Where(x => x.lName.Contains(lastName.ToLower()));
            }
            //store the list of names that match into a list
            userOrder.userInfos = name.ToList();
            return(userOrder);
        }
コード例 #2
0
        public void TestServSearchUserByName()
        {
            var options = new DbContextOptionsBuilder <Project1Context>()
                          .UseInMemoryDatabase(databaseName: "Test16")
                          .Options;
            UserInfo newUser = new UserInfo()
            {
                fName    = "david",
                lName    = "leblanc",
                userName = "******",
                password = "******"
            };
            UserInfo newUser1 = new UserInfo()
            {
                fName    = "james",
                lName    = "jones",
                userName = "******",
                password = "******"
            };
            string firstName = "James";
            string lastName  = "Jones";
            IEnumerable <UserInfo> name;
            SearchUserByNameModel  userOrder = new SearchUserByNameModel();

            //Act
            using (var db1 = new Project1Context(options))
            {
                //adds user to the table
                db1.Add(newUser);
                db1.Add(newUser1);
                db1.SaveChanges();
                name = db1.UserInfos;
                //if both first name and last name is empty display no name
                if (string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(lastName))
                {
                    name = name.Where(x => x.fName.Contains("0"));
                    userOrder.userInfos = name.ToList();
                }
                //if something is entered into first name search any first name that contain that letter
                else if (!string.IsNullOrEmpty(firstName))
                {
                    name = name.Where(x => x.fName.Contains(firstName.ToLower()));
                }
                //if something is entered into last name search any last name that contain that letter
                else if (!string.IsNullOrEmpty(lastName))
                {
                    name = name.Where(x => x.lName.Contains(lastName.ToLower()));
                }
                //store the list of names that match into a list
                userOrder.userInfos = name.ToList();
            }
            var selectedName = userOrder.userInfos.First().fName;

            //Assert
            Assert.Equal("james", selectedName);
        }