public async Task <GenericResult <PersonDto> > AddPerson(PersonDto personDto)
        {
            try
            {
                if (personDto.FKPopulationInformationID.HasValue)
                {
                    var isPopulationExists = (await _populationService.IsPopulationExists(personDto.FKPopulationInformationID.Value));
                    if (!isPopulationExists.IsSucceed)
                    {
                        return(GenericResult <PersonDto> .UserSafeError("An error occurred when requesting population.Errors: " + isPopulationExists.GetAllMessage()));
                    }
                    else if (!isPopulationExists.Data)
                    {
                        return(GenericResult <PersonDto> .UserSafeError("There is no population info with given id"));
                    }
                }

                var newPerson = await _personRepo.InsertAsync(_mapper.Map <Person>(personDto));

                return(GenericResult <PersonDto> .Success(_mapper.Map <PersonDto>(newPerson)));
            }
            catch (Exception e)
            {
                return(GenericResult <PersonDto> .Error(e));
            }
        }
        public async Task <GenericResult <UserDto> > AddNewUser(NewUserDto newUserDto)
        {
            try
            {
                if (!await _peopleService.IsPersonExists(newUserDto.FKPersonID))
                {
                    return(GenericResult <UserDto> .UserSafeError("There is no person info with given id"));
                }


                var newUser = await _userRepo.InsertAsync(new User
                {
                    FKPersonID   = newUserDto.FKPersonID,
                    Email        = newUserDto.Email,
                    UserName     = newUserDto.UserName,
                    PasswordHash = _cipherService.Encrypt(newUserDto.Password),
                });

                return(GenericResult <UserDto> .Success(_mapper.Map <UserDto>(newUser)));
            }
            catch (Exception e)
            {
                return(GenericResult <UserDto> .Error(e));
            }
        }
Beispiel #3
0
        public async Task <GenericResult <StudentInformationDto> > AddNewStudent(NewStudentInformationDto newStudentInformationDto)
        {
            if (string.IsNullOrEmpty(newStudentInformationDto.StudentNumber))
            {
                return(GenericResult <StudentInformationDto> .UserSafeError($"Error! Student number can not be null"));
            }
            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    if (await _studentRepo.AsQueryable().AnyAsync(x => x.StudentNumber == newStudentInformationDto.StudentNumber))
                    {
                        transaction.Dispose();
                        return(GenericResult <StudentInformationDto> .UserSafeError($"Error! Student number: \"{newStudentInformationDto.StudentNumber}\" is already in use"));
                    }
                    newStudentInformationDto.RegistrationDate = DateTime.Now;
                    var newPopulationResult = await _populationService.AddPopulationInfo(newStudentInformationDto.NewUserDto.PersonDto.PopulationInformationDto);

                    if (newPopulationResult.IsSucceed)
                    {
                        newStudentInformationDto.NewUserDto.PersonDto.FKPopulationInformationID = newPopulationResult.Data;
                        var newPersonResult = await _peopleService.AddPerson(newStudentInformationDto.NewUserDto.PersonDto);

                        if (newPersonResult.IsSucceed)
                        {
                            newStudentInformationDto.NewUserDto.FKPersonID = newPersonResult.Data.ID;
                            var newUserResult = await _userService.AddNewUser(newStudentInformationDto.NewUserDto);

                            if (newUserResult.IsSucceed)
                            {
                                newStudentInformationDto.FKUserID = newUserResult.Data.ID;
                                var newStudentDbEntity = _mapper.Map <StudentInformation>(newStudentInformationDto);

                                var newStudent = await _studentRepo.InsertAsync(newStudentDbEntity);

                                await _eventPublisher.PublishAsync(new NewStudentCreatedEvent()
                                {
                                    FKUserID      = newStudent.FKUserID,
                                    StudentNumber = newStudent.StudentNumber
                                });

                                transaction.Complete();
                                return(GenericResult <StudentInformationDto> .Success(_mapper.Map <StudentInformationDto>(newStudent)));
                            }
                            else
                            {
                                transaction.Dispose();
                                return(newUserResult.ConvertTo(default(StudentInformationDto), "An error occurred while adding new user."));
                            }
                        }
                        else
                        {
                            await _populationService.Delete(newPopulationResult.Data);

                            transaction.Dispose();
                            return(newPersonResult.ConvertTo(default(StudentInformationDto), "An error occurred while adding new person."));
                        }
                    }
                    else
                    {
                        transaction.Dispose();
                        return(newPopulationResult.ConvertTo(default(StudentInformationDto), "An error occurred while adding new population."));
                    }
                }
                catch (Exception e)
                {
                    transaction.Dispose();
                    return(GenericResult <StudentInformationDto> .Error(e));
                }
            }
        }