Пример #1
0
        public async void GetSearchesByPeriodQueryHandler_should_return_searches_by_period()
        {
            // Arrange
            var searches = Builder <Search> .CreateListOfSize(10)
                           .TheFirst(1)
                           .With(s => s.Id        = "1254")
                           .With(s => s.Timestamp = new DateTime(2021, 2, 3))
                           .TheRest()
                           .With(s => s.Id        = Guid.NewGuid().ToString())
                           .With(s => s.Timestamp = new DateTime(2021, 3, 3))
                           .Build();

            A.CallTo(() => _mongoDBService.GetAsync(new DateTime(2021, 2, 1), new DateTime(2021, 2, 4))).Returns(new List <Search>()
            {
                searches[0]
            });
            var command = new GetSearchesByPeriodQuery
            {
                StartDate = new DateTime(2021, 2, 1),
                EndDate   = new DateTime(2021, 2, 4)
            };

            // Act
            var result = await _getSearchesByPeriodQueryHandler.Handle(command, default);

            // Assert
            A.CallTo(() => _mongoDBService.GetAsync(new DateTime(2021, 2, 1), new DateTime(2021, 2, 4))).MustHaveHappenedOnceExactly();
            Assert.Equal(searches[0].Id, result.ToList()[0].Id);
        }
        public async Task <IEnumerable <SearchDto> > Handle(GetSearchesByPeriodQuery request, CancellationToken cancellationToken)
        {
            if (request.EndDate <= request.StartDate)
            {
                throw new ValidationException("End date must be greater than start date.");
            }

            var result = await _mongoDBService.GetAsync(request.StartDate, request.EndDate);

            return(_mapper.Map <List <SearchDto> >(result));
        }
        public async Task <IActionResult> GetAsync([FromQuery] GetSearchesByPeriodQuery query, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Administrator get searches by period {query.StartDate} and {query.EndDate} ");
            var result = await _mediator.Send(query, cancellationToken);

            if (result is null)
            {
                return(NotFound("Search not found!"));
            }

            return(Ok(result));
        }
Пример #4
0
        public async void CreateSearchCommand_should_return_ValidationException_when_period_end_greater_that_period_start()
        {
            // Arrange
            var command = new GetSearchesByPeriodQuery
            {
                StartDate = new DateTime(2021, 2, 4),
                EndDate   = new DateTime(2021, 2, 1)
            };

            // Act Assert
            await Assert.ThrowsAsync <ValidationException>(async() =>
                                                           await _getSearchesByPeriodQueryHandler.Handle(command, default));
        }