public async Task If_a_campaign_is_currently_open__handler_returns_that_campaign() { var sp = new ServiceProviderBuilder().BuildServiceProvider(ConfigureServices); using (var scope = sp.CreateScope()) { var dbSession = scope.ServiceProvider.GetRequiredService <DbSessionProvider>(); await dbSession.CreateSession().SaveAsync(new Campaign( startDateTime: NodaTime.SystemClock.Instance.GetOffsetDateTime().Minus(NodaTime.Duration.FromDays(7)), endDateTime: NodaTime.SystemClock.Instance.GetOffsetDateTime().Plus(NodaTime.Duration.FromDays(7)), editionId: 1, name: "test")); } using (var scope = sp.CreateScope()) { var mediator = scope.ServiceProvider.GetRequiredService <IMediator>(); var query = new GetCurrentCampaign.Query(); var result = await mediator.Send(query); Assert.True(result.HasValue); var campaign = result.Value; Assert.Equal("test", campaign.Name); } }
public async Task <Maybe <Details.Campaign> > Handle(GetCurrentCampaign.Query request, CancellationToken cancellationToken) { var now = _clock.GetCurrentInstant(); var campaigns = await _campaignRepo.GetAll(); var campaign = campaigns.SingleOrDefault(x => x.StartDateTime.ToInstant() < now && x.EndDateTime.ToInstant() > now); if (campaign == null) { return(Maybe <Details.Campaign> .None); } var campaignDetails = await _detailsHandler.Handle(new Details.Query() { CampaignID = campaign.Id }, cancellationToken); return(Maybe <Details.Campaign> .From(campaignDetails.Value)); }