Beispiel #1
0
        public StudentDTOForAdmin UpdateStudentParent(string id, string parentId)
        {
            Student found = db.StudentsRepository.GetByID(id);

            if (found == null)
            {
                throw new HttpException("The student with id: " + id + " was not found.");
            }

            Parent oldParent = db.ParentsRepository.GetByID(found.Parent.Id);

            if (oldParent == null)
            {
                throw new HttpException("The student with id: " + id + " dont have a gardian - not possible.");
            }

            Parent foundParent = db.ParentsRepository.GetByID(parentId);

            found.Parent = foundParent ?? throw new HttpException("The parent with id: " + parentId + " was not found.");

            db.StudentsRepository.Update(found);

            IEnumerable <Student> oldParentStudents = oldParent.Students;

            if (oldParentStudents.Count() == 0)
            {
                db.ParentsRepository.Delete(oldParent);
            }

            db.Save();

            emailsService.CreateMailForParentNewStudentAssigned(found.Parent.Id);

            StudentDTOForAdmin updatedDTO = new StudentDTOForAdmin();

            updatedDTO = toDTO.ConvertToStudentDTOForAdmin(found, (List <IdentityUserRole>)found.Roles);

            return(updatedDTO);
        }
Beispiel #2
0
        public async Task <IdentityResult> RegisterStudentAndParent(RegisterStudentDTO studentDTO)
        {
            Parent parentInTheSystem = db.ParentsRepository.GetByJmbg(studentDTO.ParentJmbg);

            if (parentInTheSystem != null)
            {
                throw new HttpException("The parent with JMBG " + studentDTO.ParentJmbg + " is already in the system " +
                                        "with id: " + parentInTheSystem.Id + " Register student" +
                                        " with HttpPost at http://localhost:54164/project/account/register-student");
            }

            ApplicationUser foundJmbg = GetByJmbg(studentDTO.Jmbg);

            if (foundJmbg != null)
            {
                throw new HttpException("The user with JMBG " + studentDTO.Jmbg + " is already in the system with id " + foundJmbg.Id);
            }

            ApplicationUser foundUserName = await FindUserByUserName(studentDTO.UserName);

            if (foundUserName != null)
            {
                throw new HttpException("The username you entered for the student: " + studentDTO.UserName + " already exists.");
            }

            Form form = db.FormsRepository.GetByID(studentDTO.FormId);

            if (form == null)
            {
                throw new HttpException("Form id " + studentDTO.FormId + " was not found.");
            }

            ApplicationUser foundParentUserName = await FindUserByUserName(studentDTO.ParentUserName);

            if (foundParentUserName != null)
            {
                throw new HttpException("Username you entered for the parent " + studentDTO.ParentUserName + " already exists.");
            }

            RegisterParentDTO parentDTO = new RegisterParentDTO
            {
                UserName        = studentDTO.ParentUserName,
                FirstName       = studentDTO.ParentFirstName,
                LastName        = studentDTO.ParentLastName,
                Email           = studentDTO.ParentEmail,
                Password        = studentDTO.ParentPassword,
                ConfirmPassword = studentDTO.ParentConfirmPassword,
                Jmbg            = studentDTO.ParentJmbg,
                MobilePhone     = studentDTO.ParentMobilePhone
            };

            IdentityResult registeredParent = await RegisterParent(parentDTO);

            if (!registeredParent.Succeeded)
            {
                throw new HttpException("Failed to register the parent.");
            }

            Parent newParent = db.ParentsRepository.GetByUserName(studentDTO.ParentUserName);

            if (newParent == null)
            {
                throw new HttpException("Something went wrong.");
            }

            Student newUser = new Student
            {
                UserName   = studentDTO.UserName,
                FirstName  = studentDTO.FirstName,
                LastName   = studentDTO.LastName,
                Email      = studentDTO.Email,
                Jmbg       = studentDTO.Jmbg,
                DayOfBirth = studentDTO.DayOfBirth,
                IsActive   = true,
                Parent     = newParent,
                Form       = form
            };

            IdentityResult regStudent = await db.AuthRepository.RegisterStudent(newUser, studentDTO.Password);

            if (!regStudent.Succeeded)
            {
                throw new HttpException("Failed to register the student.");
            }

            emailsService.CreateRegistrationMail(newUser.Id, studentDTO.Password);
            emailsService.CreateRegistrationMail(newUser.Parent.Id, studentDTO.ParentPassword);
            emailsService.CreateMailForParentNewStudentAssigned(newUser.Parent.Id);
            return(regStudent);
        }