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 <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 <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 <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)); } }
private GenericResult EnforceTriggerStatus(OrderStateMachine.Trigger trigger, ref TriggerStatus triggerStatus) { if (IsTriggerHandlePending(trigger)) { if (_workingData.PendingTrigger != null) { if (triggerStatus.IsPending()) { return(GenericResult.FailureFormat("There is already a {0} pending command. You must handle it before", _workingData.PendingTrigger)); } if (trigger != _workingData.PendingTrigger) { return(GenericResult.FailureFormat("There is a {0} pending not a {1}. You cannot {0} it", _workingData.PendingTrigger, trigger, triggerStatus)); } _workingData.SetPendingTrigger(null); } else { triggerStatus = TriggerStatus.Pending; } } else if (triggerStatus.IsPending() || triggerStatus.IsPendingReply()) { return(GenericResult.FailureFormat("The command {0} could not be {1}. No pending required", trigger, triggerStatus)); } return(GenericResult.Success()); }
private GenericResult HandleCommand( OrderStateMachine.Trigger trigger, ITriggerContext context, IOrderEditableData editableData = null, IOrderDealingData dealingData = null, IOrderCoreData coreData = null, TriggerStatus status = TriggerStatus.Done) { Contract.Requires(context != null, "context != null"); _workingData = CurrentData.Clone() .SetRoutingData(dealingData) .SetEditableData(editableData) .SetTrigger(trigger) .SetPendingTrigger(status.IsPendingReply() && this.PendingTrigger == trigger ? null : this.PendingTrigger); var result = EnforceTriggerStatus(trigger, ref status); if (result.IsFailure()) { return(result); } if (status == TriggerStatus.Rejected) { result = GenericResult.Success(); } else { var triggerSucceeded = status == TriggerStatus.Pending ? _stateMachine.CanFireTrigger(trigger) : _stateMachine.TryFireTrigger(trigger); result = triggerSucceeded ? GenericResult.Success() : GenericResult.Failure(string.Concat(_workingData.StateMachineErrorMessage, String.Format("The commmand {0} is not allowed when the order is in {1} state", trigger, OrderState))); } if (result.IsSuccess()) { _workingData.SetOrderState(_stateMachine.GetState); if (editableData != null) { if (coreData != null) { this.InsertDataFrom(coreData); } EventLogs.Add(new OrderParameterEventLog <IOrderEditableData>(context, trigger, status, _workingData.OrderState, this, editableData)); } else if (dealingData != null) { EventLogs.Add(new OrderParameterEventLog <IOrderDealingData>(context, trigger, status, _workingData.OrderState, this, dealingData)); if (dealingData.Trade != null) { _workingData.SetExecutionQuantity(ComputeExecutionQuantity(_workingData.Side)); } } else { EventLogs.Add(new OrderEventLog(context, trigger, status, _workingData.OrderState, this)); } _workingData.SetOrderStatus(ComputeOrderStatus(_workingData)); RefreshCurrentData(_workingData); } _workingData = null; return(result); }
public static GenericResult IsValid <T>(this AbstractValidator <T> validator, T item) { Contract.Requires(item != null, "item != null"); var result = validator.Validate(item); return(result.IsValid ? GenericResult.Success() : GenericResult.FailureFormat(result.Errors.Aggregate(new StringBuilder(), (s, v) => { s.AppendFormat("{0} : {1}", v.PropertyName, v.ErrorMessage); return s; }).ToString())); }
private GenericResult OrderHandleRequest(Enum command, IEnumerable <Tuple <Order, IOrderEditableData, IOrderCoreData, IOrderDealingData> > parameters) { foreach (var parameter in parameters) { var result = OrderCommandMappings[command](parameter.Item1, null, parameter.Item3, parameter.Item2, parameter.Item4); if (result.IsSuccess()) { _repositoryFacade.Orders.Update(parameter.Item1); } } return(GenericResult.Success()); }
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 <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> > SelectStudent(int id) { var searchResult = await _studentSearchService.GetStudentInformationById(id); if (searchResult.IsSucceed) { SetSelectedStudent(searchResult.Data); return(GenericResult <bool> .Success(true, "Student selected")); } else { return(searchResult.GetUserSafeResult(false)); } }
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)); } }
private GenericResult HandleTradeCommand( OrderStateMachine.Trigger trigger, ITriggerContext context, ITrade trade, ITradeEditableData editableData = null ) { Contract.Requires(context != null, "context != null"); _workingData = CurrentData.Clone(); if (!_stateMachine.CanFireTrigger(trigger)) { return(GenericResult.FailureFormat(String.Format("The commmand {0} is not allowed when the _order is in {1} state", trigger, OrderState))); } GenericResult result; switch (trigger) { case OrderStateMachine.Trigger.AddTrade: trade = new Trade { Order = this }; result = trade.Create(context, editableData); break; case OrderStateMachine.Trigger.TradeBooked: //TODO handle the booking done result = GenericResult.Success(); break; case OrderStateMachine.Trigger.CancelTrade: result = trade.Cancel(context); break; case OrderStateMachine.Trigger.UpdateTrade: default: return(GenericResult.FailureFormat("The trade command {0} is not implemented", trigger)); } return(result.IsSuccess() ? HandleCommand(trigger, context, dealingData: new OrderDealingEventParameter(trade)) : result); }
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)); } } }
public GenericResult Cancel(ITriggerContext context) { return(GenericResult.Success()); }
public GenericResult IsAutoAccepted(IOrder order) { return(GenericResult.Success()); }