コード例 #1
0
        public bool AddStudent(Student studentToAdd)
        {
            studentToAdd.User.Username = CredentialsHelper.GenerateUsername(studentToAdd.FirstName, studentToAdd.LastName);
            studentToAdd.User.Password = HashHelper.Hash(CredentialsHelper.GenerateRandomPassword());
            studentToAdd.User.Role     = Role.Student;

            if (_context.Users.Any(u => u.Username == studentToAdd.User.Username))
            {
                return(false);
            }

            _context.Students.Add(studentToAdd);
            _context.SaveChanges();
            return(true);
        }
コード例 #2
0
        public bool AddInstructor(Instructor instructorToAdd)
        {
            if (_context.Users.Any(u => u.Username == instructorToAdd.User.Username))
            {
                return(false);
            }

            var vehicle = _context.Vehicles.FirstOrDefault(v => instructorToAdd.Vehicle.Model.Contains(v.Manufacturer) &&
                                                           instructorToAdd.Vehicle.Model.Contains(v.Model) && instructorToAdd.Vehicle.Year == v.Year);

            if (vehicle == null)
            {
                return(false);
            }

            instructorToAdd.User.Username = CredentialsHelper.GenerateUsername(instructorToAdd.FirstName, instructorToAdd.LastName);
            instructorToAdd.User.Password = HashHelper.Hash(CredentialsHelper.GenerateRandomPassword());
            instructorToAdd.User.Role     = Role.Instructor;

            _context.Instructors.Add(instructorToAdd);
            _context.SaveChanges();
            return(true);
        }