Beispiel #1
0
    public async Task <Result <SurveyModel, Error> > Handle(GetSurveyDetailQuery request, CancellationToken cancellationToken)
    {
        var cacheKey = $"{request.Id}";

        var(isCached, cachedSurvey) = await _cache.TryGetValueAsync(cacheKey, cancellationToken);

        if (isCached)
        {
            return(cachedSurvey);
        }

        var survey = await _surveyContext.Surveys
                     .Include(s => s.Options)
                     .Include(s => s.Owner)
                     .ProjectTo <SurveyModel>(_mapper.ConfigurationProvider)
                     .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);

        if (survey is null)
        {
            return(Errors.General.NotFound(nameof(Survey), request.Id));
        }

        await _cache.SetAsync(cacheKey, survey, 60, cancellationToken);

        return(survey);
    }
Beispiel #2
0
        public async Task Handle_Returns_Correct_Type()
        {
            const int id = 1;

            var query = new GetSurveyDetailQuery(id);

            var handler = new GetSurveyDetailWithEntityFrameworkQueryHandler(_surveyContext, _mapper, _cache);

            var result = await handler.Handle(query, CancellationToken.None);

            result.ShouldBeOfType <Result <SurveyModel, Error> >();
        }
Beispiel #3
0
        public async Task Handle_Returns_Error_When_Survey_Id_Does_Not_Exist()
        {
            const int id = 100;

            var query = new GetSurveyDetailQuery(id);

            var handler = new GetSurveyDetailWithEntityFrameworkQueryHandler(_surveyContext, _mapper, _cache);

            var result = await handler.Handle(query, CancellationToken.None);

            result.Error.ShouldBe(Errors.General.NotFound());
        }
    public async Task GivenExistingSurveyId_WhenCallingHandle_ThenExpectedResultTypeShouldBeReturned()
    {
        const int id = 1;

        var query = new GetSurveyDetailQuery(id);

        var handler = new GetSurveyDetailQueryHandler(_surveyContext, _mapper, _cache);

        var result = await handler.Handle(query, CancellationToken.None);

        result.Should().BeOfType <Result <SurveyModel, Error> >();
    }
    public async Task GivenSurveyIdWhichDoesNotExist_WhenCallingHandle_ThenResponseShouldIndicateNotFoundError()
    {
        const int id = 100;

        var query = new GetSurveyDetailQuery(id);

        var handler = new GetSurveyDetailQueryHandler(_surveyContext, _mapper, _cache);

        var result = await handler.Handle(query, CancellationToken.None);

        result.Error.Should().Be(Errors.General.NotFound());
    }
Beispiel #6
0
        public async Task Handle_Returns_Correct_Id()
        {
            const int id = 1;

            var query = new GetSurveyDetailQuery(id);

            var handler = new GetSurveyDetailWithEntityFrameworkQueryHandler(_surveyContext, _mapper, _cache);

            var result = await handler.Handle(query, CancellationToken.None);

            var survey = result.Value;

            survey.Id.ShouldBe(id);
        }
    public async Task GivenExistingSurveyId_WhenCallingHandle_ThenReturnedSurveyIdShouldMatchGivenSurveyId()
    {
        const int id = 1;

        var query = new GetSurveyDetailQuery(id);

        var handler = new GetSurveyDetailQueryHandler(_surveyContext, _mapper, _cache);

        var result = await handler.Handle(query, CancellationToken.None);

        var survey = result.Value;

        survey.Id.Should().Be(id);
    }
Beispiel #8
0
        public async Task Handle_Returns_Correct_Respondent_Type()
        {
            const int    id = 1;
            const string expectedTopicText = "Testers";

            var query = new GetSurveyDetailQuery(id);

            var handler = new GetSurveyDetailWithEntityFrameworkQueryHandler(_surveyContext, _mapper, _cache);

            var result = await handler.Handle(query, CancellationToken.None);

            var survey = result.Value;

            survey.RespondentType.ShouldBe(expectedTopicText);
        }
Beispiel #9
0
        public async Task Handle_Returns_Correct_Number_Of_Respondents()
        {
            const int id = 1;
            const int expectedNumberOfRespondents = 10;

            var query = new GetSurveyDetailQuery(id);

            var handler = new GetSurveyDetailWithEntityFrameworkQueryHandler(_surveyContext, _mapper, _cache);

            var result = await handler.Handle(query, CancellationToken.None);

            var survey = result.Value;

            survey.NumberOfRespondents.ShouldBe(expectedNumberOfRespondents);
        }
    public async Task GivenExistingSurveyId_WhenCallingHandle_ThenReturnedSurveyRespondentTypeShouldMatchExpectedValue()
    {
        const int    id = 1;
        const string expectedTopicText = "Testers";

        var query = new GetSurveyDetailQuery(id);

        var handler = new GetSurveyDetailQueryHandler(_surveyContext, _mapper, _cache);

        var result = await handler.Handle(query, CancellationToken.None);

        var survey = result.Value;

        survey.RespondentType.Should().Be(expectedTopicText);
    }
    public async Task GivenExistingSurveyId_WhenCallingHandle_ThenReturnedSurveyNumberOfRespondentsShouldMatchExpectedValue()
    {
        const int id = 1;
        const int expectedNumberOfRespondents = 10;

        var query = new GetSurveyDetailQuery(id);

        var handler = new GetSurveyDetailQueryHandler(_surveyContext, _mapper, _cache);

        var result = await handler.Handle(query, CancellationToken.None);

        var survey = result.Value;

        survey.NumberOfRespondents.Should().Be(expectedNumberOfRespondents);
    }