Ejemplo n.º 1
0
        public async Task <Student> Register(Student student)
        {
            if (!await StudentValidator.Create(student))
            {
                return(student);
            }

            try
            {
                await UOW.Begin();

                User user = new User()
                {
                    Username = student.Identify,
                    Password = CryptographyExtentions.GeneratePassword(),
                    IsAdmin  = false,
                    Email    = student.Email
                };
                student.User = user;
                await UOW.StudentRepository.Create(student);

                await UOW.Commit();

                await Utils.RegisterMail(user);

                return(await UOW.StudentRepository.Get(student.Id));
            }
            catch (Exception ex)
            {
                await UOW.Rollback();

                throw new MessageException(ex);
            }
        }
Ejemplo n.º 2
0
        public async Task <Student> Register(Student student)
        {
            if (!await StudentValidator.Create(student))
            {
                return(student);
            }

            try
            {
                await UOW.Begin();

                student.Id = Guid.NewGuid();
                await UOW.StudentRepository.Create(student);

                User user = new User()
                {
                    Username  = student.Identify,
                    Id        = Guid.NewGuid(),
                    Password  = CryptographyExtentions.GeneratePassword(),
                    IsAdmin   = false,
                    StudentId = student.Id,
                    Email     = student.Email
                };
                await UOW.UserRepository.Create(user);

                await UOW.Commit();

                var Mail = new Mail();
                Mail.Recipients = new List <string> {
                    user.Email
                };
                Mail.Subject = "Tạo tài khoản TF";
                Mail.Body    = $"Tài khoản của bạn đã được tạo. Username: {user.Username} Password: {user.Password}";
                Thread sendMailThread = new Thread(() => MailService.Send(Mail));
                sendMailThread.Start();
                return(await UOW.StudentRepository.Get(student.Id));
            }
            catch (Exception ex)
            {
                await UOW.Rollback();

                throw new MessageException(ex);
            }
        }
Ejemplo n.º 3
0
        public async Task <Student> Create(Student student)
        {
            if (!await StudentValidator.Create(student))
            {
                return(student);
            }

            using (UOW.Begin())
            {
                try
                {
                    student.Id = Guid.NewGuid();
                    await UOW.StudentRepository.Create(student);

                    var user = await UOW.UserRepository.Create(new User()
                    {
                        Id        = Guid.NewGuid(),
                        Username  = student.StudentNumber.ToString(),
                        Password  = student.StudentNumber.ToString(),
                        StudentId = student.Id,
                        IsAdmin   = false
                    });

                    await UOW.Commit();

                    return(await Get(student.Id));
                }
                catch (Exception e)
                {
                    await UOW.Rollback();

                    student.AddError(nameof(StudentService), nameof(Create), CommonEnum.ErrorCode.SystemError);
                    return(student);
                }
            }
        }