コード例 #1
0
ファイル: UserHandler.cs プロジェクト: Bartske/BrightLearn
        //Create


        //Read

        public User GetUser(string UserName, string Password)
        {
            if (UserName == "")
            {
                throw new Exception("There was no username given!");
            }

            if (Password == "")
            {
                throw new Exception("There was no password given!");
            }

            if (_dbContext.Login.Where(l => l.UserName == UserName && l.Password == Password).Count() == 0)
            {
                throw new Exception("There is no record of that user!");
            }
            Models.DataModels.Login U_L = _dbContext.Login.Where(l => l.UserName == UserName && l.Password == Password).First();
            int loginID = U_L.ID;

            Models.DataModels.User U = _dbContext.User.Where(u => u.LoginID == loginID).First();

            return(new User()
            {
                FirstName = U.FirstName,
                MiddleName = U.MiddleName,
                LastName = U.LastName,
                ID = U.ID,
                Email = U.Email,
                UserName = UserName,
                AccountType = U.Type,
            });
        }
コード例 #2
0
ファイル: UserHandler.cs プロジェクト: Bartske/BrightLearn
        public User GetUser(string FirstName, string MiddleName, string LastName)
        {
            if (FirstName == "" || LastName == "")
            {
                throw new Exception("You must fill in the firstname and lastname!");
            }

            if (_dbContext.User.Where(u => u.FirstName == FirstName && u.MiddleName == MiddleName && u.LastName == LastName).Count() == 0)
            {
                throw new Exception("There is no record of that user!");
            }

            Models.DataModels.User User = _dbContext.User.Where(u => u.FirstName == FirstName && u.MiddleName == MiddleName && u.LastName == LastName).Last();
            string UserName             = _dbContext.Login.Where(l => l.ID == User.LoginID).Last().UserName;

            return(new User()
            {
                FirstName = User.FirstName,
                MiddleName = User.MiddleName,
                LastName = User.LastName,
                ID = User.ID,
                Email = User.Email,
                UserName = UserName,
                AccountType = User.Type,
            });
        }
コード例 #3
0
        public void Test_For_Edit_User(string userIdToEdit, bool expectedResult)
        {
            // Arrange
            var userVMToEdit = new Models.ViewModels.User()
            {
                UserId = userIdToEdit, FirstName = "TestUserName", LastName = "TestUserLastName", Id = Guid.NewGuid()
            };

            Models.DataModels.User userDMToGet;
            if (expectedResult)
            {
                userDMToGet = new Models.DataModels.User()
                {
                    Id = userVMToEdit.Id, UserId = userVMToEdit.UserId, FirstName = userVMToEdit.FirstName, LastName = userVMToEdit.LastName
                }
            }
            ;
            else
            {
                userDMToGet = null;
            }

            mockUserRepo.Setup(repo => repo.GetById(userIdToEdit)).Returns(userDMToGet);
            mockUserRepo.Setup(repo => repo.Update(It.IsAny <Models.DataModels.User>())).Returns(expectedResult);
            // Act
            var actualResult = userLogicTest.EditUser(userIdToEdit, userVMToEdit);

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }
コード例 #4
0
        public void Test_For_Converter_FromViewModel_To_DataModel(bool passModelForConversion)
        {
            var expectedGuid      = Guid.NewGuid();
            var testUserViewModel = new Models.ViewModels.User()
            {
                UserId = "TestUser1", FirstName = "TestFirstName1", LastName = "TestLastName1", Id = expectedGuid
            };
            var testUserDataModel = new Models.DataModels.User()
            {
                UserId    = "AsIsUser1",
                FirstName = "AsIsFirstName",
                LastName  = "AsIsLastName",
                Created   = DateTime.Today.AddDays(-1),
                Id        = Guid.NewGuid()
            };

            Models.DataModels.User actualResult;
            if (passModelForConversion)
            {
                actualResult = testUserViewModel.AsDataModel(It.IsAny <bool>(), testUserDataModel);
            }
            else
            {
                actualResult = testUserViewModel.AsDataModel(It.IsAny <bool>());
            }

            Assert.Equal(expectedGuid, actualResult.Id);
            Assert.Equal(testUserViewModel.FirstName, actualResult.FirstName);
            Assert.Equal(testUserViewModel.LastName, actualResult.LastName);
            Assert.Equal(testUserViewModel.UserId, actualResult.UserId);
        }
コード例 #5
0
ファイル: UserHandler.cs プロジェクト: Bartske/BrightLearn
        public User GetUser(string Email)
        {
            if (Email == "")
            {
                throw new Exception("Email must be filled in!");
            }

            if (_dbContext.User.Where(u => u.Email == Email).Count() == 0)
            {
                throw new Exception("There is no record of that user!");
            }

            Models.DataModels.User User = _dbContext.User.Where(u => u.Email == Email).Last();
            string UserName             = _dbContext.Login.Where(l => l.ID == User.LoginID).Last().UserName;

            return(new User()
            {
                FirstName = User.FirstName,
                MiddleName = User.MiddleName,
                LastName = User.LastName,
                ID = User.ID,
                Email = User.Email,
                UserName = UserName,
                AccountType = User.Type,
            });
        }
コード例 #6
0
ファイル: UserHandler.cs プロジェクト: Bartske/BrightLearn
        //Update

        public bool UpdateUser(User User)
        {
            if (User.FirstName == "" || User.LastName == "" || User.Email == "" || User.AccountType == "" || User.UserName == "")
            {
                throw new Exception("All the values must be filled in!");
            }
            if (_dbContext.Login.Where(l => l.UserName == User.UserName).Count() > 0)
            {
                throw new Exception("This username already exists!");
            }

            if (_dbContext.User.Where(u => u.ID == User.ID).Count() == 0)
            {
                throw new Exception("There is no record of that user!");
            }

            Models.DataModels.User  NewUser = _dbContext.User.Where(u => u.ID == User.ID).First();
            Models.DataModels.Login Login   = _dbContext.Login.Where(l => l.ID == NewUser.LoginID).First();

            NewUser.Email      = User.Email;
            NewUser.FirstName  = User.FirstName;
            NewUser.LastName   = User.LastName;
            NewUser.MiddleName = User.MiddleName;
            NewUser.Type       = User.AccountType;

            Login.UserName = User.UserName;

            _dbContext.SaveChanges();

            return(true);
        }
コード例 #7
0
        public void Test_For_Converting_AsDataModel_List()
        {
            // Arrange
            var usersVMList = new Models.ViewModels.User[]
            {
                new Models.ViewModels.User()
                {
                    UserId = "TestUser1", FirstName = "TestFirstName1", LastName = "TestLastName1", Id = Guid.NewGuid()
                },
                new Models.ViewModels.User()
                {
                    UserId = "TestUser2", FirstName = "TestFirstName2", LastName = "TestLastName2", Id = Guid.NewGuid()
                },
                new Models.ViewModels.User()
                {
                    UserId = "TestUser3", FirstName = "TestFirstName3", LastName = "TestLastName3", Id = Guid.NewGuid()
                },
                new Models.ViewModels.User()
                {
                    UserId = "TestUser4", FirstName = "TestFirstName4", LastName = "TestLastName4", Id = Guid.NewGuid()
                },
                new Models.ViewModels.User()
                {
                    UserId = "TestUser5", FirstName = "TestFirstName5", LastName = "TestLastName5", Id = Guid.NewGuid()
                },
            }.AsEnumerable();

            var testDataModelList = new Models.DataModels.User[] {
                new Models.DataModels.User()
                {
                    UserId = "As-Is-User1", FirstName = "As-Is-FirstName1", LastName = "As-Is-LastName1", Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "As-Is-User2", FirstName = "As-Is-FirstName2", LastName = "As-Is-LastName2", Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "As-Is-User3", FirstName = "As-Is-FirstName3", LastName = "As-Is-LastName3", Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "As-Is-User4", FirstName = "As-Is-FirstName4", LastName = "As-Is-LastName4", Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "As-Is-User5", FirstName = "As-Is-FirstName5", LastName = "As-Is-LastName5", Id = Guid.NewGuid()
                },
            }.AsEnumerable();

            // Act
            var actualDataModelResult = usersVMList.AsDataModel(testDataModelList);

            // Assert
            Assert.Equal(usersVMList.Select(t => t.UserId), actualDataModelResult.Select(t => t.UserId));
            Assert.Equal(usersVMList.Select(t => t.Id), actualDataModelResult.Select(t => t.Id));
        }
コード例 #8
0
        public static Models.ViewModels.User AsViewModel(this Models.DataModels.User userData)
        {
            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap <Models.DataModels.User, Models.ViewModels.User>()
                .ForMember(vm => vm.Active, d => d.MapFrom(m => !(m.EndDate.HasValue && m.EndDate.Value.CompareTo(System.DateTime.Now) <= 0)))
                .ReverseMap();
            });

            return(config.CreateMapper().Map <Models.DataModels.User, Models.ViewModels.User>(userData));
        }
コード例 #9
0
        public User GenerateUser()
        {
            string UserName = "";

            while (true)
            {
                UserName = RandomString(5);
                if (dbContext.Login.Where(l => l.UserName == UserName).Count() == 0)
                {
                    break;
                }
            }

            string Password = "******";
            string SaltKey  = EncryptHandler.RandomString(8);

            Models.DataModels.Login login = new Models.DataModels.Login()
            {
                UserName = UserName,
                Password = Password,
                Salt     = SaltKey
            };
            dbContext.SaveChanges();
            int loginID = dbContext.Login.Last().ID;

            //SQL.Insert("INSERT INTO `login` (`ID`, `username`, `password`, `salt`) VALUES (NULL, '" + UserName + "', '" + EncryptHandler.Encrypt(Password, SaltKey) + "', '" + SaltKey + "');");

            //string loginID = SQL.Select("SELECT `ID` FROM `login` WHERE `username` = '"+UserName+"'")[0];

            Models.DataModels.User user = new Models.DataModels.User()
            {
                Email     = "*****@*****.**",
                FirstName = "Voornaam",
                LastName  = "Achternaam",
                LoginID   = loginID,
                Type      = "user"
            };
            dbContext.User.Add(user);
            dbContext.SaveChanges();

            //SQL.Insert("INSERT INTO `user` (`ID`, `loginID`, `email`, `firstName`, `middleName`, `lastName`, `type`) VALUES (NULL, '"+loginID+"', '*****@*****.**', 'Voornaam', NULL, 'Achternaam', 'user');");

            return(new User()
            {
                UserName = UserName,
                Password = Password
            });
        }
コード例 #10
0
        public void Test_For_Search_Users(string searchText, bool exactMatch, string fieldName)
        {
            // Arrange
            var testSearchResults = new Models.DataModels.User[]
            {
                new Models.DataModels.User()
                {
                    UserId = "User1", FirstName = "TestFirstName1", LastName = "TestLastName1", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User2", FirstName = "TestFirstName2", LastName = "TestLastName2", Created = DateTime.Today.AddDays(-5), Id = Guid.NewGuid(), EndDate = DateTime.Today
                },
                new Models.DataModels.User()
                {
                    UserId = "User3", FirstName = "TestFirstName3", LastName = "TestLastName3", Created = DateTime.Today.AddMonths(-8), Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User4", FirstName = "TestFirstName4", LastName = "TestLastName4", Created = DateTime.Today.AddYears(-3), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddMonths(-5)
                },
                new Models.DataModels.User()
                {
                    UserId = "User5", FirstName = "TestFirstName5", LastName = "TestLastName5", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User6", FirstName = "TestFirstName6", LastName = "TestLastName6", Created = DateTime.Today.AddMonths(-10), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddMonths(-1)
                },
                new Models.DataModels.User()
                {
                    UserId = "User7", FirstName = "TestFirstName7", LastName = "TestLastName7", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User8", FirstName = "TestFirstName8", LastName = "TestLastName8", Created = DateTime.Today.AddDays(-10), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddDays(-1)
                },
            };

            mockUserRepo.Setup(repo => repo.Search(It.IsAny <System.Linq.Expressions.Expression <Func <Models.DataModels.User, bool> > >())).Returns(testSearchResults);
            // Act
            var actualResult = userLogicTest.Search(searchText, exactMatch, fieldName);

            // Assert
            Assert.NotNull(actualResult);
            Assert.Equal(testSearchResults.Count(), actualResult.Count());
        }
コード例 #11
0
        public void Test_For_Get_User_By_Id(string testUserIdToGet)
        {
            // Arrange
            var expectedUser = new Models.DataModels.User()
            {
                UserId = testUserIdToGet, FirstName = "TestFirstName", LastName = "TestLastName", Created = DateTime.Today, Id = Guid.NewGuid()
            };

            mockUserRepo.Setup(repo => repo.GetById(testUserIdToGet)).Returns(expectedUser);
            // Act
            var actualUserResult = userLogicTest.GetUserById(testUserIdToGet);

            // Assert
            Assert.NotNull(actualUserResult);
            Assert.NotEqual(Guid.Empty, actualUserResult.Id);
            Assert.True(actualUserResult.Active);
        }
コード例 #12
0
ファイル: ManageHandler.cs プロジェクト: Bartske/BrightLearn
        public User GenerateUser()
        {
            string UserName = "";

            while (true)
            {
                UserName = RandomString(5);
                if (_dbContext.Login.Where(l => l.UserName == UserName).Count() == 0)
                {
                    break;
                }
            }

            string Password = "******";
            string SaltKey  = EncryptHandler.RandomString(8);
            string newPass  = EncryptHandler.Encrypt(Password, SaltKey);

            Models.DataModels.Login login = new Models.DataModels.Login()
            {
                UserName = UserName,
                Password = newPass,
                Salt     = SaltKey
            };
            _dbContext.Login.Add(login);
            _dbContext.SaveChanges();
            login = _dbContext.Login.OrderByDescending(u => u.ID).FirstOrDefault();

            Models.DataModels.User User = new Models.DataModels.User()
            {
                Email     = "*****@*****.**",
                FirstName = "Voornaam",
                LastName  = "Achternaam",
                LoginID   = login.ID,
                Type      = "user"
            };
            _dbContext.User.Add(User);
            _dbContext.SaveChanges();

            return(new User()
            {
                UserName = UserName,
                Password = Password
            });
        }
コード例 #13
0
ファイル: UserHandler.cs プロジェクト: Bartske/BrightLearn
        public User GetUser(int ID)
        {
            if (dbContext.User.Where(u => u.ID == ID).Count() == 0)
            {
                throw new Exception("There is no record of that user!");
            }


            Models.DataModels.User U = dbContext.User.Where(u => u.ID == ID).ToList().Last();
            string UserName          = dbContext.Login.Where(l => l.ID == U.LoginID).ToList().Last().UserName;

            return(new User()
            {
                FirstName = U.FirstName,
                MiddleName = U.MiddleName,
                LastName = U.LastName,
                ID = U.ID,
                Email = U.Email,
                UserName = UserName,
                AccountType = U.Type,
            });
        }
コード例 #14
0
        public void Test_For_AddUser()
        {
            // Arrange
            var testUserInfoToAdd = new Models.ViewModels.User()
            {
                UserId = "TestUser1", FirstName = "TestFirstName", LastName = "TestLastName"
            };
            var expectedUserInfo = new Models.DataModels.User()
            {
                UserId = "TestUser1", FirstName = "TestFirstName", LastName = "TestLastName", Id = Guid.NewGuid(), Created = DateTime.Today
            };

            mockUserRepo.Setup(repo => repo.Create(It.IsAny <Models.DataModels.User>())).Returns(expectedUserInfo);

            // Act
            var actualResult = userLogicTest.AddUser(testUserInfoToAdd);

            // Assert
            Assert.NotNull(actualResult);
            Assert.NotEqual(Guid.Empty, actualResult.Id);
            Assert.Equal(expectedUserInfo.UserId, actualResult.UserId);
            Assert.True(actualResult.Active);
        }
コード例 #15
0
        public int VerifyUser(UserDetails userDetails)
        {
            var user = Db.FirstOrDefault <Models.DataModels.User>("WHERE [Username] = @0", userDetails.Username);

            if (user != null)
            {
                return(user.Id);
            }

            user = new Models.DataModels.User
            {
                Username    = userDetails.Username,
                Name        = userDetails.Name,
                Designation = userDetails.Designation,
                Team        = userDetails.Team,
                Category    = userDetails.Category,
                Location    = userDetails.Location
            };

            Db.Insert("Users", user);
            user = Db.FirstOrDefault <Models.DataModels.User>("WHERE [Username] = @0", userDetails.Username);

            return(user.Id);
        }
コード例 #16
0
ファイル: ManageHandler.cs プロジェクト: Bartske/BrightLearn
 public void UpdateUserType(int ID, string Type)
 {
     Models.DataModels.User User = _dbContext.User.Where(u => u.ID == ID).First();
     User.Type = Type;
     _dbContext.SaveChanges();
 }
コード例 #17
0
 public void UpdateUserType(int ID, string type)
 {
     Models.DataModels.User usr = dbContext.User.Where(u => u.ID == ID).First();
     usr.Type = type;
     dbContext.SaveChanges();
 }
コード例 #18
0
        public static Models.DataModels.User AsDataModel(this Models.ViewModels.User userData, bool isCreate = false, Models.DataModels.User dataModel = null)
        {
            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap <Models.ViewModels.User, Models.DataModels.User>();
            });

            var mapper = config.CreateMapper();

            if (isCreate)
            {
                return(mapper.Map <Models.ViewModels.User, Models.DataModels.User>(userData,
                                                                                   opts => opts.ConfigureMap()
                                                                                   .ForMember(m => m.Created, d => d.AddTransform(v => System.DateTime.Now))));
            }

            if (dataModel != null)
            {
                return(mapper.Map(userData, dataModel));
            }
            else
            {
                return(mapper.Map <Models.DataModels.User>(userData));
            }
        }
コード例 #19
0
        public void Test_For_Get_All_Users(bool isActiveOnly, int expectedTotal)
        {
            // Arrange
            var testUsersList = new Models.DataModels.User[]
            {
                new Models.DataModels.User()
                {
                    UserId = "User1", FirstName = "TestFirstName1", LastName = "TestLastName1", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User2", FirstName = "TestFirstName2", LastName = "TestLastName2", Created = DateTime.Today.AddDays(-5), Id = Guid.NewGuid(), EndDate = DateTime.Today
                },
                new Models.DataModels.User()
                {
                    UserId = "User3", FirstName = "TestFirstName3", LastName = "TestLastName3", Created = DateTime.Today.AddMonths(-8), Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User4", FirstName = "TestFirstName4", LastName = "TestLastName4", Created = DateTime.Today.AddYears(-3), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddMonths(-5)
                },
                new Models.DataModels.User()
                {
                    UserId = "User5", FirstName = "TestFirstName5", LastName = "TestLastName5", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User6", FirstName = "TestFirstName6", LastName = "TestLastName6", Created = DateTime.Today.AddMonths(-10), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddMonths(-1)
                },
                new Models.DataModels.User()
                {
                    UserId = "User7", FirstName = "TestFirstName7", LastName = "TestLastName7", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User8", FirstName = "TestFirstName8", LastName = "TestLastName8", Created = DateTime.Today.AddDays(-10), Id = Guid.NewGuid(), EndDate = DateTime.Today.AddDays(-1)
                },
                new Models.DataModels.User()
                {
                    UserId = "User9", FirstName = "TestFirstName9", LastName = "TestLastName9", Created = DateTime.Today, Id = Guid.NewGuid(), EndDate = null
                },
                new Models.DataModels.User()
                {
                    UserId = "User10", FirstName = "TestFirstName10", LastName = "TestLastName10", Created = DateTime.Today, Id = Guid.NewGuid(), EndDate = DateTime.MinValue
                },
            };
            var activeList = new Models.DataModels.User[]
            {
                new Models.DataModels.User()
                {
                    UserId = "User1", FirstName = "TestFirstName1", LastName = "TestLastName1", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User3", FirstName = "TestFirstName3", LastName = "TestLastName3", Created = DateTime.Today.AddMonths(-8), Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User5", FirstName = "TestFirstName5", LastName = "TestLastName5", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User7", FirstName = "TestFirstName7", LastName = "TestLastName7", Created = DateTime.Today, Id = Guid.NewGuid()
                },
                new Models.DataModels.User()
                {
                    UserId = "User9", FirstName = "TestFirstName1", LastName = "TestLastName1", Created = DateTime.Today, Id = Guid.NewGuid(), EndDate = null
                },
                new Models.DataModels.User()
                {
                    UserId = "User10", FirstName = "TestFirstName10", LastName = "TestLastName10", Created = DateTime.Today, Id = Guid.NewGuid(), EndDate = DateTime.MinValue
                },
            };

            if (isActiveOnly)
            {
                mockUserRepo.Setup(repo => repo.GetAll()).Returns(activeList);
            }
            else
            {
                mockUserRepo.Setup(repo => repo.GetAll()).Returns(testUsersList);
            }

            // Act
            var actualList = userLogicTest.GetUsers(isActiveOnly);

            // Assert
            Assert.NotNull(actualList);
            Assert.Equal(expectedTotal, actualList.Count());
        }