public async Task<OneOf<ModelWithErrors<ViewModel>, Success>> Handle( Command request, CancellationToken cancellationToken) { ThrowIfFlowStateNotValid(); var validator = new CommandValidator(); var validationResult = await validator.ValidateAsync(request); if (!validationResult.IsValid) { var vm = CreateViewModel(); request.Adapt(vm); return new ModelWithErrors<ViewModel>(vm, validationResult); } _journeyInstance.UpdateState(state => state.SetDescription( request.WhoFor, request.EntryRequirements, request.WhatYoullLearn, request.HowYoullLearn, request.HowYoullBeAssessed, request.WhatYouCanDoNext, isComplete: true)); return new Success(); }
public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle( Command request, CancellationToken cancellationToken) { ThrowIfFlowStateNotValid(); var tLevelId = Guid.NewGuid(); var currentUser = _currentUserProvider.GetCurrentUser(); var now = _clock.UtcNow; var result = await _sqlQueryDispatcher.ExecuteQuery( new CreateTLevel() { CreatedBy = currentUser, CreatedOn = now, EntryRequirements = _journeyInstance.State.EntryRequirements, HowYoullBeAssessed = _journeyInstance.State.HowYoullBeAssessed, HowYoullLearn = _journeyInstance.State.HowYoullLearn, LocationVenueIds = _journeyInstance.State.LocationVenueIds, ProviderId = request.ProviderId, StartDate = _journeyInstance.State.StartDate.Value, TLevelDefinitionId = _journeyInstance.State.TLevelDefinitionId.Value, TLevelId = tLevelId, Website = _journeyInstance.State.Website, WhatYouCanDoNext = _journeyInstance.State.WhatYouCanDoNext, WhatYoullLearn = _journeyInstance.State.WhatYoullLearn, WhoFor = _journeyInstance.State.WhoFor, YourReference = _journeyInstance.State.YourReference }); if (result.Value is CreateTLevelFailedReason reason) { if (reason == CreateTLevelFailedReason.TLevelAlreadyExistsForDate) { var vm = await CreateViewModel(); var validationResult = new ValidationResult(new[] { new ValidationFailure( nameof(ViewModel.StartDate), "T Level already exists, enter a new start date") }); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } throw new NotImplementedException( $"Unknown {nameof(CreateTLevelFailedReason)}: '{reason}'."); } _journeyInstance.UpdateState(state => state.SetCreatedTLevel(tLevelId)); // Complete JourneyInstance so state can no longer be changed _journeyInstance.Complete(); return(new Success()); }
public Task <Unit> Handle(Command request, CancellationToken cancellationToken) { ThrowIfFlowStateNotValid(); // Stash the values in state // Since these haven't been validated `isComplete` is set to false // to ensure the un-validated data cannot be submitted _journeyInstance.UpdateState(state => state.SetDetails( request.YourReference, request.StartDate?.ToDateTime(), request.LocationVenueIds, request.Website, isComplete: false)); return(Unit.Task); }
public async Task <OneOf <ModelWithErrors <ViewModel>, SuccessViewModel> > Handle(Command request, CancellationToken cancellationToken) { var ukprn = await GetUkprnForCourse(request.CourseId); var(course, courseRun) = await GetCourseAndCourseRun(request.CourseId, request.CourseRunId, ukprn); if (!request.Confirm) { var vm = await CreateViewModel(course, courseRun); var validationResult = new ValidationResult(new[] { new ValidationFailure(nameof(request.Confirm), "Confirm you want to delete the course") }); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } await _cosmosDbQueryDispatcher.ExecuteQuery(new DeleteCourseRunQuery() { CourseId = request.CourseId, CourseRunId = request.CourseRunId, ProviderUkprn = ukprn, UpdatedBy = _currentUserProvider.GetCurrentUser().UserId, UpdatedDate = _clock.UtcNow, }); // The next page needs this info - stash it in the JourneyModel // since it will no longer able to query for it. _journeyInstance.UpdateState(new JourneyModel() { CourseName = courseRun.CourseName, ProviderId = (await _providerInfoCache.GetProviderIdForUkprn(ukprn)).Value, ProviderUkprn = ukprn }); _journeyInstance.Complete(); return(new SuccessViewModel() { CourseId = request.CourseId, CourseRunId = request.CourseRunId, CourseName = courseRun.CourseName }); }
public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle(Command request, CancellationToken cancellationToken) { _journeyInstance.ThrowIfCompleted(); var apprenticeship = await GetApprenticeship(request.ApprenticeshipId); if (!request.Confirm) { var vm = CreateViewModel(apprenticeship); var validationResult = new ValidationResult(new[] { new ValidationFailure(nameof(request.Confirm), "Confirm you want to delete the Apprenticeship") }); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } await _sqlQueryDispatcher.ExecuteQuery(new DeleteApprenticeshipQuery() { ApprenticeshipId = request.ApprenticeshipId, DeletedBy = _currentUserProvider.GetCurrentUser(), DeletedOn = _clock.UtcNow }); _providerOwnershipCache.OnApprenticeshipDeleted(request.ApprenticeshipId); // The next page needs this info - stash it in the JourneyModel // since it will no longer able to query for it. _journeyInstance.UpdateState(new JourneyModel() { ApprenticeshipId = apprenticeship.ApprenticeshipId, ProviderId = apprenticeship.ProviderId, ApprenticeshipTitle = apprenticeship.Standard.StandardName, NotionalNVQLevelv2 = apprenticeship.Standard.NotionalNVQLevelv2 }); _journeyInstance.Complete(); return(new Success()); }
public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle(Command request, CancellationToken cancellationToken) { _journeyInstance.ThrowIfCompleted(); var tLevel = await GetTLevel(request.TLevelId); if (!request.Confirm) { var vm = CreateViewModel(tLevel); var validationResult = new ValidationResult(new[] { new ValidationFailure(nameof(request.Confirm), "Confirm you want to delete the T Level") }); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } await _sqlQueryDispatcher.ExecuteQuery(new DeleteTLevelQuery() { TLevelId = request.TLevelId, DeletedBy = _currentUserProvider.GetCurrentUser(), DeletedOn = _clock.UtcNow }); _providerOwnershipCache.OnTLevelDeleted(request.TLevelId); // The next page needs this info - stash it in the JourneyModel // since it will no longer able to query for it. _journeyInstance.UpdateState(new JourneyModel() { TLevelName = tLevel.TLevelDefinition.Name, ProviderId = tLevel.ProviderId, YourReference = tLevel.YourReference }); _journeyInstance.Complete(); return(new Success()); }
public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle( Command request, CancellationToken cancellationToken) { ThrowIfFlowStateNotValid(); var authorizedTLevelDefinitions = await GetAuthorizedTLevelDefinitions(request.ProviderId); var validator = new CommandValidator(authorizedTLevelDefinitions); var validationResult = await validator.ValidateAsync(request); if (!validationResult.IsValid) { var vm = CreateViewModel(request.ProviderId, authorizedTLevelDefinitions); request.Adapt(vm); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } if (request.SelectedTLevelDefinitionId != _journeyInstance.State.TLevelDefinitionId) { var tLevelDefinition = authorizedTLevelDefinitions .Single(tld => tld.TLevelDefinitionId == request.SelectedTLevelDefinitionId); var exemplarContent = await _sqlQueryDispatcher.ExecuteQuery( new GetTLevelDefinitionExemplarContent() { TLevelDefinitionId = request.SelectedTLevelDefinitionId.Value }); _journeyInstance.UpdateState(state => state.SetTLevel( tLevelDefinition.TLevelDefinitionId, tLevelDefinition.Name, exemplarContent)); } return(new Success()); }
public Task <Unit> Handle(Command request, CancellationToken cancellationToken) { // Stash the values in state // Since these haven't been validated `IsValid` is set to false // to ensure the un-validated data cannot be submitted _journeyInstance.UpdateState(state => { state.YourReference = request.YourReference; state.StartDate = request.StartDate.Value.ToDateTime(); state.LocationVenueIds = request.LocationVenueIds; state.Website = request.Website; state.WhoFor = request.WhoFor; state.EntryRequirements = request.EntryRequirements; state.WhatYoullLearn = request.WhatYoullLearn; state.HowYoullBeAssessed = request.HowYoullBeAssessed; state.HowYoullLearn = request.HowYoullLearn; state.WhatYouCanDoNext = request.WhatYouCanDoNext; state.IsValid = false; }); return(Unit.Task); }
public async Task <OneOf <ModelWithErrors <ViewModel>, Success> > Handle( Command request, CancellationToken cancellationToken) { ThrowIfFlowStateNotValid(); var providerVenues = await GetVenuesForProvider(request.ProviderId); // Remove any invalid venue IDs request.LocationVenueIds ??= new HashSet <Guid>(); request.LocationVenueIds.Intersect(providerVenues.Select(v => v.VenueId)); var validationResult = await _validator.ValidateAsync(request); // TODO Move this logic into Validation.TLevelValidation when Validator doesn't have to be injected. // This is a limitation of the way the current Date validation works that will go away when // tag helper library supports model binding to DateTimes. await CheckNoExistingTLevelForStartDate(validationResult, request); if (!validationResult.IsValid) { var vm = CreateViewModel(request.ProviderId, providerVenues); request.Adapt(vm); return(new ModelWithErrors <ViewModel>(vm, validationResult)); } _journeyInstance.UpdateState(state => state.SetDetails( request.YourReference, request.StartDate.Value.ToDateTime(), request.LocationVenueIds, request.Website, isComplete: true)); return(new Success()); }