Example #1
0
        public async void GetSearchReportQueryHandler_should_return_search_report_by_period()
        {
            // Arrange
            var searches = Builder <SearchReport> .CreateListOfSize(10)
                           .TheFirst(1)
                           .With(s => s.TotalRequests = 10)
                           .With(s => s.Date          = new DateTime(2021, 2, 3))
                           .TheRest()
                           .With(s => s.TotalRequests = 3)
                           .With(s => s.Date          = new DateTime(2021, 3, 3))
                           .Build();

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

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

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

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

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

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

            return(Ok(result));
        }
Example #4
0
        public async void CreateSearchCommand_should_return_ValidationException_when_period_end_greater_that_period_start()
        {
            // Arrange
            var command = new GetSearchReportQuery
            {
                StartDate = new DateTime(2021, 2, 4),
                EndDate   = new DateTime(2021, 2, 1)
            };

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