Пример #1
0
        public async Task <IdResultDto <long> > AddRequestAsync <T>(T requestDto, WsClient wsClient)
            where T : AbstractRequestDto
        {
            IdResultDto <long> result = new IdResultDto <long>();

            // saving img to disk
            if (requestDto is PostDto)
            {
                PostDto post = requestDto as PostDto;
                TrimUserInfo(post.User);
                ProcessImage(post.Image);
            }

            // preparing request
            Request request = PrepareRequest(requestDto, wsClient);

            // saving to db
            long id = await _requestRepository.AddRequestAsync(request);

            // preparing result
            if (id > 0)
            {
                result.IsOk    = true;
                result.Id      = id;
                result.Message = "Request successfully added to queue.";
                result.Url     = _urlHelper.Link(requestDto.RouteName, new { id });
            }
            else
            {
                result.Message = "Something went wrong while adding request to db...";
            }

            return(result);
        }
Пример #2
0
        private async Task <IActionResult> ProcessRequestAsync <T>(T requestDto) where T : AbstractRequestDto
        {
            IActionResult result;

            string typeForLogs = typeof(T).Name;

            if (requestDto == null)
            {
                _logger.LogError("{0} Null input for '{1}'", LogDescription(), typeForLogs);
                result = BadRequest(new ResultDto {
                    Message = "Null input."
                });
            }
            else
            {
                // validating input
                MessageListResult validationResult = requestDto.Validate();

                if (!validationResult.IsOk)
                {
                    _logger.LogError("{0} ValidErr for '{1}': {2}",
                                     LogDescription(), typeForLogs, validationResult.Message);
                    ResultDto badValidationResult = _mapper.Map <ResultDto>(validationResult);
                    result = BadRequest(badValidationResult);
                }
                else
                {
                    // getting wsClient
                    WsClient wsClient = await GetWsClient();

                    // adding request to queue
                    IdResultDto <long> addResult = await _requestService.AddRequestAsync(requestDto, wsClient);

                    if (addResult.IsOk)
                    {
                        _logger.LogDebug("{0} Ok for: '{1}' -> Id: '{2}'",
                                         LogDescription(), typeForLogs, addResult.Id);
                        result = Ok(addResult);
                    }
                    else
                    {
                        // when ID from DB <= 0
                        _logger.LogError("{0} Error for: '{1}' -> Message: '{2}'",
                                         LogDescription(), typeForLogs, addResult.Message);
                        result = InternalServerError(addResult);
                    }
                }
            }

            return(result);
        }
Пример #3
0
        public async void AddPostNotOk()
        {
            // Arrange
            List <WsClient>    expectedWsClients = _fixture.CreateMany <WsClient>(1).ToList();
            PostDto            postDto           = _fixture.Create <PostDto>();
            IdResultDto <long> expectedResult    = _fixture.Build <IdResultDto <long> >()
                                                   .With(x => x.IsOk, false).With(x => x.Id, 0)
                                                   .Create();

            _postServiceMock.Setup(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>())).ReturnsAsync(expectedResult);
            _cacheServiceMock.Setup(x => x.GetWsClientsAsync()).ReturnsAsync(expectedWsClients);
            _controller.ControllerContext.HttpContext.Request.Headers["Hash"] = _fixture.Create <string>();

            // Act
            IActionResult actionResult = await _controller.AddPost(postDto);

            // Assert
            actionResult.Should().NotBeNull().And.BeOfType <ObjectResult>();

            _cacheServiceMock.Verify(x => x.GetWsClientsAsync(), Times.Once);
            _postServiceMock.Verify(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>()), Times.Once);
            _loggerMock.Verify(m => m.Log(
                                   LogLevel.Error,
                                   0,
                                   It.Is <FormattedLogValues>(v => v.ToString()
                                                              .Contains($"Error for: '{postDto.GetType().Name}' -> Message: '{expectedResult.Message}'")),
                                   null,
                                   It.IsAny <Func <object, Exception, string> >()
                                   ));

            var objectResult = actionResult as ObjectResult;

            objectResult.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
            objectResult.Value.Should().NotBeNull().And.BeOfType(typeof(IdResultDto <long>));
            var idResultDto = objectResult.Value as IdResultDto <long>;

            idResultDto.IsOk.Should().Be(false);
            idResultDto.Message.Should().NotBeNull().And.Be(expectedResult.Message);
            idResultDto.Id.Should().Be(0, because: "Post shouldn't be added");
        }
Пример #4
0
        public async void AddPostOk()
        {
            // Arrange
            List <WsClient>    expectedWsClients = _fixture.CreateMany <WsClient>(1).ToList();
            PostDto            postDto           = _fixture.Create <PostDto>();
            IdResultDto <long> expectedResult    = _fixture.Build <IdResultDto <long> >()
                                                   .With(x => x.IsOk, true)
                                                   .Create();

            _postServiceMock.Setup(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>())).ReturnsAsync(expectedResult);
            _cacheServiceMock.Setup(x => x.GetWsClientsAsync()).ReturnsAsync(expectedWsClients);

            // Act
            IActionResult actionResult = await _controller.AddPost(postDto);

            // Assert
            actionResult.Should().NotBeNull().And.BeOfType <OkObjectResult>();

            _cacheServiceMock.Verify(x => x.GetWsClientsAsync(), Times.Once);
            _postServiceMock.Verify(x => x.AddRequestAsync(postDto, It.IsAny <WsClient>()), Times.Once);
            _loggerMock.Verify(m => m.Log(
                                   LogLevel.Debug,
                                   0,
                                   It.Is <FormattedLogValues>(v => v.ToString()
                                                              .Contains($"Ok for: '{postDto.GetType().Name}' -> Id: '{expectedResult.Id}'")),
                                   null,
                                   It.IsAny <Func <object, Exception, string> >()
                                   ));

            var objectResult = actionResult as OkObjectResult;

            objectResult.StatusCode.Should().Be(StatusCodes.Status200OK);
            objectResult.Value.Should().NotBeNull().And.BeOfType(typeof(IdResultDto <long>));
            var idResultDto = objectResult.Value as IdResultDto <long>;

            idResultDto.IsOk.Should().Be(true);
            idResultDto.Message.Should().NotBeNull().And.Be(expectedResult.Message);
            idResultDto.Id.Should().Be(expectedResult.Id);
        }