public void PublishCreatedEventAsync()
 {
     try
     {
         Random rnd  = new Random();
         var    task = Task.Run(async() =>
         {
             await
             _eventPublisher.PublishAsync(new TestEvent2
             {
                 userid = rnd.Next(0, int.MaxValue),
                 userTc = "tc:" + rnd.Next(0, int.MaxValue)
             });
         });
         task.Wait();
     }
     catch (Exception e)
     {
         Assert.That(false, e.Message);
     }
     Assert.Pass();
 }
Example #2
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));
                }
            }
        }