public async Task <GenericResult <StudentSearchResultDto> > GetStudentInformationById(int id) { try { var student = await _studentInformationRepo.AsQueryable().Where(x => x.ID == id).Select(x => new StudentSearchResultDto { ID = x.ID, StudentNumber = x.StudentNumber, Email = x.User.Email, UserName = x.User.UserName, Name = x.User.Person.Name, Surname = x.User.Person.Surname, RegistrationDate = x.RegistrationDate }).FirstOrDefaultAsync(); if (student == null) { return(GenericResult <StudentSearchResultDto> .UserSafeError(null, "There is no student with given id")); } return(GenericResult <StudentSearchResultDto> .Success(student)); } catch (Exception e) { return(GenericResult <StudentSearchResultDto> .Error(null, 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)); } }
public async Task <GenericResult <User> > AuthenticateAsync(int sessionId, string login, string password) { var cmd = Db.CreateProcedureCommand("dbo.Login"); cmd.Parameters.AddWithValue("@Login", login); cmd.Parameters.AddWithValue("@Password", password); cmd.Parameters.AddWithValue("@SessionId", sessionId); cmd.Parameters.Add("@UserId", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output; cmd.Parameters.Add("@SessionIdNew", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@Error_msg", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output; await Db.ExecuteNonQueryAsync(cmd, CancellationToken.None); var errMsg = cmd.ReadOutputValue <string>("@Error_msg"); if (!string.IsNullOrEmpty(errMsg)) { return(GenericResult <User> .Error(errMsg)); } if (!cmd.ReadOutputValue <int?>("@UserId").HasValue) { return(GenericResult <User> .Error("Ошибка логина или пароля")); } var user = new User { Login = login, SessionId = cmd.ReadOutputValue <int?>("@SessionIdNew"), UserId = cmd.ReadOutputValue <int?>("@UserId"), UserName = cmd.ReadOutputValue <string>("@UserName") }; return(GenericResult <User> .Success(user)); }
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 <TReturnType> > RequestGenericResultReturnsEndPointAsync <TReturnType>(Func <HttpClient, Task <HttpResponseMessage> > clientFunc) { try { return(await RequestEndPointAsync <GenericResult <TReturnType> >(clientFunc)); } catch (Exception e) { return(GenericResult <TReturnType> .Error(e)); } }
public GenericResult Execute(IOrganizationService service) { try { return(ConcreteExecute(service)); } catch (Exception ex) { return(GenericResult.Error(ex.ToString())); } }
public async Task <GenericResult <bool> > IsPopulationExists(int id) { try { bool exists = await _populationRepo.AsQueryable().AnyAsync(x => x.ID == id); return(GenericResult <bool> .Success(exists)); } catch (Exception e) { return(GenericResult <bool> .Error(e)); } }
public async Task <GenericResult <bool> > Delete(int id) { try { var newPopulation = await _populationRepo.DeleteAsync(id); return(GenericResult <bool> .Success(true)); } catch (Exception e) { return(GenericResult <bool> .Error(e)); } }
public async Task <GenericResult <int> > GetClientSecretAsync(string login) { var cmd = Db.CreateProcedureCommand("dbo.GetClient"); cmd.Parameters.AddWithValue("@Login", login); cmd.Parameters.Add("@UserId", SqlDbType.Int).Direction = ParameterDirection.Output; await Db.ExecuteNonQueryAsync(cmd, CancellationToken.None); if (!cmd.ReadOutputValue <int?>("@UserId").HasValue) { return(GenericResult <int> .Error("Пользователь не найден")); } return(GenericResult <int> .Success(cmd.ReadOutputValue <int?>("@UserId").Value)); }
public async Task <GenericResult <int> > AddPopulationInfo(PopulationInformationDto informationDto) { try { var mappedEntity = _mapper.Map <PopulationInformation>(informationDto); var newPopulation = await _populationRepo.InsertAsync(mappedEntity); return(GenericResult <int> .Success(newPopulation.ID)); } catch (Exception e) { return(GenericResult <int> .Error(e)); } }
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)); } } }