Exemple #1
0
 public void MyRepoTest()
 {
     try
     {
         var list = _personMyRepo.AsQueryable().ToList();
     }
     catch (Exception e)
     {
         Assert.That(false, "Error: " + e.Message);
     }
     Assert.Pass();
 }
        public async Task <GenericResult <List <(int id, string name, string foto, string studentNumber)> > > SearchStudent(string query)
        {
            try
            {
                var students = await _studentInformationRepo.AsQueryable()
                               .Where(x =>
                                      x.StudentNumber.Contains(query) ||
                                      x.User.UserName.Contains(query) ||
                                      x.User.Person.Name.Contains(query) ||
                                      x.User.Person.Surname.Contains(query)
                                      )
                               .OrderByDescending(x => x.ID).ThenBy(x => x.StudentNumber)
                               .Take(10)
                               .Select(x => new { x.ID, Name = x.User.Person.Name + " " + x.User.Person.Surname, x.User.Person.PhotoUrl, x.StudentNumber })
                               .ToListAsync();

                return(GenericResult <List <(int id, string name, string foto, string studentNumber)> > .Success(students.Select(x => (x.ID, x.Name, x.PhotoUrl, x.StudentNumber)).ToList()));
            }
            catch (Exception e)
            {
                return(GenericResult <List <(int id, string name, string foto, string studentNumber)> > .Error(null, e));
            }
        }
 public async Task <bool> IsPersonExists(int id) =>
 await _personRepo.AsQueryable().AnyAsync(x => x.ID == id);
Exemple #4
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));
                }
            }
        }