Ejemplo n.º 1
0
        public async Task <IActionResult> RegisterStudent(StudentForRegisterDto studentForRegisterDto)
        {
            studentForRegisterDto.Username = studentForRegisterDto.Username;
            if (await _repository.UserExists(studentForRegisterDto.Username))
            {
                return(BadRequest("Użytkownik o takiej nazwie już istnieje !"));
            }
            var schoolClass = _context.Classes.FirstOrDefault(x => x.Name == studentForRegisterDto.SchoolClass);

            if (schoolClass == null)
            {
                return(BadRequest("Nie ma takiej klasy"));
            }
            var studentToCreate = new Student
            {
                Username      = studentForRegisterDto.Username,
                FirstName     = studentForRegisterDto.FirstName,
                LastName      = studentForRegisterDto.LastName,
                BirthDate     = studentForRegisterDto.BirthDate,
                Address       = studentForRegisterDto.Address,
                Phone         = studentForRegisterDto.Phone,
                SchoolClassID = schoolClass.ClassID,
                UserRole      = "Student"
            };

            var createdUser = await _repository.Register(studentToCreate, studentForRegisterDto.Password);

            return(StatusCode(201));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> StudentRegister(StudentForRegisterDto userForRegisterDto)
        {
            // validate request

            userForRegisterDto.Username = userForRegisterDto.Username.ToLower();

            if (await _studentRepo.UserExists(userForRegisterDto.Username))
            {
                return(BadRequest("Username already exists"));
            }

            Random random            = new Random();
            var    randomPhotoNumber = random.Next(1, 100);

            var userToCreate = new Student
            {
                Username       = userForRegisterDto.Username,
                Name           = userForRegisterDto.Name,
                Surname        = userForRegisterDto.Surname,
                PhotoURL       = "https://randomuser.me/api/portraits/men/" + randomPhotoNumber + ".jpg",
                Created        = DateTime.Now,
                EnrollmentDate = DateTime.Now
            };

            var createdUser = await _studentRepo.Register(userToCreate, userForRegisterDto.Password);

            // role
            var r = _context.Roles.FirstOrDefault(x => x.Name == "Student");

            if (r != null)
            {
                var sr = new StudentRole {
                    Student = createdUser, Role = r
                };
                _context.StudentRoles.AddAsync(sr).Wait();
                _context.SaveChangesAsync().Wait();
            }
            //

            return(StatusCode(201));
        }