public async Task <ActionResult <ServiceRequestDTO> > UpdateServiceRequestByIdAsync(Guid id, [FromBody] PutUpdateServiceRequestDTO request)
        {
            var command = new UpdateServiceRequestByIdCommand(id, request);

            try
            {
                var response = await _mediator.Send(command);

                if (response is null)
                {
                    return(NotFound());
                }

                return(Ok(response.AsServiceRequestDTO()));
            }
            catch (ArgumentException ex)
            {
                // TODO: TECHDEBT - Find a better implementation for this.
                // I should probably aggregate a collection of errors produced in the request pipeline instead of catching a single exception and returning a single error.
                var details = new ProblemDetails()
                {
                    Title  = ex.Message,
                    Status = (int)HttpStatusCode.BadRequest,
                    Type   = "https://tools.ietf.org/html/rfc7231#section-6.5.1",
                };

                return(new ObjectResult(details)
                {
                    ContentTypes = { "application/problem+json" },
                    StatusCode = (int)HttpStatusCode.BadRequest
                });
            }
        }
Exemplo n.º 2
0
        public async void Given_UpdateServiceRequestByIdCommand_And_ServiceRequestId_When_HandlingUpdateServiceRequestByIdCommand_AndServiceRequestDoesNotExist_Then_ReturnsNull()
        {
            // Arrange
            var updateDTO = new PutUpdateServiceRequestDTO
            {
                BuildingCode  = "456",
                ModifiedBy    = "Aaron Jaeger",
                CurrentStatus = CurrentStatus.Created,
                Description   = "Now it's too hot!"
            };

            var updateServiceRequestCommand = new UpdateServiceRequestByIdCommand(Guid.Empty, updateDTO);

            _serviceRequestRepository
            .Setup(repo => repo.RetrieveByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync((ServiceRequest)null);

            var target = new UpdateServiceRequestByIdCommandHandler(_logger.Object, _serviceRequestRepository.Object);

            // Act
            var actual = await target.Handle(updateServiceRequestCommand, default);

            // Assert
            Assert.Null(actual);
        }
Exemplo n.º 3
0
        public async void Given_UpdateServiceRequestByIdCommand_And_ServiceRequestId_When_HandlingUpdateServiceRequestByIdCommand_AndServiceRequestExists_Then_ReturnsUpdatedServiceRequestEntity()
        {
            // Arrange
            var expectedBuildingCode   = "456";
            var expectedLastModifiedBy = "Aaron Jaeger";
            var expectedCurrentStatus  = CurrentStatus.Created;
            var expectedDescription    = "Now it's too hot!";

            var updateDTO = new PutUpdateServiceRequestDTO
            {
                BuildingCode  = expectedBuildingCode,
                ModifiedBy    = expectedLastModifiedBy,
                CurrentStatus = expectedCurrentStatus,
                Description   = expectedDescription
            };

            var expectedCreatedBy = "Aaron";

            var serviceRequestToUpdate = new ServiceRequest("123", "Turn up the heat!", expectedCreatedBy);

            var updateServiceRequestCommand = new UpdateServiceRequestByIdCommand(Guid.Empty, updateDTO);

            _serviceRequestRepository
            .Setup(repo => repo.RetrieveByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(serviceRequestToUpdate);

            var target = new UpdateServiceRequestByIdCommandHandler(_logger.Object, _serviceRequestRepository.Object);

            // Act
            var actual = await target.Handle(updateServiceRequestCommand, default);

            // Assert
            Assert.Equal(expectedBuildingCode, actual.GetBuildingCode);
            Assert.Equal(expectedDescription, actual.GetDescription);
            Assert.Equal(expectedCurrentStatus, actual.CurrentStatus);
            Assert.Equal(expectedLastModifiedBy, actual.GetLastModifiedBy);
            Assert.Equal(expectedCreatedBy, actual.GetCreatedBy);
            Assert.InRange((DateTime)actual.GetLastModifiedDate, DateTime.UtcNow.AddSeconds(-30), DateTime.UtcNow);
        }
Exemplo n.º 4
0
        public async void Given_UpdateServiceRequestByIdCommand_And_ServiceRequestId_When_HandlingUpdateServiceRequestByIdCommand_With_InvalidData_AndServiceRequestExists_Then_ThrowsArgumentException()
        {
            // Arrange
            var updateDTO = new PutUpdateServiceRequestDTO
            {
                BuildingCode  = "456",
                ModifiedBy    = "       ",
                CurrentStatus = (CurrentStatus)43,
                Description   = "Now it's too hot!"
            };

            var serviceRequestToUpdate = new ServiceRequest("123", "Turn up the heat!", "Aaron");

            var updateServiceRequestCommand = new UpdateServiceRequestByIdCommand(Guid.Empty, updateDTO);

            _serviceRequestRepository
            .Setup(repo => repo.RetrieveByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(serviceRequestToUpdate);

            var target = new UpdateServiceRequestByIdCommandHandler(_logger.Object, _serviceRequestRepository.Object);

            // Act /Assert
            await Assert.ThrowsAsync <ArgumentException>(() => target.Handle(updateServiceRequestCommand, default));
        }