Esempio n. 1
0
        public async Task <Result <StudentDto> > CreateStudentAsync(long accountId)
        {
            try
            {
                var account = await _accountService.GetAccountCredentialsByIdAsync(accountId);

                if (account == null)
                {
                    return(Result <StudentDto> .Error(ErrorCode.NotFound,
                                                      "Account not found"));
                }

                if (account.Role == UserRole.NotAssigned)
                {
                    account.Role = UserRole.Student;


                    var student = new Student
                    {
                        Account   = account,
                        AccountId = accountId
                    };

                    _unitOfWork.StudentRepository.Add(student);

                    await _unitOfWork.CommitAsync();

                    await _notification.AccountApproved(account);

                    return(Result <StudentDto> .Success(_mapper.Map <StudentDto>(student)));
                }
                else
                {
                    _unitOfWork.Rollback();

                    return(Result <StudentDto> .Error(ErrorCode.ValidationError,
                                                      "This account already assigned."));
                }
            }
            catch
            {
                _unitOfWork.Rollback();

                return(Result <StudentDto> .Error(ErrorCode.InternalServerError,
                                                  "Cannot create student."));
            }
        }