public async Task <OneOf <NotFound, CourseRunDetailViewModel> > Handle(Query request, CancellationToken cancellationToken) { var course = await _sqlQueryDispatcher.ExecuteQuery(new GetCourse() { CourseId = request.CourseId }); if (course == null) { return(new NotFound()); } var courseRun = course.CourseRuns.SingleOrDefault(c => c.CourseRunId == request.CourseRunId && c.CourseRunStatus == CourseStatus.Live); if (courseRun == null) { return(new NotFound()); } var getProvider = _cosmosDbQueryDispatcher.ExecuteQuery(new Core.DataStore.CosmosDb.Queries.GetProviderByUkprn { Ukprn = course.ProviderUkprn }); var getQualification = _larsSearchClient.Search(new LarsLearnAimRefSearchQuery { LearnAimRef = course.LearnAimRef }); var getFeChoice = _cosmosDbQueryDispatcher.ExecuteQuery(new GetFeChoiceForProvider { ProviderUkprn = course.ProviderUkprn }); await Task.WhenAll(getProvider, getQualification, getFeChoice); var provider = getProvider.Result; var qualification = getQualification.Result.Items.SingleOrDefault(); var feChoice = getFeChoice.Result; var getSqlProvider = _sqlQueryDispatcher.ExecuteQuery(new Core.DataStore.Sql.Queries.GetProviderById { ProviderId = provider.Id }); var getProviderVenues = _sqlQueryDispatcher.ExecuteQuery(new GetVenuesByProvider() { ProviderId = provider.Id }); await Task.WhenAll(getSqlProvider, getProviderVenues); var sqlProvider = getSqlProvider.Result; var venues = getProviderVenues.Result; var venue = courseRun.VenueId.HasValue ? venues.Single(v => v.VenueId == courseRun.VenueId) : null; var providerContact = provider.ProviderContact.SingleOrDefault(c => c.ContactType == "P"); var providerAddressLines = NormalizeAddress(providerContact?.ContactAddress); var alternativeCourseRuns = course.CourseRuns .Where(r => r.CourseRunId != request.CourseRunId && r.CourseRunStatus == CourseStatus.Live) .Select(r => new { CourseRun = r, Venue = venues.SingleOrDefault(v => v.VenueId == r.VenueId) }); var regions = await _regionCache.GetAllRegions(); return(new CourseRunDetailViewModel { CourseRunId = courseRun.CourseRunId, OfferingType = Core.Search.Models.FindACourseOfferingType.Course, AttendancePattern = courseRun.DeliveryMode == CourseDeliveryMode.ClassroomBased ? (CourseAttendancePattern?)courseRun.AttendancePattern : null, Cost = courseRun.Cost, CostDescription = HtmlEncode(courseRun.CostDescription), CourseName = HtmlEncode(courseRun.CourseName), CourseURL = ViewModelFormatting.EnsureHttpPrefixed(courseRun.CourseWebsite), CreatedDate = courseRun.CreatedOn, DeliveryMode = courseRun.DeliveryMode, DurationUnit = courseRun.DurationUnit, DurationValue = courseRun.DurationValue, FlexibleStartDate = courseRun.FlexibleStartDate, StartDate = !courseRun.FlexibleStartDate ? courseRun.StartDate : null, StudyMode = courseRun.StudyMode.HasValue ? courseRun.StudyMode.Value : 0, National = courseRun.National, Course = new CourseViewModel { AwardOrgCode = qualification.Record.AwardOrgCode, CourseDescription = HtmlEncode(course.CourseDescription), CourseId = course.CourseId, EntryRequirements = HtmlEncode(course.EntryRequirements), HowYoullBeAssessed = HtmlEncode(course.HowYoullBeAssessed), HowYoullLearn = HtmlEncode(course.HowYoullLearn), LearnAimRef = course.LearnAimRef, QualificationLevel = qualification.Record.NotionalNVQLevelv2, WhatYoullLearn = HtmlEncode(course.WhatYoullLearn), WhatYoullNeed = HtmlEncode(course.WhatYoullNeed), WhereNext = HtmlEncode(course.WhereNext) }, Venue = venue != null ? new VenueViewModel { AddressLine1 = HtmlEncode(venue.AddressLine1), AddressLine2 = HtmlEncode(venue.AddressLine2), County = HtmlEncode(venue.County), Email = venue.Email, Postcode = venue.Postcode, Telephone = venue.Telephone, Town = HtmlEncode(venue.Town), VenueName = HtmlEncode(venue.VenueName), Website = ViewModelFormatting.EnsureHttpPrefixed(venue.Website), Latitude = Convert.ToDecimal(venue.Latitude), Longitude = Convert.ToDecimal(venue.Longitude) } : null, Provider = new ProviderViewModel { ProviderName = sqlProvider.DisplayName, TradingName = sqlProvider.DisplayName, CourseDirectoryName = provider.CourseDirectoryName, Alias = provider.Alias, Ukprn = provider.UnitedKingdomProviderReferenceNumber, AddressLine1 = HtmlEncode(providerAddressLines.AddressLine1), AddressLine2 = HtmlEncode(providerAddressLines.AddressLine2), Town = HtmlEncode(providerContact?.ContactAddress?.PostTown ?? providerContact?.ContactAddress?.Items?.FirstOrDefault()?.ToString()), Postcode = providerContact?.ContactAddress?.PostCode, County = HtmlEncode(providerContact?.ContactAddress?.County ?? providerContact?.ContactAddress?.Locality), Telephone = providerContact?.ContactTelephone1, Fax = providerContact?.ContactFax, Website = ViewModelFormatting.EnsureHttpPrefixed(providerContact?.ContactWebsiteAddress), Email = providerContact?.ContactEmail, EmployerSatisfaction = feChoice?.EmployerSatisfaction, LearnerSatisfaction = feChoice?.LearnerSatisfaction, }, Qualification = new QualificationViewModel { AwardOrgCode = qualification.Record.AwardOrgCode, AwardOrgName = HtmlEncode(qualification.Record.AwardOrgName), LearnAimRef = qualification.Record.LearnAimRef, LearnAimRefTitle = HtmlEncode(qualification.Record.LearnAimRefTitle), LearnAimRefTypeDesc = HtmlEncode(qualification.Record.LearnAimRefTypeDesc), QualificationLevel = qualification.Record.NotionalNVQLevelv2, SectorSubjectAreaTier1Desc = HtmlEncode(qualification.Record.SectorSubjectAreaTier1Desc), SectorSubjectAreaTier2Desc = HtmlEncode(qualification.Record.SectorSubjectAreaTier2Desc) }, AlternativeCourseRuns = alternativeCourseRuns.Select(c => new AlternativeCourseRunViewModel { CourseRunId = c.CourseRun.CourseRunId, AttendancePattern = c.CourseRun.DeliveryMode == CourseDeliveryMode.ClassroomBased ? (CourseAttendancePattern?)c.CourseRun.AttendancePattern : null, Cost = c.CourseRun.Cost, CostDescription = HtmlEncode(c.CourseRun.CostDescription), CourseName = HtmlEncode(c.CourseRun.CourseName), CourseURL = ViewModelFormatting.EnsureHttpPrefixed(c.CourseRun.CourseWebsite), CreatedDate = c.CourseRun.CreatedOn, DeliveryMode = c.CourseRun.DeliveryMode, DurationUnit = c.CourseRun.DurationUnit, DurationValue = c.CourseRun.DurationValue, FlexibleStartDate = c.CourseRun.FlexibleStartDate, StartDate = !c.CourseRun.FlexibleStartDate ? c.CourseRun.StartDate : null, StudyMode = c.CourseRun.StudyMode.HasValue ? c.CourseRun.StudyMode.Value : 0, Venue = c.Venue != null ? new VenueViewModel { AddressLine1 = HtmlEncode(c.Venue.AddressLine1), AddressLine2 = HtmlEncode(c.Venue.AddressLine2), County = HtmlEncode(c.Venue.County), Email = c.Venue.Email, Postcode = c.Venue.Postcode, Telephone = c.Venue.Telephone, Town = HtmlEncode(c.Venue.Town), VenueName = HtmlEncode(c.Venue.VenueName), Website = ViewModelFormatting.EnsureHttpPrefixed(c.Venue.Website), Latitude = Convert.ToDecimal(c.Venue.Latitude), Longitude = Convert.ToDecimal(c.Venue.Longitude) } : null }).ToArray(), SubRegions = regions.SelectMany( r => r.SubRegions.Where(sr => courseRun.SubRegionIds?.Contains(sr.Id) ?? false), (r, sr) => new SubRegionViewModel { SubRegionId = sr.Id, Name = sr.Name, ParentRegion = new RegionViewModel { Name = r.Name, RegionId = r.Id } }).ToArray() });
public async Task <OneOf <NotFound, TLevelDetailViewModel> > Handle(Query request, CancellationToken cancellationToken) { var tLevel = await _sqlQueryDispatcher.ExecuteQuery(new GetTLevel() { TLevelId = request.TLevelId }); if (tLevel == null) { return(new NotFound()); } var getProvider = _cosmosDbQueryDispatcher.ExecuteQuery( new Core.DataStore.CosmosDb.Queries.GetProviderById() { ProviderId = tLevel.ProviderId }); var getSqlProvider = _sqlQueryDispatcher.ExecuteQuery( new Core.DataStore.Sql.Queries.GetProviderById { ProviderId = tLevel.ProviderId }); var getVenues = _sqlQueryDispatcher.ExecuteQuery( new GetVenuesByIds() { VenueIds = tLevel.Locations.Select(l => l.VenueId) }); await Task.WhenAll(getProvider, getSqlProvider, getVenues); var provider = await getProvider; var sqlProvider = await getSqlProvider; var venues = await getVenues; var feChoice = await _cosmosDbQueryDispatcher.ExecuteQuery( new GetFeChoiceForProvider() { ProviderUkprn = provider.Ukprn }); var providerContact = provider.ProviderContact .SingleOrDefault(c => c.ContactType == "P"); return(new TLevelDetailViewModel() { TLevelId = tLevel.TLevelId, TLevelDefinitionId = tLevel.TLevelDefinition.TLevelDefinitionId, Qualification = new QualificationViewModel() { FrameworkCode = tLevel.TLevelDefinition.FrameworkCode, ProgType = tLevel.TLevelDefinition.ProgType, QualificationLevel = tLevel.TLevelDefinition.QualificationLevel.ToString(), TLevelName = HtmlEncode(tLevel.TLevelDefinition.Name) }, Provider = new ProviderViewModel() { ProviderName = sqlProvider.DisplayName, Ukprn = provider.UnitedKingdomProviderReferenceNumber, AddressLine1 = HtmlEncode( ViewModelFormatting.ConcatAddressLines( providerContact?.ContactAddress?.SAON?.Description, providerContact?.ContactAddress?.PAON?.Description, providerContact?.ContactAddress?.StreetDescription)), AddressLine2 = HtmlEncode(providerContact?.ContactAddress?.Locality), Town = HtmlEncode(providerContact?.ContactAddress?.PostTown ?? providerContact?.ContactAddress?.Items?.ElementAtOrDefault(0)), Postcode = providerContact?.ContactAddress?.PostCode, County = HtmlEncode(providerContact?.ContactAddress?.County ?? providerContact?.ContactAddress?.Items?.ElementAtOrDefault(1)), Email = providerContact?.ContactEmail, Telephone = providerContact?.ContactTelephone1, Fax = providerContact?.ContactFax, Website = ViewModelFormatting.EnsureHttpPrefixed(providerContact?.ContactWebsiteAddress), LearnerSatisfaction = feChoice?.LearnerSatisfaction, EmployerSatisfaction = feChoice?.EmployerSatisfaction }, WhoFor = HtmlEncode(tLevel.WhoFor), EntryRequirements = HtmlEncode(tLevel.EntryRequirements), WhatYoullLearn = HtmlEncode(tLevel.WhatYoullLearn), HowYoullLearn = HtmlEncode(tLevel.HowYoullLearn), HowYoullBeAssessed = HtmlEncode(tLevel.HowYoullBeAssessed), WhatYouCanDoNext = HtmlEncode(tLevel.WhatYouCanDoNext), Website = ViewModelFormatting.EnsureHttpPrefixed(tLevel.Website), StartDate = tLevel.StartDate, Locations = tLevel.Locations .Select(l => (Venue: venues[l.VenueId], Location: l)) .Select(t => new TLevelLocationViewModel() { TLevelLocationId = t.Location.TLevelLocationId, VenueName = HtmlEncode(t.Venue.VenueName), AddressLine1 = HtmlEncode(t.Venue.AddressLine1), AddressLine2 = HtmlEncode(t.Venue.AddressLine2), Town = HtmlEncode(t.Venue.Town), County = HtmlEncode(t.Venue.County), Postcode = HtmlEncode(t.Venue.Postcode), Telephone = t.Venue.Telephone, Email = t.Venue.Email, Website = ViewModelFormatting.EnsureHttpPrefixed(t.Venue.Website), Latitude = Convert.ToDecimal(t.Venue.Latitude), Longitude = Convert.ToDecimal(t.Venue.Longitude) }) .ToArray() });
public async Task <ViewModel> Handle(Query request, CancellationToken cancellationToken) { var tLevels = await _sqlQueryDispatcher.ExecuteQuery(new GetTLevels()); var providerIds = tLevels.Select(t => t.ProviderId).Distinct(); var getProviders = _cosmosDbQueryDispatcher.ExecuteQuery( new CosmosQueries.GetProvidersByIds { ProviderIds = providerIds }); var getSqlProviders = _sqlQueryDispatcher.ExecuteQuery( new GetProvidersByIds { ProviderIds = providerIds }); var getVenues = _sqlQueryDispatcher.ExecuteQuery( new GetVenuesByIds() { VenueIds = tLevels.SelectMany(t => t.Locations.Select(l => l.VenueId)).Distinct() }); await Task.WhenAll(getProviders, getSqlProviders, getVenues); var providers = await getProviders; var sqlProviders = await getSqlProviders; var venues = await getVenues; var feChoices = await _cosmosDbQueryDispatcher.ExecuteQuery( new CosmosQueries.GetFeChoicesByProviderUkprns { ProviderUkprns = providers.Values.Select(p => p.Ukprn) }); return(new ViewModel { TLevels = tLevels.Select(t => { var provider = providers[t.ProviderId]; var sqlProvider = sqlProviders[t.ProviderId]; var feChoice = feChoices.GetValueOrDefault(provider.Ukprn); var providerContact = provider.ProviderContact .SingleOrDefault(c => c.ContactType == "P"); return new TLevelDetailViewModel { TLevelId = t.TLevelId, TLevelDefinitionId = t.TLevelDefinition.TLevelDefinitionId, Qualification = new QualificationViewModel { TLevelName = t.TLevelDefinition.Name, FrameworkCode = t.TLevelDefinition.FrameworkCode, ProgType = t.TLevelDefinition.ProgType, QualificationLevel = t.TLevelDefinition.QualificationLevel.ToString(), }, Provider = new ProviderViewModel { ProviderName = sqlProvider.DisplayName, Ukprn = provider.UnitedKingdomProviderReferenceNumber, AddressLine1 = ViewModelFormatting.ConcatAddressLines(providerContact?.ContactAddress?.SAON?.Description, providerContact?.ContactAddress?.PAON?.Description, providerContact?.ContactAddress?.StreetDescription), AddressLine2 = providerContact?.ContactAddress?.Locality, Town = providerContact?.ContactAddress?.PostTown ?? providerContact?.ContactAddress?.Items?.ElementAtOrDefault(0), Postcode = providerContact?.ContactAddress?.PostCode, County = providerContact?.ContactAddress?.County ?? providerContact?.ContactAddress?.Items?.ElementAtOrDefault(1), Email = providerContact?.ContactEmail, Telephone = providerContact?.ContactTelephone1, Fax = providerContact?.ContactFax, Website = ViewModelFormatting.EnsureHttpPrefixed(providerContact?.ContactWebsiteAddress), LearnerSatisfaction = feChoice?.LearnerSatisfaction, EmployerSatisfaction = feChoice?.EmployerSatisfaction }, WhoFor = t.WhoFor, EntryRequirements = t.EntryRequirements, WhatYoullLearn = t.WhatYoullLearn, HowYoullLearn = t.HowYoullLearn, HowYoullBeAssessed = t.HowYoullBeAssessed, WhatYouCanDoNext = t.WhatYouCanDoNext, Website = ViewModelFormatting.EnsureHttpPrefixed(t.Website), StartDate = t.StartDate, Locations = t.Locations.Select(l => { var venue = venues[l.VenueId]; return new TLevelLocationViewModel { TLevelLocationId = l.TLevelLocationId, VenueName = venue.VenueName, AddressLine1 = venue.AddressLine1, AddressLine2 = venue.AddressLine2, Town = venue.Town, County = venue.County, Postcode = venue.Postcode, Telephone = venue.Telephone, Email = venue.Email, Website = ViewModelFormatting.EnsureHttpPrefixed(venue.Website), Latitude = Convert.ToDecimal(venue.Latitude), Longitude = Convert.ToDecimal(venue.Longitude) }; }).ToArray(), }; }).ToArray() }); }