Пример #1
0
        public IActionResult PutBanner([FromRoute] int bannerId, [FromBody] PutBanner putBanner)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var parseErrors = _validateHtml.Validate(putBanner.Html);

            if (parseErrors.Any())
            {
                return(BadRequest(new ErrorResponse(parseErrors)));
            }

            if (!_repository.TryGet(bannerId, out var banner))
            {
                return(BadRequest($"Banner with {bannerId} not found"));
            }

            if (!_repository.TrySave(_mapper.Map(banner, putBanner)))
            {
                return(BadRequest($"Failed to save banner {bannerId}"));
            }

            return(Ok());
        }
Пример #2
0
        public void Should_update_banner_with_correct_id()
        {
            var updateBanner = new PutBanner {
                Html = "html2"
            };
            var mappedPutBanner = new Banner {
                Id = 1, Html = updateBanner.Html
            };

            var storedBanner = new Banner {
                Id = 1, Html = "html1"
            };

            A.CallTo(() => _repository.TryGet(A <int> .Ignored, out storedBanner))
            .Returns(true)
            .AssignsOutAndRefParameters(storedBanner);

            Banner savedBanner = null;

            A.CallTo(() => _repository.TrySave(A <Banner> .Ignored))
            .Invokes((Banner banner) => savedBanner = banner)
            .Returns(true);

            Banner    oldBanner = null;
            PutBanner newBanner = null;

            A.CallTo(() => _mapper.Map(A <Banner> .Ignored, A <PutBanner> .Ignored))
            .Invokes((Banner banner, PutBanner putBanner) =>
            {
                oldBanner = banner;
                newBanner = putBanner;
            })
            .Returns(mappedPutBanner);

            var result = _controller.PutBanner(1, updateBanner) as StatusCodeResult;

            result.StatusCode.Should().Be(StatusCodes.Status200OK);

            oldBanner.Id.Should().Be(1);
            oldBanner.Html.Should().Be("html1");

            newBanner.Html.Should().Be("html2");

            savedBanner.Id.Should().Be(1);
            savedBanner.Html.Should().Be("html2");
        }