public async Task <IActionResult> Put([FromBody] RentRequest request)
        {
            var productId = Guid.Parse(request.ProductId);
            var clientId  = Guid.Parse(request.ClientId);
            var status    = StatusHelper.Parse(request.Details?.Status);
            var until     = request.Details?.Until != null?DateHelper.Parse(request.Details.Until) : (DateTime?)null;

            using var ctx = serviceProvider.GetService <IVideoclubDbContext>();
            var rent = await GetRent(ctx, productId, clientId).ConfigureAwait(false);

            var isNew = rent is null;

            if (isNew)
            {
                rent = new Rent(productId, clientId, status, until);
                ctx.Rents.Add(rent);
            }
            else
            {
                rent.ChangeStatus(status, until);
            }
            await ctx.SaveChangesAsync().ConfigureAwait(false);

            var result = mapper.Map <RentResponse>(rent);

            return(isNew ? Created(UrlUtls.URI(this, nameof(RentController), nameof(RentController.Get), new { productId, clientId, version = "1.0" }), result)
                         : (IActionResult)Ok(result));
        }
Exemplo n.º 2
0
        public void ShouldRemoveControllerSuffix()
        {
            // Arrange
            var serviceProviderMock    = new Mock <IServiceProvider>();
            var mapperMock             = new Mock <IMapper>();
            var urlHelperMock          = new Mock <IUrlHelper>(MockBehavior.Strict);
            var statusHelperMock       = new Mock <StatusHelper>();
            var xmlSchemaValidatorMock = new Mock <IRentXmlSchemaValidator>();
            var requestMock            = new Mock <HttpRequest>();
            var httpContextMock        = Mock.Of <HttpContext>(_ => _.Request == requestMock.Object);
            var controllerContext      = new ControllerContext()
            {
                HttpContext = httpContextMock
            };
            var values         = new { };
            var actionName     = "GetAll";
            var controllerName = "Rent";
            var scheme         = "http";

            requestMock.Setup(x => x.Scheme).Returns(scheme);
            urlHelperMock.Setup(url => url.Action(It.IsAny <UrlActionContext>())).Returns("mock/url");

            var controller = new RentController(serviceProviderMock.Object, mapperMock.Object, xmlSchemaValidatorMock.Object)
            {
                ControllerContext = controllerContext,
                Url = urlHelperMock.Object
            };

            //Act
            UrlUtls.URI(controller, "RentController", "GetAll", values);

            //Assert
            urlHelperMock.Verify(url => url.Action(It.Is <UrlActionContext>(i => i.Action == actionName && i.Controller == controllerName && i.Protocol == scheme)), Times.Once());
        }