示例#1
0
        public async Task <bool> Create(StudentParentViewModel model)
        {
            try
            {
                //Create Student
                Student s = new Student();
                s.FirstName  = model.StudentFirstName;
                s.LastName   = model.StudentLastName;
                s.MiddleName = model.StudentMiddleName;
                s.Email      = model.StudentEmail;
                var studentres = await this._context.Students.AddAsync(s);


                //Create Father
                var father = new Parents();
                father.FirstName    = model.FatherFirstName;
                father.LastName     = model.FatherLastName;
                father.MiddleName   = model.FatherMiddleName;
                father.PhoneNumber  = model.FatherPhoneNumber;
                father.GuardianType = GuardianType.Father;
                var fatherres = await this._context.Parents.AddAsync(father);

                //Create Mother
                var mother = new Parents();
                mother.FirstName    = model.MotherFirstName;
                mother.LastName     = model.MotherLastName;
                mother.MiddleName   = model.MotherMiddleName;
                mother.PhoneNumber  = model.MotherPhoneNumber;
                mother.GuardianType = GuardianType.Mother;
                var motherres = await this._context.Parents.AddAsync(mother);


                //commit the changes
                await this._context.SaveChangesAsync();

                //Create Relationship of student and father and mother
                var fatherstudent = new ParentStudent();
                fatherstudent.ParentId  = fatherres.Entity.Id;
                fatherstudent.StudentId = studentres.Entity.Id;
                await this._context.ParentStudents.AddAsync(fatherstudent);

                var motherstudent = new ParentStudent();
                motherstudent.ParentId  = motherres.Entity.Id;
                motherstudent.StudentId = studentres.Entity.Id;
                await this._context.ParentStudents.AddAsync(motherstudent);

                //commit the changes
                await this._context.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
示例#2
0
        public async Task SetStudentToParent(int parentId, int studentId)
        {
            ParentStudent parentsStudent = new ParentStudent
            {
                StudentId = studentId,
                ParentId  = parentId,
            };

            await this.parentStudentRepository.AddAsync(parentsStudent);

            await this.parentStudentRepository.SaveChangesAsync();
        }
示例#3
0
        public async Task <bool> Create(ParentStudent model)
        {
            try
            {
                await this.context.ParentStudents.AddAsync(model);

                await this.context.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
示例#4
0
        public async Task <IOperationResult <User> > RegisterParentUser(User newUser, string password, int studentId)
        {
            var studentUser = await _userRepository.Set.Include(ur => ur.UserRoles)
                              .FirstOrDefaultAsync(u => u.Id == studentId);

            if (studentUser.UserRoles.All(ur => ur.RoleId != (int)RoleTypes.Student))
            {
                return(BasicOperationResult <User> .Fail("El usuario al que intenta asociar este padre, no es un estudiantes"));
            }

            newUser.IsActive         = true;
            newUser.IsEmailValidated = true;
            var userCreateResult = await _userManager.CreateAsync(newUser, password);

            if (!userCreateResult.Succeeded)
            {
                return(BasicOperationResult <User> .Fail(GetErrorsFromIdentityResult(userCreateResult.Errors)));
            }

            var parentUser = await _userRepository.Set.FirstOrDefaultAsync(u => u.Email == newUser.Email);

            int userId = parentUser.Id;

            _userRoleRepository.Create(new UserRole {
                UserId = userId, RoleId = (int)RoleTypes.Parent
            });


            var parentStudent = new ParentStudent
            {
                ParentId     = userId,
                StudentId    = studentId,
                Relationship = ParentRelationship.Father
            };


            studentUser.IsActive = true;
            _userRepository.Update(studentUser);
            _parentStudentRepository.Create(parentStudent);
            await _parentStudentRepository.Save();

            await _mailService.SendEmailForSuccesfullAcountCreation(parentUser);

            await _mailService.SendEmailStudentActivated(studentUser);

            return(BasicOperationResult <User> .Ok(parentUser));
        }
示例#5
0
        public async Task <bool> Edit(ParentStudent model)
        {
            try
            {
                var parentStudent = await this.context.ParentStudents.FindAsync(model.Id);

                if (parentStudent == null)
                {
                    return(false);
                }
                else
                {
                    parentStudent.ParentId  = model.ParentId;
                    parentStudent.StudentId = model.StudentId;
                    this.context.Entry <ParentStudent>(parentStudent).State = EntityState.Modified;
                    await this.context.SaveChangesAsync();
                }
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
示例#6
0
        private async Task AsociateExistingParentWithNewStudent(User parentUser, User studentUser)
        {
            var roles = parentUser.UserRoles.Select(ur => ur.RoleId);

            if (roles.All(r => r != (int)RoleTypes.Parent))
            {
                var userRole = new UserRole
                {
                    RoleId = (int)RoleTypes.Parent,
                    UserId = parentUser.Id
                };
                _userRoleRepository.Create(userRole);
            }

            var parentStudentsResult = await _parentStudentRepository.FindAll(ps => ps.ParentId == parentUser.Id);

            var students = parentStudentsResult.Select(ps => ps.StudentId);

            if (students.Any(s => s == studentUser.Id))
            {
                return;
            }

            var parentStudent = new ParentStudent
            {
                ParentId     = parentUser.Id,
                StudentId    = studentUser.Id,
                Relationship = ParentRelationship.Father
            };

            _parentStudentRepository.Create(parentStudent);

            await _parentStudentRepository.Save();

            await _mailService.SendEmailToExistingParentToValidateStudent(studentUser, parentUser);
        }
        public static void SeedUsers(IPasswordHasher <User> hasher, ETutorContext context)
        {
            string password = hasher.HashPassword(new User(), "123456");

            string adminEmail   = "*****@*****.**";
            string tutorEmail   = "*****@*****.**";
            string studentEmail = "*****@*****.**";
            string parentEmail  = "*****@*****.**";

            User admin = context.Users
                         .FirstOrDefault(u => u.Email == adminEmail);
            User tutor = context.Users
                         .FirstOrDefault(u => u.Email == tutorEmail);
            User student = context.Users
                           .FirstOrDefault(u => u.Email == studentEmail);
            User parent = context.Users
                          .FirstOrDefault(u => u.Email == parentEmail);

            if (admin == null)
            {
                User user = new User
                {
                    Email               = adminEmail,
                    EmailConfirmed      = true,
                    Gender              = Gender.Male,
                    IsEmailValidated    = true,
                    IsTemporaryPassword = false,
                    Name               = "Admin",
                    SecurityStamp      = "n57iIauoLM1JW9PaV6ZM",
                    LastName           = "eTutor",
                    UserName           = adminEmail,
                    NormalizedEmail    = adminEmail.ToUpper(),
                    NormalizedUserName = adminEmail.ToUpper(),
                    AboutMe            = "Escribe algo sobre ti",
                    CreatedDate        = DateTime.Now,
                    PersonalId         = "000-0000000-4",
                    UpdatedDate        = DateTime.Now,
                    IsActive           = true,
                    AccessFailedCount  = 0,
                    PasswordHash       = hasher.HashPassword(new User(), "123456")
                };

                context.Add(user);
                context.SaveChanges();

                UserRole role = new UserRole {
                    UserId = user.Id, RoleId = (int)RoleTypes.Admin
                };
                context.Add(role);
                context.SaveChanges();
            }

            if (tutor == null)
            {
                User user = new User
                {
                    Email               = tutorEmail,
                    EmailConfirmed      = true,
                    Gender              = Gender.Male,
                    IsEmailValidated    = true,
                    IsTemporaryPassword = false,
                    Name               = "Tutor",
                    LastName           = "eTutor",
                    SecurityStamp      = "xATyuXy4vgUC9JZ833ja",
                    UserName           = tutorEmail,
                    NormalizedEmail    = tutorEmail.ToUpper(),
                    NormalizedUserName = tutorEmail.ToUpper(),
                    AboutMe            = "Escribe algo sobre ti",
                    CreatedDate        = DateTime.Now,
                    UpdatedDate        = DateTime.Now,
                    PersonalId         = "000-0000000-2",
                    AccessFailedCount  = 0,
                    IsActive           = true,
                    PasswordHash       = hasher.HashPassword(new User(), "123456"),
                };

                context.Add(user);
                context.SaveChanges();

                UserRole role = new UserRole {
                    UserId = user.Id, RoleId = (int)RoleTypes.Tutor
                };
                context.Add(role);
                context.SaveChanges();
            }

            if (student == null)
            {
                User user = new User
                {
                    Email               = studentEmail,
                    EmailConfirmed      = true,
                    Gender              = Gender.Male,
                    IsEmailValidated    = true,
                    IsTemporaryPassword = false,
                    Name               = "Student",
                    LastName           = "eTutor",
                    SecurityStamp      = "V0kjQ4wR2Zi9Z2Z2mvwz",
                    UserName           = studentEmail,
                    NormalizedEmail    = studentEmail.ToUpper(),
                    NormalizedUserName = studentEmail.ToUpper(),
                    AboutMe            = "Escribe algo sobre ti",
                    CreatedDate        = DateTime.Now,
                    BirthDate          = new DateTime(2002, 2, 23),
                    IsActive           = true,
                    UpdatedDate        = DateTime.Now,
                    PersonalId         = "000-0000000-0",
                    AccessFailedCount  = 0,
                    PasswordHash       = hasher.HashPassword(new User(), "123456"),
                };

                context.Add(user);
                context.SaveChanges();

                UserRole role = new UserRole {
                    UserId = user.Id, RoleId = (int)RoleTypes.Student
                };
                context.Add(role);
                context.SaveChanges();
            }

            if (parent == null)
            {
                User user = new User
                {
                    Email               = parentEmail,
                    EmailConfirmed      = true,
                    Gender              = Gender.Male,
                    IsEmailValidated    = true,
                    IsTemporaryPassword = false,
                    Name               = "Student",
                    LastName           = "eTutor",
                    SecurityStamp      = "gJJTwcxqoSgZ66zH3vPM",
                    UserName           = parentEmail,
                    AboutMe            = "Escribe algo sobre ti",
                    NormalizedEmail    = parentEmail.ToUpper(),
                    NormalizedUserName = parentEmail.ToUpper(),
                    CreatedDate        = DateTime.Now,
                    UpdatedDate        = DateTime.Now,
                    IsActive           = true,
                    PersonalId         = "000-0000000-1",
                    AccessFailedCount  = 0,
                    PasswordHash       = hasher.HashPassword(new User(), "123456"),
                };

                context.Add(user);
                context.SaveChanges();

                UserRole role = new UserRole {
                    UserId = user.Id, RoleId = (int)RoleTypes.Parent
                };
                context.Add(role);
                context.SaveChanges();
            }

            student = context.Users
                      .Include(s => s.UserRoles)
                      .Include(s => s.Parents)
                      .FirstOrDefault(u => u.Email == studentEmail);
            if (student != null && !student.Parents.Any())
            {
                parent = context.Users
                         .Include(u => u.UserRoles)
                         .Include(u => u.Students)
                         .FirstOrDefault(u => u.Email == parentEmail);

                var relationship = new ParentStudent
                {
                    ParentId     = parent.Id,
                    StudentId    = student.Id,
                    Relationship = ParentRelationship.Father
                };
                context.Add(relationship);
                context.SaveChanges();
            }
        }