Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public IActionResult AddInstructor(Instructor instructorToAdd)
        {
            var accessTokenAsString = JwtHelper.GetTokenSubstring(Request.Headers["Authorization"].ToString());

            if (accessTokenAsString == "null")
            {
                return(Unauthorized());
            }
            var userCredentials = JwtHelper.GetCredentialsFromToken(accessTokenAsString);

            var fullName = CredentialsHelper.GenerateUsername(instructorToAdd.FirstName, instructorToAdd.LastName);

            instructorToAdd.User.DrivingSchoolId = userCredentials.Id;

            var wasAddSuccessful = _instructorRepository.AddInstructor(instructorToAdd);

            if (wasAddSuccessful)
            {
                return(Ok());
            }

            return(Forbid());
        }
Exemplo n.º 3
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);
        }